Simple ActionListener within a 2D array of JButtons

后端 未结 4 1420
不思量自难忘°
不思量自难忘° 2020-12-04 00:56

Okay so I am making a 2d array of JToggleButtons. I got the action listener up and going, but I have no way to tell which button is which.

If I click one, all it ret

4条回答
  •  抹茶落季
    2020-12-04 01:49

    very simple way is add ClientProperty to the JComponent, add to your definition into loop e.g.

    buttons[i][j].putClientProperty("column", i);
    buttons[i][j].putClientProperty("row", j);
    buttons[i][j].addActionListener(new MyActionListener());
    

    rename e to the MyActionListener and change its contents

    public class MyActionListener implements ActionListener {
    
        @Override
        public void actionPerformed(ActionEvent e) {
            JToggleButton btn = (JToggleButton) e.getSource();
            System.out.println("clicked column " + btn.getClientProperty("column")
                    + ", row " + btn.getClientProperty("row"));
    }
    

    EDIT:

    for MinerCraft clone isn't required to implements ony of Listeners, there is only about Icon, find out that in this code (don't implement any of Listeners anf remove used ItemListener)

    enter image description here

    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    
    public class ButtonsIcon extends JFrame {
    
        private static final long serialVersionUID = 1L;
        private Icon errorIcon = UIManager.getIcon("OptionPane.errorIcon");
        private Icon infoIcon = UIManager.getIcon("OptionPane.informationIcon");
        private Icon warnIcon = UIManager.getIcon("OptionPane.warningIcon");
    
        public static void main(String[] args) {
            SwingUtilities.invokeLater(new Runnable() {
    
                @Override
                public void run() {
                    ButtonsIcon t = new ButtonsIcon();
                }
            });
        }
    
        public ButtonsIcon() {
            setLayout(new GridLayout(2, 2, 4, 4));
    
            JButton button = new JButton();
            button.setBorderPainted(false);
            button.setBorder(null);
            button.setFocusable(false);
            button.setMargin(new Insets(0, 0, 0, 0));
            button.setContentAreaFilled(false);
            button.setIcon((errorIcon));
            button.setRolloverIcon((infoIcon));
            button.setPressedIcon(warnIcon);
            button.setDisabledIcon(warnIcon);
            add(button);
    
            JButton button1 = new JButton();
            button1.setBorderPainted(false);
            button1.setBorder(null);
            button1.setFocusable(false);
            button1.setMargin(new Insets(0, 0, 0, 0));
            button1.setContentAreaFilled(false);
            button1.setIcon((errorIcon));
            button1.setRolloverIcon((infoIcon));
            button1.setPressedIcon(warnIcon);
            button1.setDisabledIcon(warnIcon);
            add(button1);
            button1.setEnabled(false);
    
            final JToggleButton toggleButton = new JToggleButton();
            toggleButton.setIcon((errorIcon));
            toggleButton.setRolloverIcon((infoIcon));
            toggleButton.setPressedIcon(warnIcon);
            toggleButton.setDisabledIcon(warnIcon);
            toggleButton.addItemListener(new ItemListener() {
    
                @Override
                public void itemStateChanged(ItemEvent e) {
                    if (toggleButton.isSelected()) {
                    } else {
                    }
                }
            });
            add(toggleButton);
    
            final JToggleButton toggleButton1 = new JToggleButton();
            toggleButton1.setIcon((errorIcon));
            toggleButton1.setRolloverIcon((infoIcon));
            toggleButton1.setPressedIcon(warnIcon);
            toggleButton1.setDisabledIcon(warnIcon);
            toggleButton1.addItemListener(new ItemListener() {
    
                @Override
                public void itemStateChanged(ItemEvent e) {
                    if (toggleButton1.isSelected()) {
                    } else {
                    }
                }
            });
            add(toggleButton1);
            toggleButton1.setEnabled(false);
    
            setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            pack();
            setVisible(true);
        }
    }
    

提交回复
热议问题