icon in JTabbedPane is not shown - java

I have a problem with my tabs:
JTabbedPane tab = new JTabbedPane();
frame.add(tab, BorderLayout.CENTER);
JPanel contact = new JPanel();
contact.add(backgroundContact);
tab.add("Contacto", contact);
//tab.addTab("Contacto",new ImageIcon("images/image2.gif"), contact,"");
JPanel schedule = new JPanel();
schedule.add(backgroundSchedule);
tab.add("Horario", schedule);
//tab.addTab("Horario", new ImageIcon("images/image2.gif"), schedule,"");
JPanel cost = new JPanel();
cost.add(backgroundCost);
tab.add("Tarifas", cost);
//tab.addTab("Tarifas", new ImageIcon("images/image3.gif"), cost,"");
// Los iconos
tab.setIconAt(0, new ImageIcon("images/image1.gif"));
tab.setIconAt(1, new ImageIcon("images/image2.gif"));
tab.setIconAt(2, new ImageIcon("images/image3.gif"));
I've tried both options, but the icons are not shown. Why is it happening?
I also tried: new ImageIcon("images/im.gif") which doesn't exist and I haven any error

Try this instead:
URL urlToImage3 = this.getClass().getResource("/" + "images/image3.gif");
... new ImageIcon(urlToImage3);
You might concatenate "/" + "images/image3.gif" - I just wanted to highlight the leading /, since it is more robust to search from the root of the class-path.
If these images are an 'embedded resource' as I suspect, they will not be available by File at run-time, but should be on the class-path in one of the Jars of the app., and therefore available by URL.

Related

how to add a "console" like window to a GUI?

I have created a GUI. It has several buttons. i would like to add a console like object underneath them in which i would be able to write messages so the user would see them.
what element/object/class should i use? i want it to be able to present messages in different lines.
here is my code for creating the GUI:
public ssGUI() {
setLayout(new BorderLayout());
bRunNoAdj = new JButton("Run no adjustment");
bRunNoAdj.setVerticalTextPosition(AbstractButton.CENTER);
bRunNoAdj.setHorizontalTextPosition(AbstractButton.LEADING);
bRunNoAdj.setMnemonic(KeyEvent.VK_E);
bRunNoAdj.addActionListener(this);
bRunNoAdj.setEnabled(false);
bRunNoAdj.setBackground(Color.white);
bRunAdj = new JButton("Run adjustment");
bRunAdj.setVerticalTextPosition(AbstractButton.CENTER);
bRunAdj.setHorizontalTextPosition(AbstractButton.LEADING);
bRunAdj.setMnemonic(KeyEvent.VK_E);
bRunAdj.addActionListener(this);
bRunAdj.setEnabled(false);
bRunAdj.setBackground(Color.white);
bConnect = new JButton("Connect");
bConnect.setMnemonic(KeyEvent.VK_E);
bConnect.addActionListener(this);
bConnect.setEnabled(true);
bConnect.setBackground(Color.white);
bDisconnect = new JButton("Disconnect");
bDisconnect.setMnemonic(KeyEvent.VK_E);
bDisconnect.addActionListener(this);
bDisconnect.setEnabled(false);
bDisconnect.setBackground(Color.white);
bStationary = new JButton("Show Stationary");
bStationary.setMnemonic(KeyEvent.VK_E);
bStationary.addActionListener(this);
bStationary.setEnabled(false);
bStationary.setBackground(Color.white);
bMoving = new JButton("Show Moving");
bMoving.setMnemonic(KeyEvent.VK_E);
bMoving.addActionListener(this);
bMoving.setEnabled(false);
bMoving.setBackground(Color.white);
JPanel topPanel = new JPanel();
topPanel.add(bConnect);
topPanel.add(bDisconnect);
add(topPanel, BorderLayout.NORTH);
JPanel centerPanel = new JPanel();
centerPanel.add(bRunNoAdj);
centerPanel.add(bRunAdj);
add(centerPanel, BorderLayout.CENTER);
JPanel bottomPanel = new JPanel();
bottomPanel.add(bStationary);
bottomPanel.add(bMoving);
add(bottomPanel, BorderLayout.SOUTH);
}
any help would be appreciated, thank you.
The easiest would probably be to use a JTextArea.
You would ofcourse prevent the user from editing the area, this can be done like this:
JTextArea area = new JTextArea();
area.setEditable(false);
And if you want the user to be able to scroll the area you can add it to a JScrollPane like this:
JTextArea area = new JTextArea();
JScrollPane scrollableArea = new JScrollPane(area);
And lastly you can add a line to the area by doing:
area.setText(area.getText() + "\n" + newLine);
Or preferably:
area.append("\n" + newLine);
I hope this helps :)
Use a JTextArea. Call text_area.setEditable(false); on it to make it read-only.
I think what you mean is JTextArea or JTextField

BorderLayout() doesn't seem to be working. JButtons won't go to SOUTH

As the title says, a panel with JButtons in it stays north for some reason. Here's the code which I think is relevant.
f = new JFrame();
f.setTitle("Book Quiz");
f.setSize(800, 400);
f.setLocation(400, 250);
f.setResizable(false);
f.setDefaultCloseOperation(EXIT_ON_CLOSE);
card = new JPanel();
cardLayout = new CardLayout();
card.setLayout(cardLayout);
takeQuizCard = new JPanel();
takeQuizCard.setLayout(new BorderLayout());
quizButtons = new JPanel();
submit = new JButton("Submit Answer");
next = new JButton("Next");
quizDone = new JButton("Done");
quizDone.addActionListener(this);
quizQuit = new JButton("Quit");
quizQuit.addActionListener(this);
quizButtons.setLayout(new FlowLayout());
quizButtons.add(submit);
quizButtons.add(next);
quizButtons.add(quizDone);
quizButtons.add(quizQuit);
takeQuizCard.add(quizButtons, BorderLayout.SOUTH);
quizInfo = new JPanel(new GridLayout(0, 1));
card.add(takeQuizCard, TAKE_QUIZ_CARD);
takeQuizCard.add(quizButtons);
f.add(card);
There are also 4 radio buttons and two labels on the WEST part. I left it out so it doesn't distract anyone, but if it is relevant, I'll add it. Anyone have any ideas? I have another 'card' in my program that works properly, and everything seems to be the same code-wise.
You are re-adding quizButtons on the second last line. It is overriding your previous placement of SOUTH.
Remove:
takeQuizCard.add(quizButtons);
And keep:
takeQuizCard.add(quizButtons, BorderLayout.SOUTH);

separate two sets of code

How to separate the components from the sql method? I need to get this set of code separated from the rest. I am having difficulty because it linked.
Component droplabel = new DropTargetTextArea("test", "testing");
JLabel cellLabel = new JLabel(icon);
JPanel cellPanel = new JPanel(new BorderLayout());
cellPanel.add(cellLabel, BorderLayout.NORTH);
cellPanel.add(droplabel, BorderLayout.CENTER);
gridPanel.add(cellPanel);
////full code
connection = getConnection();
try {
statement = (PreparedStatement) connection
.prepareStatement("select image from image");
result = statement.executeQuery();
while (result.next()) {
byte[] image = null;
image = result.getBytes("image");
Image img = Toolkit.getDefaultToolkit().createImage(image);
ImageIcon icon = new ImageIcon(img);
Component droplabel = new DropTargetTextArea("test", "testing");
JLabel cellLabel = new JLabel(icon);
JPanel cellPanel = new JPanel(new BorderLayout());
cellPanel.add(cellLabel, BorderLayout.NORTH);
cellPanel.add(droplabel, BorderLayout.CENTER);
gridPanel.add(cellPanel);
}
}
Basically you have two tasks here:
Retrieve a set of images from the underlying database. I say set because you are using a while-loop for iterating over a ResultSet.
connection = getConnection();
try {
statement = (PreparedStatement) connection.prepareStatement("select image from image");
result = statement.executeQuery();
while (result.next()) {
byte[] image = null;
image = result.getBytes("image");
}
}
You could extract this code to a separate method and use an byte-array to store the retrieved information. This array would be the return-value of the method.
Creating ImageIcons and using them in JLabels
Image img = Toolkit.getDefaultToolkit().createImage(image);
ImageIcon icon = new ImageIcon(img);
Component droplabel = new DropTargetTextArea("test", "testing");
JLabel cellLabel = new JLabel(icon);
JPanel cellPanel = new JPanel(new BorderLayout());
cellPanel.add(cellLabel, BorderLayout.NORTH);
cellPanel.add(droplabel, BorderLayout.CENTER);
gridPanel.add(cellPanel);
This code could also be moved to a separate method. The method retrieves an array of images (or only one depending on your setup) and created the ImageIcon.
First a little Tip: you should read about JPA and MVC.
Now to your code: Make a new class and give her a name like "DatabaseHelper" and then put your whole JDBC-Code in a method like "getAllImages()" and make a container class (POJO) for the Image. For the first term this should help you but on the long term you should use JPA and MVC.

Java getClass().getResource on a png returning Null Pointer

I am not sure if I am referring to the right location with this code, the images I am trying to access are titled Flower0.png etc.
They are located in the same directory as the rest of my code for this project.
This class is in a src folder called hangman.ui and the .png files are located in a directory folder called Resources.
Perhaps getClass().getResource is not right?
This is my first time trying to put images into a GUI.
Help is much appreciated!
public WiltingFlowerRendererRemix(HangmanLogic logic)
{
panel = new JPanel();
panel.setLayout(new BorderLayout());
imageLabel = new JLabel();
panel.add(imageLabel, BorderLayout.CENTER);
int numberOfImages = 10;
images = new ImageIcon[numberOfImages];
for (int i = 0; i < numberOfImages; i++)
{
images[i] = new ImageIcon(getClass().getResource("Flower"+Integer.toString(i) + ".png"));
}
}
You say the images are in a folder called "Resources"? You can load images like this then:
BufferedImage image = ImageIO.read(getClass().getResource("/Resources/Flower0.png"));
ImageIcon icon = new ImageIcon(image);
To use it on the GUI you can use a JLabel.
JLabel label = new JLabel();
label.setIcon(icon);
And then add the label to a panel for example.
For me Works...
According to Maven:
https://maven.apache.org/guides/introduction/introduction-to-the-standard-directory-layout.html
src/main/resources: Application/Library resources
Then I put the ICON in this Location:
PROJECTNAME\src\main\resources\usedPictures\
--Open.png
Clean and Build. And you can check here the location where the file will be get....
PROJECTNAME\target\classes\usedPictures\
--Open.png
Now the Example using the ICON:
button.setIcon(
new javax.swing.ImageIcon(
getClass().getClassLoader().getResource("usedPictures/Open.png")
)
);

Java: Text not wrapping in JFrame dialog

I'm using a JFrame to present a message box with some text and 2 buttons. How do I get the text to wrap automatically based on the size of the box?
Here's my current code:
dialogFrame = new JFrame();
JButton newButton = new JButton("New");
newButton.addActionListener(new newUploadAction());
JButton resumeButton = new JButton("Resume");
resumeButton.addActionListener(new resumeUploadAction());
//dialogFrame.setUndecorated(true);
JPanel addPanel = new JPanel();
JPanel addPanel2 = new JPanel();
addPanel.add(newButton);
addPanel.add(resumeButton);
String text = "<html><p>A previous control file exists for this file. ";
text += "Would you like to initiate a new transfer or resume the previous one?</p></html>";
JLabel testLabel = new JLabel(text);
//testLabel.setPreferredSize(new Dimension(1, 1));
addPanel2.add(testLabel);
Container content = dialogFrame.getContentPane();
//content.setBackground(Color.white);
content.setLayout(new BoxLayout(content, BoxLayout.PAGE_AXIS));
content.add(addPanel2);
content.add(addPanel);
dialogFrame.setSize(200,200);
dialogFrame.setVisible(true);
dialogFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
I read somewhere that calling
testLabel.setPreferredSize(new Dimension(1, 1));
would cause the wrapping behavior I want, but that just resulted in the text not showing up at all.
You could place the text in a JTextArea and call setLineWrap(true) and setWrapStyleWord(true) on the JTextArea. If you want it to look more JLabel-ish, then just change the color settings of the JTextArea to your liking.
EDIT 1
Also another thing that could work: consider having the holding JPanel use a BorderLayout:
JPanel addPanel2 = new JPanel(new BorderLayout()); //!! added BorderLayout
So that the JLabel added will fill the addPanel2.

Categories

Resources