0

I'm super confused with what exactly models are supposed to do in java swing. At present I'm basically trying to create a model for JButton to detect if it isPressed(); My essential goal of the model is to do something like this:

if(myButton.isPressed() ) {
    myButton.setBackground(Color.RED);
}
else{//when any other button is pressed?
    myButton.setBackground(Color.WHITE);
}

At present my code is something like this:

    numberButton = new JButton("Num");
    numberButton.setBounds(20,40,80,30);
    numberButton.addChangeListener(new ChangeListener() {
        public void stateChanged (ChangeEvent e){
           if (model.isPressed() ){
              doColorChange(model);
           }
        }
    });

I understand that this is totally wrong, but I have no idea where, and I haven't found a tutorial that really explains what I'm doing wrong or why I need a model for this at all.

Please help me restore my sanity! Thanks a lot!

mKorbel
  • 109,525
  • 20
  • 134
  • 319
HunderingThooves
  • 962
  • 8
  • 20
  • 33
  • What if you simply get the button's model via `getModel()` and add the ChangeListener to the model? What is this `doColorChange(...)` method you speak of? – Hovercraft Full Of Eels Feb 08 '12 at 23:38
  • The DoColorChange method is totally empty because I can't think of a way for it to take a button Argument from inside of it's own model... Which is probably the totally wrong way to go about it anyways. – HunderingThooves Feb 08 '12 at 23:42
  • For better help sooner, post an [SSCCE](http://sscce.org/). – Andrew Thompson Feb 09 '12 at 05:31
  • afaics, the only thingy that's completely wrong is the call to setBounds :-) Listening to the model technically is just fine, though the LAF might have its own ideas about state-related visuals. Beware: all non-standard "decorations" might confuse your users – kleopatra Feb 09 '12 at 11:27

4 Answers4

4

Use radio buttons in a button group.

RedAndWhite

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

import java.net.URL;
import javax.imageio.ImageIO;

class RedAndWhite {

    public static Image getColoredImage(Color color, int size) {
        BufferedImage bi = new BufferedImage(
            size,
            size,
            BufferedImage.TYPE_INT_RGB);
        Graphics g = bi.getGraphics();
        g.setColor(color);
        g.fillRect(0,0,size,size);

        g.dispose();
        return bi;
    }

    public static void main(String[] args) throws Exception {
        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                Image red = getColoredImage(Color.RED, 32);
                Image white = getColoredImage(Color.WHITE, 32);
                JPanel p = new JPanel(new GridLayout(1,0,5,5));

                ButtonGroup bg = new ButtonGroup();
                for (int ii=0; ii<6; ii++) {
                    JRadioButton b = new JRadioButton();
                    b.setSelectedIcon(new ImageIcon(red));
                    b.setIcon(new ImageIcon(white));
                    b.setContentAreaFilled(true);
                    bg.add(b);
                    p.add(b);
                }

                JOptionPane.showMessageDialog(null, p);
            }
        });
    }
}
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
2

I think what you want is register an ActionListener or Action with the button in order to handle button presses, right?

Edit: rereading your answer, it seems you want to highlight the button that is being pressed, right? In that case, try to use your own button ui (subclass the one of the look and feel you're using). BasicButtonUI has a method protected void paintButtonPressed(Graphics g, AbstractButton b) that you could override to apply the highlight when the button is being pressed.

Thomas
  • 87,414
  • 12
  • 119
  • 157
1

there are follows ways how to make JButton nicer

1) most nicest way is painting only Borders from ButtonModel on Mouse events

2) override whole BasicButtonUI with all rellevant Look and Feels

3) put there Icons and override all possible methods implemented in JButton API

4) best at all would be implement Custom Look and Feel

5) create Custom JComponent based on JLabel/JComponent

Community
  • 1
  • 1
mKorbel
  • 109,525
  • 20
  • 134
  • 319
0

I always took it that the ButtonModel is intended (mostly) for use by the UI class to render the button in its pressed/not-pressed/armed/selected/... state and track changes to that state.

If you simply want to paint the button red (that is waht you want?) while it is pressed your solution seems fine to me.

If you have a Toggle Button (that remains pressed after it has been clicked to indicate a "selected" state, you might want to use an ItemListener and check for

ItemEvent.ITEM_STATE_CHANGED == ItemEvent.SELECTED // paint red
ItemEvent.ITEM_STATE_CHANGED == ItemEvent.DESELECTED // paint white

If you want to execute application logic when the button is clicked, use an ActionListener.

alex
  • 1,229
  • 7
  • 6