2

I have a panel in which I put 2 radio buttons. I want to implement double click action on a radio button but it seems it doesn't work. In conclusion I want to make another component visible while double clicking one of the radio button, more precisely when clicking on its label.

Is there any explanation why the mouse listener works only if the radio buttons are disabled?

Maroun
  • 94,125
  • 30
  • 188
  • 241
Ramona
  • 167
  • 1
  • 6
  • 5
    Why do you want that? A radio button is not supposed to be double-clicked. Nobody will ever double-click on your radio button because all users expect a radio button to work as all the other radio buttons: you click it to select it, and that's all. – JB Nizet Apr 15 '13 at 21:43
  • 1
    true, still a valid question tho – 0x6C38 Apr 15 '13 at 21:44
  • 4
    I agree, its a terrible UI. However a MouseListener works fine, although there is no way to determine if the label or icon is clicked. Post your SSCCE demonstrating the problem. – camickr Apr 15 '13 at 21:49
  • The real situation is that I want to make the radio buttons to have editable "labels". For this I need to add two more components, 2 jtextfields which become visible when double clicking the radio button. – Ramona Apr 15 '13 at 21:55
  • 2
    @Ramona Consider then using RadioButton's without text and adding JLabel next to them with a mouse listener. – Guillaume Polet Apr 15 '13 at 21:57
  • @camickr you can find out where the click happened if you compute text bounds using `SwingUtilities.layoutCompoundLabel` (although it's not 100% reliable, it will mostly work). Solution described right above should be a lot easier and more reliable – Guillaume Polet Apr 15 '13 at 21:58
  • @Ramona Check out my updated answer with a double clickable and editable radio button label. – Guillaume Polet Apr 15 '13 at 22:14
  • @GuillaumePolet, thanks, haven't looked at the SwingUtilities class in a while. – camickr Apr 16 '13 at 04:26

1 Answers1

3

EDIT:

Check out this code where we separate the text of the radio button in a JLabel/JTextField

import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.FocusAdapter;
import java.awt.event.FocusEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

import javax.swing.BorderFactory;
import javax.swing.BoxLayout;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.JTextField;
import javax.swing.SwingUtilities;

public class TestRadioButton {

    private void initUI() {
        JFrame frame = new JFrame(TestRadioButton.class.getSimpleName());
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        final JPanel panel = new JPanel();
        panel.setLayout(new BoxLayout(panel, BoxLayout.LINE_AXIS));
        final JRadioButton button = new JRadioButton();
        final JLabel label = new JLabel("Double click me");
        final JTextField editionTF = new JTextField();
        editionTF.setOpaque(false);
        editionTF.addFocusListener(new FocusAdapter() {
            @Override
            public void focusLost(FocusEvent e) {
                stopEdition(panel, label, editionTF);
            }

        });
        editionTF.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                stopEdition(panel, label, editionTF);
            }
        });
        label.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                if (e.getClickCount() == 2) {
                    startEdition(panel, label, editionTF);
                }
            }
        });
        editionTF.setBorder(BorderFactory.createEmptyBorder());
        editionTF.setFont(label.getFont());
        editionTF.setVisible(false);
        panel.add(button);
        panel.add(label);
        panel.add(editionTF);
        frame.add(panel, BorderLayout.NORTH);
        frame.pack();
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);
    }

    protected void stopEdition(final JPanel panel, final JLabel label, final JTextField editionTF) {
        label.setText(editionTF.getText());
        label.setVisible(true);
        editionTF.setVisible(false);
    }

    protected void startEdition(final JPanel panel, final JLabel label, final JTextField editionTF) {
        editionTF.setText(label.getText());
        label.setVisible(false);
        editionTF.setVisible(true);
    }

    public static void main(String[] args) {
        final TestRadioButton test = new TestRadioButton();
        SwingUtilities.invokeLater(new Runnable() {

            @Override
            public void run() {
                test.initUI();
            }
        });
    }
}

Not really an answer, but comments are too small for this. This works just fine for me with following code. There must be some other problem that you don't show us. Consider posting an SSCCE

import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JRadioButton;
import javax.swing.SwingUtilities;

public class TestRadioButton {

    private void initUI() {
        JFrame frame = new JFrame(TestRadioButton.class.getSimpleName());
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        final JRadioButton button = new JRadioButton("Double click me");
        button.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                if (e.getClickCount() == 2) {
                    SwingUtilities.invokeLater(new Runnable() {

                            @Override
                            public void run() {
                                 JOptionPane.showMessageDialog(button, "Radio button has been double clicked");
                            }
                    };
                }
            }
        });

        frame.add(button);
        frame.setSize(400, 400);
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);
    }

    public static void main(String[] args) {
        final TestRadioButton test = new TestRadioButton();
        SwingUtilities.invokeLater(new Runnable() {

            @Override
            public void run() {
                test.initUI();
            }
        });
    }
}
Guillaume Polet
  • 47,259
  • 4
  • 83
  • 117
  • [JOptionPane should be wrapped in invokeLater](http://stackoverflow.com/questions/8282488/why-does-setselected-on-jcheckbox-lose-effect), works or doesn't depends of Java version – mKorbel Apr 16 '13 at 12:18
  • @mKorbel indeed it is probably more appropriate to do so although in this case we don't fall exactly into the same situation since we don't care about the radio button state. – Guillaume Polet Apr 16 '13 at 12:36