Reputation: 11
I am trying to display an imageIcon I want the image to be displayed once you have selected an item from the orderList JList the image
DefaultListModel<MenuItem> orderList = new DefaultListModel<MenuItem>();
JList<MenuItem> listOrder = new JList<MenuItem> (orderList);
listOrder.addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent arg0) {
MenuItem selectedItem = listOrder.getSelectedValue();
ImageIcon icon = selectedItem.getItemImage();
AbstractButton imageLabel = null;
imageLabel.setIcon(icon);
}
});
listOrder.setFont(new Font("Arial", Font.PLAIN, 14));
listOrder.setBackground(Color.GRAY);
listOrder.setBounds(506, 67, 317, 375);
contentPane.add(listOrder);
Upvotes: 1
Views: 78
Reputation: 845
AbstractButton imageLabel = null;
imageLabel.setIcon(icon);
This is not valid, You can't set imageLabel as null and then setting icon for it. If Image label is the Square box below "Item Image", then it should be initialized at class level and you need to just change the icon using setIcon and re validate then button using revalidate() method.
Upvotes: 1
Reputation: 1176
This:
AbstractButton imageLabel = null;
imageLabel.setIcon(icon);
Is going to raise a NullPointerException. Initialize the variable imageLabel properly. For example:
AbstractButton imageLabel = new JButton(icon);
Upvotes: 1