1

I've got an application that is using Swing for it's UI. I want a button that switch the type of communication that the app is using. I want to use a Toggle Button to identify the type of communication that's selected.

My problem is that I don't want the color of the button to change after it's been clicked. Currently the button looks like this... Non-Selected

https://i.stack.imgur.com/Ccdie.png

And then when clicked it looks like this...

Selected

https://i.stack.imgur.com/Q5yp4.png

The text changing is what I want, but I would prefer them to have the same color / style.

Here's my code for this...

    JToggleButton tglbtnCommunicationType = new JToggleButton("AlwaysOn");
    tglbtnCommunicationType.setFocusPainted(false);
    tglbtnCommunicationType.addChangeListener(new ChangeListener( ) {
        public void stateChanged(ChangeEvent tgl) {
            System.out.println("ChangeEvent!");
            if(tglbtnCommunicationType.isSelected()){
                tglbtnCommunicationType.setText("REST");
                tglbtnCommunicationType.setBackground(UIManager.getColor("Button.background"));
            }
            else
            {
                tglbtnCommunicationType.setText("AlwaysOn");
            };
        }
    });

My thought is that setting the background when it is selected to the standard background color would fix that, but it doesn't look like it. Any ideas?

Thanks!

Answer: I switched to a JButton instead, thanks for the help everyone!

JButton btnCommunicationType = new JButton("AlwaysOn");
    btnCommunicationType.setFocusPainted(false);
    btnCommunicationType.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            if(btnCommunicationType.getText().equals("AlwaysOn"))
            {
                btnCommunicationType.setText("REST");
                //TODO:  Insert Code for Switching Communication to REST here
            }
            else if(btnCommunicationType.getText().equals("REST")){
                btnCommunicationType.setText("AlwaysOn");
                //TODO: Insert Code for Switching Communication to AlwaysOne here
            }
        }
    });
    btnCommunicationType.setBounds(275, 199, 97, 25);
    thingWorxConnectionPanel.add(btnCommunicationType);
mcheli
  • 35
  • 1
  • 6
  • Possible duplicate of [Disable standard repainting of JToggleButton when It is selected](http://stackoverflow.com/questions/12227455/disable-standard-repainting-of-jtogglebutton-when-it-is-selected) – Arnaud Apr 25 '16 at 14:57
  • 2
    So how does the user know it is selected if there is no difference in painting? The UI has a different painting for a reason. Use the component the way it is designed to be used. For example take an editor like Work. As you move the caret of text with different attrbutes the button background changes so you know which attributes are part of the text. If you don't have a visual clue then why use the toggle button? `The text changing is what I want, but I would prefer them to have the same color / style.` If you want the same painting then use a JButton. – camickr Apr 25 '16 at 14:59
  • @camickr The user will know it is selected because the text on the button will change. – mcheli Apr 25 '16 at 15:02
  • @vishalgajera if that line is put into comment, there is the same result. (Same as the images i've provided above. – mcheli Apr 25 '16 at 15:03
  • 1
    So then just use a JButton like I suggested. Although I still think this is a terrible UI. Use the appropriate component for the job. – camickr Apr 25 '16 at 15:04
  • @user2711700 you can skip to use JToggleButton instead of it use JButton and use it's actionListener and just change it's text will help you as per your req. – Vishal Gajera Apr 25 '16 at 15:04
  • Use a normal button and toggle the selection flag by yourself. – Sergiy Medvynskyy Apr 25 '16 at 15:05

1 Answers1

1

you can do it by using JButton only instead of JToggleButton ,

JButton showButton = new JButton("AlwaysOn");
showButton.addActionListener(new ActionListener()
{
  public void actionPerformed(ActionEvent e)
  {
     String currentText = showButton.getText();
     if("AlwaysOn".equals(currentText)){
          showButton.setText("REST");
     }else{
          showButton.setText("AlwaysOn");
      }
  }
});
Vishal Gajera
  • 4,137
  • 5
  • 28
  • 55