I am trying to get plotted graphics to refresh/repopulate when the user chooses each of various options in a JComboBox. The examples I have found on the web all use JLabels, which may be fine for image files, but which do not work for paintComponent-generated custom graphics.
I tried to roll my own solution in the ~60 lines of code below. I am using a trivial example of a rectangle to be re-sized. If you compile and run the code below, you will see that it does not repaint when the user selects different options from the JComboBox. Also, I have deliberately not yet done anything with displayConstraints because I do not want to impose a solution if someone has a better approach. My goal is for the JComboBox to be displayed in its own row at the top, and for the plotted graphing to be done in a much larger second row underneath the first row. The second row will absorb all resizing changes, while the first row will remain more or less the same size when the JFrame is resized. By choosing different options from the JComboBox, the user will be able to make the plotted rectangle get smaller or larger relative to the current size of the JFrame.
Can anyone show me how to fix the code below so that it accomplishes my above-stated goals?
import java.awt.Graphics;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import javax.swing.JComboBox;
import javax.swing.JFrame;
public class ComboBox extends JFrame implements ItemListener {
final String[] sizes = { "10%", "20%", "33%" };
JComboBox combobox = new JComboBox(sizes);
int selectedIndex;
public ComboBox() {
setLayout(new GridBagLayout());
combobox.setSelectedIndex(-1);
combobox.addItemListener(this);
GridBagConstraints comboBoxConstraints = new GridBagConstraints();
comboBoxConstraints.gridx = 0;
comboBoxConstraints.gridy = 0;
comboBoxConstraints.gridwidth = 1;
comboBoxConstraints.gridheight = 1;
comboBoxConstraints.fill = GridBagConstraints.NONE;
add(combobox,comboBoxConstraints);//This should be placed at top, in middle.
GridBagConstraints displayConstraints = new GridBagConstraints();
displayConstraints.gridx = 0;
displayConstraints.gridy = 1;
displayConstraints.gridwidth = 1;
displayConstraints.gridheight = 1;
displayConstraints.fill = GridBagConstraints.BOTH;
//I am aware that nothing is done with displayConstraints.
//I just want to indicate that the rectangle should go below the combobox,
//and that the rectangle should resize while the combobox should not.
//Other suggested approaches are welcome.
setSize(300, 300);
setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
setLocationRelativeTo(null);
setVisible(true);
}
public static void main(String[] args) {new ComboBox();}
public void itemStateChanged(ItemEvent e) {
if (e.getStateChange() == ItemEvent.SELECTED) {
JComboBox combo = (JComboBox) e.getSource();
selectedIndex = combo.getSelectedIndex();
System.out.println("selectedIndex is: "+selectedIndex);
repaint();
}
}
protected void paintComponent(Graphics g){
int scaleFactor = 1;
if(selectedIndex==0){scaleFactor = 10;}
if(selectedIndex==1){scaleFactor = 5;}
if(selectedIndex==2){scaleFactor = 3;}
if(selectedIndex!=-1){
int xStart = (getWidth()/2)-(getWidth()/scaleFactor);
int yStart = (getHeight()/2)-(getHeight()/scaleFactor);
g.drawRect(xStart, yStart, (getWidth()/scaleFactor), (getHeight()/scaleFactor));
}
}
}
You shouldn’t draw directly in a JFrame and even if you did, JFrame has no paintComponent method. Use an
@Overrideannotation to see for yourself. Instead, draw in a JPanel or JComponent, do the drawing in paintComponent, and make sure that you’re correctly overriding the method with the override annotation.For example: