In this GUI there are four images which place on array of DefaultComboBoxModel
and label which helps to display the image. The default image is 1.png
which appear on the frame. But when I select the other image from the combo list then it doesn't appear.
Code
public class Combo extends JFrame {
public Combo() {
super("Combo 2 GUI");
setSize(400, 300);
MethodG();
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLocationRelativeTo(null);
setVisible(true);
}
private void MethodG() {
JLabel l1;
JComboBox box;
JPanel p;
Container pane = getContentPane();
p = new JPanel();
box = new JComboBox();
box.setModel(new DefaultComboBoxModel(new String[] { "1.png", "2.png", "3.png", "4.png" }));
box.setMaximumRowCount(3);
String boxoption = (String) box.getSelectedItem();
Icon[] icons = { new ImageIcon(getClass().getResource(boxoption)) };
// Default display
l1 = new JLabel(icons[0]);
box.addItemListener(new ItemListener() {
@Override
public void itemStateChanged(ItemEvent e) {
if (e.getStateChange() == ItemEvent.SELECTED) {
l1.setIcon(icons[box.getSelectedIndex()]);
}
}
});
pane.add(p);
p.add(box);
p.add(l1);
}
}
Main method
public class Main {
public static void main(String[] args) {
Combo obj = new Combo();
}
}
And the second problem what does these two lines do and how to write in if syntax.
JComboBox schemaBox;
String schema = (schemaBox.isEnabled() ? schemaBox.getSelectedItem().toString() : null);
String selectTable = (schema == null ? "" : schema + ".") + tableName;