Refreshing JLabel - Java - java

I have some problems with JLabels and Frames..
I have the following code:
list.addListSelectionListener(
new ListSelectionListener() {
#Override
public void valueChanged(ListSelectionEvent e) {
//System.out.println(e.getFirstIndex());
String selectedFile = list.getSelectedValue().toString();
System.out.println("Selected file " + selectedFile);
JLabel label;
ImageIcon icon = new ImageIcon("C:\\Users\\danie\\Pictures\\" + selectedFile);
// frame.setSize(1047, 680);
label = new JLabel(icon);
//label.setSize(100,100);
frame.getContentPane().add(label, BorderLayout.CENTER);
frame.revalidate();
frame.repaint();
}
});
I want to refresh the label in the center of the borderlayout if a item in a JList is selected. But with this code, the old image is futher displayed and the new image is only drawed behind the existing image.
Could anyone help me? :)

First you are creating a new JLabel instance instead of working on the existing one. What you actually want to do is:
labe.setIcon(icon);
This will automatically refresh the element.

Suggestions:
Don't create a new JLabel, give it an ImageIcon and expect the existing JLabel to change. The two JLabels, the original one and the one created here, are two completely different objects, and changing the state of one (the icon it's displaying) will not magically change the state of the other.
Do make sure that the original JLabel has an instance variable in the class (not in your listener class), a field, refer to it, and then in your listener code, change the icon displayed in that JLabel by calling its setIcon(...) method
No need to call revalidate() or repaint() here as this should be done if you change components held within a container, such as if you removed the original JLabel from the JFrame's contentPane and swapped in a new one. Note that I do not recommend that you do this as it is over-complicating what should be a simple thing -- swapping icons.
To simplify things, I suggest that you read all your images in at program startup, create ImageIcons at that time, and then put them into an array or collection (such as an ArrayList<Icon> or a HashMap<String, Icon>), and then in your listener code, extract the correct icon, and put it into the existing JLabel.

You are not using the same JLabel it seems. You should alter the label you already have, not create a new one.

Related

JDialog layout issues

I'm using a JDialog to create a customized dialog box for my Java project. I'm having issues with the layout at the moment. It seems each JLabel I add to the dialog goes over the existing one. Do I need to add some sort of JPanel?
I also seem to have a issue with the size. I set it too 500x500 but why does it only goes as large as the text width?
JDialog processData = new JDialog(f1, "TItle goes here");
JLabel centretext = new JLabel("Look at me im centre!");
JLabel leftext = new JLabel("LOok at me im left");
JLabel righttext = new JLabel("LOok at me im right");
processData.setVisible(true);
processData.add(centretext);
processData.add(lefttext);
processData.add(rightext);
processData.toFront();
processData.setSize(500,500);
processData.setLocation(500,500);
processData.pack();
JDialog uses a BorderLayout by default, which means, it will only show a single component in any of the five available positions, all the others get ignored.
Consider using a different layout manager. See Laying Out Components Within a Container for more details

How to execute an action on a component with a button of an external object?

It was hard to word the title for this one, but here's the explanation:
I have a menu bar that I'm adding as an external object from my MenuBar.java that extends JMenuBar to my main program file APP.java that extends JFrame.
MenuBar and a JPanel (which is in my main program file, APP.java) are added to the JFrame. How do I make buttons from the MenuBar perform actions on the JPanel.
Here's how my JMenuItem objects look like right now in MenuBar.java:
item = new JMenuItem("New);
item.setMnemonic(KeyEvent.VK_N);
item.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_N,
ActionEvent.ALT_MASK));
item.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
JLabel block = new JLabel();
block.setPreferredSize(new Dimension(120, 160));
//***This is where I run into a problem... I want to add this JLabel to my JPanel in
// the main file, and I also want to revalidate/repaint the JPanel to take show
// the new JPanels as they're added.....
}
});
file.add(item);
I'm not sure if I need to extend my APP to implement ActionListener.... but then I am not sure what to do afterwards.
EDIT:
Well, I was able to perform the intended action by making my content panel public and static, thus making it available without instantiating the APP object. And then I was able to implement this code into my actionPerormed methods in ActionListeners:
APP.content.add(new Thumb());
APP.content.validate();
Thumb() method creates a new JLabel;
Hopefully this won't mess up my stuff later on down the line, being that my content panel is static now.
This is hard to answer.
I would use setAction(Action) (indirectly). One can make an Action as child of AbstractAction, and an Action can hold its text, an icon, mnemonic key and more.
One typical usage is a JTextPane that provides a Action[] getActios() and those actions might be added to the menu bar or a JToolBar.
Please look up some code samples.
I leave it at this half of an answer.
An intro.
It would depend on what actions you want to perform, but the overall solution is the same. You need to pass a reference of the object of the object you want to work with to the menu class.
If you can, its better to pass a model of interace, limiting your actions to only performing work you really want them to

Adding JButton dynamically to the JPanel not working with Netbeans

I created a JFrame Class with Netbeans 7.3 and added two panels from the palette.
I have added a button in the first panel on the click of which I want to add a new button in the second panel(topoPane).
Below is the button click event that I have written for the same. But, the button is not getting added to the panel even when the event is getting called.
Please tell me what's wrong in it.
private void jButton1MouseClicked(java.awt.event.MouseEvent evt)
{
// TODO add your handling code here:
System.out.println("Creating the Button");
JButton but = new JButton();
but.setBackground(Color.red);
but.setText("New Button");
but.setBounds(500, 500, 500, 500);
topoPane.add(but);
topoPane.revalidate();
}
From your use of setBounds, it is obvious that you are using a null layout. Because of this you need to call repaint() as containers with no layout do not automatically repaint added components on revalidate.
Apart from the fact that calling repaint is good practice, layout managers can remove the need to make this call along with manage the sizing and positioning of components. This makes it a good reason to use a layout manager.

Adding to JPanel in application

When a particular button is clicked I want another set of buttons to be added to the Panel, however at the moment when I do this, I can add them as many times as I want, I need this to be only possible once. Would the best way to do this be set the adding of the buttons and fields in a while loop?
if(e.getSource() == selectScript){
while(scriptB < 1 ){
imageID = new JTextField("INT");
imageDescription = new JTextField("imgDescription");
imagePath = new JTextField("imagePath");
manageImageTab.add(imageID);
manageImageTab.add(imageDescription);
manageImageTab.add(imagePath);
insertImage = new JButton("Add an Image");
insertImage.addActionListener(new dbaccess());
manageImageTab.add(insertImage);
manageImageTab.revalidate();
validate();
scriptB++;
}
}
Perhaps rather than add and remove the JButtons, you could add the buttons once at the code start, just don't make them visible until you need them, or perhaps better place them all on a JPanel that is not visible and then made visible when desired. Just don't forget to call revalidate() and repaint() on the container that holds the buttons and their panel.
If I understand you correctly, I would use a flag alreadyAdded that starts out false, gets set to true after the controls have been added, then don't allow it to add after that.

Dynamically remove Component from JPanel

I am adding and deleting components dynamically in a JPanel.
Adding and deleting functionality works fine but when I delete the component it deletes the last component rather than the component to be deleted.
How can I solve this issue?
Interestingly enough I am coming across the same issue and I am surprised people are upvoting the other answer, as he is clearly asking about dynamically created Components, not components already created under a variable name which is obtainable, instead of anonymously created objects.
The answer is pretty simple. Use
getComponents() to iterate through an array of components added to the JPanel. Find the kind of component you want to remove, using instanceof for example. In my example, I remove any JCheckBoxes added to my JPanel.
Make sure to revalidate and repaint your panel, otherwise changes will not appear
Component is from java.awt.Component.
//Get the components in the panel
Component[] componentList = panelName.getComponents();
//Loop through the components
for(Component c : componentList){
//Find the components you want to remove
if(c instanceof JCheckBox){
//Remove it
clientPanel.remove(c);
}
}
//IMPORTANT
panelName.revalidate();
panelName.repaint();
Using the method Container.remove(Component), you can remove any component from the container. For example:
JPanel j = new JPanel();
JButton btn1 = new JButton();
JButton btn2 = new JButton();
j.add(btn1);
j.add(btn2);
j.remove(btn1);

Categories

Resources