javauser-interfacejtogglebutton

JToggle Button background color change


Here are my code where I implemented one button has named "start/stop"

Here is my code.....

//Buttons for start and stop 
btnStartStop = new JToggleButton("Start/Stop");
//btnStartStop.setEnabled(false);
btnStartStop.addActionListener(new ActionListener() {
    public void actionPerformed(ActionEvent e) {
        if (chk_start) {
              tout_textPane.setText("Stop Magnetic Levitation Project");
              btnStartStop.setText("start");
              btnStartStop.setBackground(new Color(170, 255, 0));
              btnStartStop.setForeground(Color.WHITE);
              btnStartStop.setSelected(false);
              CommandHandler(".plot");
              chk_start = false;
          } else {
                 tout_textPane.setText("Starting Magnetic Levitation Project");
                 btnStartStop.setText("stop");
                 btnStartStop.setBackground(Color.RED);
                 btnStartStop.setForeground(Color.WHITE);
                 btnStartStop.setSelected(true); 
                 chk_start = true;
              }
        CommandHandler(".isr");
        }
    });
btnStartStop.setBounds(27, 10, 210, 21);
panel_ctrl_i.add(btnStartStop);

Solution

  • I tried both command btnStartStop.setBorderPainted(false); btnStartStop.setOpaque(false); But still not working

    Works fine for me, and no, you don't seem to be able to get rid of the "selected" highlight, this is something controlled by the look and feel and that's a whole other mess

    import java.awt.Color;
    import java.awt.EventQueue;
    import java.awt.GradientPaint;
    import java.awt.Graphics;
    import java.awt.Graphics2D;
    import java.awt.GridBagLayout;
    import java.awt.Point;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import javax.swing.JFrame;
    import javax.swing.JPanel;
    import javax.swing.JToggleButton;
    
    public class Main {
        public static void main(String[] args) {
            new Main();
        }
    
        public Main() {
            EventQueue.invokeLater(new Runnable() {
                @Override
                public void run() {
                    JFrame frame = new JFrame();
                    frame.add(new TestPane());
                    frame.pack();
                    frame.setLocationRelativeTo(null);
                    frame.setVisible(true);
                }
            });
        }
    
        public class TestPane extends JPanel {
    
            public TestPane() {
                setLayout(new GridBagLayout());
                GradientButton btn = new GradientButton();
                btn.setOpaque(true);
                btn.setBorderPainted(false);
                btn.setBackground(Color.RED);
                add(btn);
    
                btn.addActionListener(new ActionListener() {
                    @Override
                    public void actionPerformed(ActionEvent e) {
                        if (btn.isSelected()) {
                            btn.setBackground(Color.GREEN);
                        } else {
                            btn.setBackground(Color.RED);
                        }
                    }
                });
            }
    
            @Override
            protected void paintComponent(Graphics g) {
                super.paintComponent(g);
                Graphics2D g2d = (Graphics2D) g.create();
                g2d.dispose();
            }
    
        }
    
        private static class GradientButton extends JToggleButton {
            private GradientButton() {
                super("Gradient Button");
                setContentAreaFilled(false);
                setFocusPainted(false); // used for demonstration
            }
    
            @Override
            protected void paintComponent(Graphics g) {
                final Graphics2D g2 = (Graphics2D) g.create();
                g2.setPaint(new GradientPaint(
                        new Point(0, 0),
                        Color.WHITE,
                        new Point(0, getHeight()),
                        Color.PINK.darker()));
                g2.fillRect(0, 0, getWidth(), getHeight());
                g2.dispose();
    
                super.paintComponent(g);
            }
        }
    }
    

    You should also do a search on jbutton change background as it's basically the same problem