My program currently adds an image to a List, which it then creates a Jbutton dynamically storing a thumbnail. Like found here. although the images are added as the user selects them.
The issue I am having is that I cannot get the image to change to the thumbnail that is selected as they are not linked to the images in the list. Is there a way I can store the index number of the image in the button, so when I click it I know which image to show in the List? Or is there another more intelligent way? Thanks.
//Create thumbnail
private void createThumbnail(ImpImage image){
Algorithms a = new Algorithms();
ImpImage thumb = new ImpImage();
//Create Thumbnail
thumb.setImg(a.shrinkImage(image.getImg(), 75, 75));
//Create ImageIcon
ImageIcon icon = new ImageIcon(thumb.getImg());
//Create JButton
JButton iconButton = new JButton(icon);
//Create ActionListener
iconButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
bottomBarLabel.setText("Clicked");
imagePanel.removeAll();
imagePanel.add(images.get(position)); //Needs Fixing
imagePanel.revalidate();
}
});
//Add to previewPanel
previewPanel.add(iconButton);
previewPanel.revalidate();
previewPanel.repaint();
}
The way I have implemented this is: 1) Open image, 2) Add image to List, 3)Create a thumbnail of image -> add to ImageIcon -> add to Jbutton -> add to component w/ actionListener. The issue is I don't store the buttons in a list so it doesn't know what number its respective image is in the List.
One method you can try is to set an action command on the button when it is created. This java tutorial demonstrates this concept is greater detail.
In your case, you would set it like so:
final JButton iconButton = new JButton(icon);
iconButton.setActionCommand("some_unique_identifying_information");
And you can retrieve it in the actionPerformed
callback like so:
@Override
public void actionPerformed(ActionEvent e) {
String actionCommand = iconButton.getActionCommand();
// ... rest of method
}
Note the use of final
on the iconButton. From the looks of your code, the button that fires the event is accessible via the action listener's closure. If you prefer, or cannot use the above method, you can access the button like so:
@Override
public void actionPerformed(ActionEvent e) {
JButton button = (JButton) e.getSource();
String actionCommand = button.getActionCommand();
// ... rest of method
}
Similar to Christian's suggestion in the comments, you can, if you wish, maintain a Map
or other data structure in which your unique action command is related to an image. Your map might look like Map<String, ImpImage>
(assuming ImpImage
is the class of your icon).
Alternatively, the action command may be the string representation of your index value. While it must be stored in String
form, once you retrieve it via getActionCommand()
, you can parse it back to a number using Integer.parseInt(String)
.
How about your extend the JButton class.
class MyButton extends JButton{
public int index;
MyButton(){super();}
MyButton(int index){super();this.index=index;}
}
When you need to get the index, cast it back to MyButton and get the index;
MyButton mybtn = (MyButton)this;
来源:https://stackoverflow.com/questions/21533356/is-there-a-way-to-store-data-in-a-jbutton