1

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); 

MenuGUI

1
  • You should make sure that the button is added to a visible container, and obviously that it isn't null (so initialize it) .. Commented Mar 30, 2016 at 12:12

2 Answers 2

1
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.

Sign up to request clarification or add additional context in comments.

Comments

1

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); 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.