tags:

views:

37

answers:

1

Ok, So I make a JToggleButton:

JToggleButton button = new JToggleButton(new ImageIcon(features[i].getImage())) {

    private static final long serialVersionUID = 1L;

    @Override
    public void paint(Graphics g) {
        super.paint(g);
        if (isSelected()) {
            g.setColor(Color.RED);
            g.drawRect(0, 0, getSize().width - 1, getSize().height - 1);
        }
    }
};
try {
    if (bodyButton.isSelected()) {
        if (equals(features[i].getImage(), avatar.getBody().getImage())) {
            button.setSelected(true);
        }
    } else if (eyesButton.isSelected()) {
        if (equals(features[i].getImage(), avatar.getEyes().getImage())) {
            button.setSelected(true);
        }
    } else if (glassesButton.isSelected()) {
        if (equals(features[i].getImage(), avatar.getGlasses().getImage())) {
            button.setSelected(true);
        }
    } else if (hairButton.isSelected()) {
        if (equals(features[i].getImage(), avatar.getHair().getImage())) {
            button.setSelected(true);
        }
    } else if (pantsButton.isSelected()) {
        if (equals(features[i].getImage(), avatar.getPants().getImage())) {
            button.setSelected(true);
        }
    } else if (shirtButton.isSelected()) {
        if (equals(features[i].getImage(), avatar.getShirt().getImage())) {
            button.setSelected(true);
        }
    } else if (shoesButton.isSelected()) {
        if (equals(features[i].getImage(), avatar.getShoes().getImage())) {
            button.setSelected(true);
        }
    }
} catch (Exception e) {}

But I am having problems with it not painting. I setting selected true right after its institated as you can see, but its not painting the button SOMETIMES, Other times it work GREAT! Iknow that it is being set to true because I added a after it.

System.out.println(button.isSelected() + " " +i);
+4  A: 

I might override paintComponent() in JToggleButton, or perhaps just use setIcon().

Addendum: Be sure you're running on the EDT and overriding paintComponent() correctly. The following short, complete, compilable example (sscce) works reliably for me. As @camickr suggested, creating an sscce may help isolate a problem you encounter.

import java.awt.*;
import javax.swing.*;

public class NewMain extends JPanel {

    public NewMain() {
        super(true);
        JToggleButton button = new JToggleButton(new ImageIcon("test.gif")) {

            @Override
            protected void paintComponent(Graphics g) {
                super.paintComponent(g);
                g.drawRect(0, 0, getSize().width - 1, getSize().height - 1);
            }
        };
        this.add(button);
    }

    private static void create() {
        JFrame f = new JFrame();
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        f.setLayout(new GridLayout(2, 2));
        for (int i = 0; i < 4; i++) {
            f.add(new NewMain());
        }
        f.pack();
        f.setVisible(true);
    }

    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {

            @Override
            public void run() {
                create();
            }
        });
    }
}
trashgod
Tryed Paint Component, No Luck, and SetIcon is being used already
Cody
Every-things running from action events from the EDT, so I have no idea.
Cody
Which is why YOU are supposed to create the SSCCE. We can't guess what exactly you are doing to cause the problem.
camickr