Kemosabe
Kemosabe

Reputation: 321

Java JComboBox repaint error

So I am working with JComboBox in my new program, but for some reason every time something is selected it repaints. I do not want it to repaint the entire program when something is selected, just the boxes specified. See the code below. The first section here is from the class that contains the panel. First here are the declarations for the JComboBox's and the Labels they edit once selected:

private JComboBox crewview = new JComboBox(SpaceGame.stuffselector);
private JComboBox resourceview = new JComboBox(SpaceGame.stuffselector1);
private JLabel crewmember, crewmember1, crewmember2;
private JLabel resourcev, resourcev1, resourcev2;

where stuff selector is an array of number digits 1 to 10

My paint method containts these, as it does not work unless I have these here:

    resourceview.setLocation(400,80);
    resourcev.setLocation(455,85);
    resourcev1.setLocation(455,95);
    resourcev2.setLocation(455,105);

    crewview.setLocation(400, 20);
    crewmember.setLocation(455,25);
    crewmember1.setLocation(455,35);
    crewmember2.setLocation(455,45);

And here are my action listeners:

    private  class crewviewListener implements ActionListener{
    public void actionPerformed(ActionEvent e){
        lifeForm temp = SpaceGame.playership.getCrewat(crewview.getSelectedIndex());
        crewmember.setText("Name: " + temp.getName()); //set the text to the crew member
        crewmember1.setText("Race: " + temp.getRace()); 
        crewmember2.setText("Worth: " + temp.getWorth());
    }
}
private class resourceviewListener implements ActionListener{
    public void actionPerformed(ActionEvent e1){
        Resource temp1 = SpaceGame.playership.getResourceat(resourceview.getSelectedIndex());
        resourcev.setText("Name: " + temp1.getName()); //set the text to the crew member
        resourcev1.setText("Worth: " + temp1.getWorth()); 
        resourcev2.setText("Amount: " + temp1.getAmount());
    }
}

So as you may tell be reading the code, I am trying to get it to only update the JLables when stuff in the box is selected, and not repaint everything.

Upvotes: 0

Views: 105

Answers (1)

camickr
camickr

Reputation: 324098

JLabels are non-opaque by default. This means that whenever you change the text in the label, the background of the label must also be repainted. Therefore the panel that contains the label will also be repainted.

Swing will usually determine a clipped area of the panel to be repainted. That is the rectangular area that contains the 3 labels will be repainted, not the entire panel.

Post a SSCCE that demonstrates the problem if you need more help.

Upvotes: 1

Related Questions