1

I want to set my JToggleButton in a disabled state, i.e. so that the user can't toggle it by clicking. I tried btn.setEnabled(false); but it grays out my icon and I don't want that. Is there any other method which doesn't gray out the icon, but doesn't let the user toggle the button?

dryairship
  • 6,022
  • 4
  • 28
  • 54
user3484582
  • 557
  • 1
  • 6
  • 22
  • 2
    What is the point of this? The user will get frustrated if they keep clicking the button and nothing happens. That is why you make the button disable so there is a visible clue that the button can't be clicked. – camickr Dec 27 '15 at 23:03
  • @camickr I'm making a dice game where the user on some occasions has the option to toggle and reroll some of the dice. However, on my initial roll I don't want the user toggling the dice. I'm using question mark icons so the user knows he isn't supposed to click on the dice but only on the roll button. I don't like the dice being greyed out just from an aesthetic point of view, want to have my icons visible. I hope I'm clear. – user3484582 Dec 27 '15 at 23:14
  • 1
    `I don't like the dice being greyed out just from an aesthetic point of view` - Not a good reason. This is how the UI should be used. Have you ever had to download some software but you can't click on the "Download Button" until you click on the check box indicating you "Accept" the licence? This is the same thing. If you can't click on the button then it should not be there or it should be disabled. Companies spend millions of dollars to have consistent UI's that work for all applications. Follow the industry standards!!! – camickr Dec 27 '15 at 23:21
  • So what you need is rather custom controll. – Antoniossss Dec 27 '15 at 23:29
  • I still agree with camickr, but, you would separate the `JToggleButton` from the icon (maybe using a `JLabel`), so you can disable the `JToggleButton` without effecting the icon – MadProgrammer Dec 27 '15 at 23:30
  • @camickr In this case though I'm not using a button provided by the UI, but my own custom icon to be displayed. I believe it's reasonable to be asking something like that. Thanks for the input. So from what you guys are saying it is not worth it to do so. I just wanted to know if there is a dedicated method for this. Thanks! – user3484582 Dec 27 '15 at 23:33

3 Answers3

2

You could disassociate the icon from the button and use appropriate layout constraints to provide a visual clue to the relationship instead, using a JLabel to display the icon instead, for example...

Separate button and icon

import java.awt.EventQueue;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JToggleButton;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

public class Test {

    public static void main(String[] args) {
        new Test();
    }

    public Test() {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    try {
                        UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                    } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
                        ex.printStackTrace();
                    }

                    JFrame frame = new JFrame("Testing");
                    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                    frame.add(new TestPane());
                    frame.pack();
                    frame.setLocationRelativeTo(null);
                    frame.setVisible(true);
                } catch (IOException ex) {
                    ex.printStackTrace();
                }
            }
        });
    }

    public class TestPane extends JPanel {

        public TestPane() throws IOException {
            setLayout(new GridBagLayout());
            GridBagConstraints gbc = new GridBagConstraints();
            gbc.gridwidth = GridBagConstraints.REMAINDER;

            BufferedImage img = ImageIO.read(new File("/Volumes/Disk02/Dropbox/MegaTokyo/thumnails/2.jpg"));
            JLabel label = new JLabel(new ImageIcon(img));
            add(label, gbc);

            JToggleButton btn = new JToggleButton("Click");
            btn.setEnabled(false);
            add(btn, gbc);
        }

    }

}

Personally, I'm thinking a JCheckBox might be more appropriate for this style

MadProgrammer
  • 343,457
  • 22
  • 230
  • 366
2

but my own custom icon to be displayed.

You can also specify a "disabled icon" to be used by the toggle button. It could be the same Icon you use by default, or a slightly different icon.

When you specify your own Icon you don't get the greyed out effect.

camickr
  • 321,443
  • 19
  • 166
  • 288
  • Awesome, exactly what I was looking for! – user3484582 Dec 27 '15 at 23:51
  • Btw, for some reason `setDisabledIcon()` didn't work. A blank button icon appeared everytime, which was disabled but not greyd out. The parameters and syntax should be the same as `setIcon()`, right? Any ideas what it might be? – user3484582 Dec 28 '15 at 14:03
  • 1
    @user3484582, worked fine for me. Post a proper [SSCCE](http://sscce.org/) that demonstrates the problem. That is create a JFrame with a single disabled JToggleButton that displays a disable Icon. It will take 10-15 lines of code to test the concept. – camickr Dec 28 '15 at 15:48
  • you are right, it does work for me too if I test it in a new frame. I'll have to check my code again I guess, I must be doing something wrong. Thanks! – user3484582 Dec 28 '15 at 16:11
0

To allow the JToggleButton to be clicked only once.

  1. If there are N JToggleButtons, declare N number of int class variables equal to 0.

    (int jtbIntValue1=0, jtbIntValue1=0 ,...jtbIntValueN = 0)

  2. Before allowing the user to press the JToggle button check if(jtbIntValueN != 0)

  3. when a JToggle button is clicked update the corresponding JToggleButton int value equal to 1. (From jtbIntValueN = 0 to jtbIntValueN = 1).

         public class Development1 
         {
           int b1 = 0 ; // For one Button
         public Development1() 
         {
         ...........
         ...........
         JToggleButton jtb1 = new JToggleButton();
         jtb1.addMouseListener(new MouseAdapter() {
        @Override
        public void mouseClicked(MouseEvent e1) 
        {
            if(b1!=1)
            {
            if(SwingUtilities.isRightMouseButton(e1) && e1.getClickCount() == 1)
            {
                jtb1.setIcon(icRight); // Or do whatever you want to
            }
            else if(SwingUtilities.isLeftMouseButton(e1) && e1.getClickCount() == 1)
            {
                jtb1.setIcon(icLeft);                   
            }
            }
            b1 = 1;
        }
    });
    
User9211
  • 194
  • 1
  • 2
  • 17