KMKZ509
KMKZ509

Reputation: 1

Changing the icon of a JButton based on a JRadioButton selection

I have a JRadioButton with an ActionListener but cannot figure out how to trigger an icon change for a JButton in a different panel when it's clicked. The code for both is listed below. The image needs to switch from the left button to the right when the correct radio button is selected.

package gui;

public class ExampleGUI extends JFrame {

private static final long serialVersionUID = 1L;
private JPanel contentPane;
ImageIcon icon = new ImageIcon(ExampleGUI.class
        .getResource("/gui/schlange1.gif"));

/**
 * Launch the application.
 */
public static void main(String[] args) {
    EventQueue.invokeLater(new Runnable() {
        public void run() {
            try {
                ExampleGUI frame = new ExampleGUI();
                frame.setVisible(true);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    });
}

/**
 * Create the frame.
 */
public ExampleGUI() {
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setBounds(100, 100, 450, 300);
    contentPane = new JPanel();
    contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
    contentPane.setLayout(new BorderLayout(0, 5));
    setContentPane(contentPane);

    JLabel lblExampleGui = new JLabel("Example GUI");
    lblExampleGui.setFont(new Font("Tahoma", Font.PLAIN, 18));
    lblExampleGui.setHorizontalAlignment(SwingConstants.CENTER);
    contentPane.add(lblExampleGui, BorderLayout.NORTH);

    JPanel radioButtonPanel = new JPanel();
    contentPane.add(radioButtonPanel, BorderLayout.SOUTH);

    JPanel imagePanelBoxes = mainImagePanel();
    contentPane.add(imagePanelBoxes, BorderLayout.CENTER);

    JButton leftImage = leftClickImage();
    imagePanelBoxes.add(leftImage);

    JButton rightImage = rightClickImage();
    imagePanelBoxes.add(rightImage);

    JRadioButton leftRadioButton = leftRadioButton();
    radioButtonPanel.add(leftRadioButton);

    JRadioButton rightRadioButton = rightRadioButton();
    radioButtonPanel.add(rightRadioButton);

    ButtonGroup group = new ButtonGroup();
    group.add(leftRadioButton);
    group.add(rightRadioButton);
}

private JPanel mainImagePanel() {
    JPanel imagesPanel = new JPanel();
    imagesPanel.setBorder(new EmptyBorder(0, 5, 0, 5));
    imagesPanel.setLayout(new GridLayout(0, 2, 10, 0));
    return imagesPanel;
}

private JRadioButton leftRadioButton() {
    final JRadioButton leftRadioButton = new JRadioButton("LEFT");
    leftRadioButton.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            changeIcon(leftClickImage(), icon);
        }
    });
    leftRadioButton.setSelected(true);
    return leftRadioButton;
}

private JRadioButton rightRadioButton() {
    final JRadioButton rightRadioButton = new JRadioButton("RIGHT");
    rightRadioButton.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            changeIcon(rightClickImage(), icon);
        }
    });
    rightRadioButton.isSelected();
    return rightRadioButton;
}

private JButton leftClickImage() {
    JButton leftImage = new JButton("");
    leftImage.setIcon(new ImageIcon(ExampleGUI.class
            .getResource("/gui/schlange1.gif")));
    leftImage.setBackground(Color.BLACK);
    return leftImage;
}

private JButton rightClickImage() {
    final JButton rightImage = new JButton("");
    rightImage.setBackground(Color.BLACK);
    return rightImage;
}

public void changeIcon(JButton jb, ImageIcon icon) {
    jb.setIcon(icon);
}

}

Upvotes: 0

Views: 1514

Answers (4)

e-oj
e-oj

Reputation: 207

ImageIcon icon = new ImageIcon("img src"); 

rightRadiobutton.addActionListener(new ActionListener(ActionEvent ae){
   changeIcon(rightButton, icon); 
});

public void changeIcon(JButon jb, ImageIcon icon){
    jb.setIcon(icon);
}

You do the same for leftRadioButton. Also you dont need separate methods to create components; you can just assign and use them like:

JRadioButton rightJrb = new JRadioButton("I am a radio button");
rightJrb.addActionListener();

Upvotes: 0

meister_reineke
meister_reineke

Reputation: 364

You could provide the Button, who is to be changed as an argument of your JRadioButton-creator, but that looks quite unreadable and ill-designed

private JRadioButton leftRadioButton(JButton affectedButton) {
    JRadioButton leftRadioButton = new JRadioButton("LEFT");
    leftRadioButton.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent arg0) {
            if (leftRadioButton.isSelected())
                affectedButton=rightRadioButton();
            else 
                affectedButton=leftRadioButton();
        }
    });
    leftRadioButton.setSelected(true);
    return leftRadioButton;
}

I would rather use not an in-line deffinition of your actionListener but, implement it in your frame (or whatelse class you use) to get access to the Buttons and Labels etc used in that class. It gets quite more readable, if you dont have too many items to listen to.

public class buttonchanger extends JFrame implements ActionListener{
    JPanel radioButtonPanel;
    JPanel imagePanelBoxes;
    JButton leftImage;
    JButton rightImage;
    JRadioButton leftRadioButton;
    JRadioButton rightRadioButton;

    public initGUI() {
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setBounds(100, 100, 450, 300);
        contentPane = new JPanel();
        contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
        contentPane.setLayout(new BorderLayout(0, 5));
        setContentPane(contentPane);

        JLabel lblExampleGui = new JLabel("Example GUI");
        lblExampleGui.setFont(new Font("Tahoma", Font.PLAIN, 18));
        lblExampleGui.setHorizontalAlignment(SwingConstants.CENTER);
        contentPane.add(lblExampleGui, BorderLayout.NORTH);

        JPanel radioButtonPanel = new JPanel();
        contentPane.add(radioButtonPanel, BorderLayout.SOUTH);

        JPanel imagePanelBoxes = mainImagePanel();
        contentPane.add(imagePanelBoxes, BorderLayout.CENTER);

        JButton leftImage = leftClickImage();
        imagePanelBoxes.add(leftImage);

        JButton rightImage = rightClickImage();
        imagePanelBoxes.add(rightImage);

        JRadioButton leftRadioButton = leftRadioButton();
        leftRadioButton.addActionListener(this);
        radioButtonPanel.add(leftRadioButton);

        JRadioButton rightRadioButton = rightRadioButton();
        rightRadioButton.addActionListener(this);
        radioButtonPanel.add(rightRadioButton);

        ButtonGroup group = new ButtonGroup();
        group.add(leftRadioButton);
        group.add(rightRadioButton);
    }

    public void ActionListener(ActionEvent actE){
        Object obj=actE.getSource();
        if (obj==leftRadioButton){
            leftImage.setIcon(yourIcon);
            //or do whatever you intend to do
        }
    }

}

hope this is more of the solution you were looking for. I still dont know what button should change to what state after the radioButton-Event

Upvotes: 0

e-oj
e-oj

Reputation: 207

public class SwitchButton {
public static void main(String [] args){
    SwitchButton sb = new SwitchButton();
}

JFrame jfButtons =  new JFrame();
JPanel jpButtons =  new JPanel();
JRadioButton jrb = new JRadioButton("if you click me");
JButton jb = new JButton("I'll change");

public SwitchButton(){
    jrb.addActionListener(new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent e) {
            changeColor(jb, Color.blue);
        }
    });
    jpButtons.add(jrb);
    jpButtons.add(jb);
    jfButtons.add(jpButtons);
    jfButtons.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
    jfButtons.setVisible(true);
    jfButtons.pack();
}

public void changeColor(JButton jbtn, Color color){
    jbtn.setBackground(color);
}

}

This does basically what you're trying to do. You'll just have to change changeColor() to changeIcon()

Upvotes: 1

Antoniossss
Antoniossss

Reputation: 32507

Just simply

yourJButton.setIcon(yourIconToSet);

and this should be ofc called from action listener on radio button

leftRadioButton.addActionListener(new ActionListener() {
    public void actionPerformed(ActionEvent arg0) {
        // ? <- here invoke code to change your button's label
    }
});

Upvotes: 0

Related Questions