简体   繁体   中英

Swing: grayable multi-line JCheckBox?

I want to have a checkbox with multi-line text and I want to disable it sometimes.

  • Simple JCheckBox works fine and gets disabled, but it's not multiline.
  • Putting <html>line1<br>line2</html> provides correct size and layout, but when I disable the control, only the checkbox itself is grayed, the text remains black.
  • I know I can change the HTML text color to gray, but this will not work for the "Classic Windows" look-and-feel where disabled text should rendered as "sunken". Or, actually, it will work, but the appearance will differ from other disabled controls nearby, which is not good.
  • I can create a simple JCheckBox containing the first line of text and a JLabel with the second line and disable them simultaneously, but clicking the second line (the JLabel) doesn't activate the checkbox, and clicking the checkbox displays the keyboard focus only around the first line which confuses the user.

Is it possible to have a checkbox and its label as separate controls and have some kind of link between them, as in HTML? Probably I would be able to cook something from this.

Is it possible to subclass JButton and override something there, for example, to change the way the focus rectangle is drawn? The rectangle is drawn by com.sun.java.swing.plaf.windows.WindowsButtonUI but I'm kinda afraid of subclassing that class because it's too deep in the standard library and my application may break with a new JRE.

EDIT 04.02.2015 : The above applies to Java 1.6. In Java 1.7 and higher, disabling a multi-line checkbox changes its appearance, but it still looks not the same as a disabled single-line checkbox; in particular, on Classic Windows theme the text doesn't become sunken.


(source: keep4u.ru )

I might be missing something, but I just disable the JCheckBox and the JLabel.

Using Java 1.7 and Windows Vista.

启用复选框禁用复选框

Here's the code

package com.ggl.testing;

import java.awt.BorderLayout;

import javax.swing.JCheckBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JToggleButton;
import javax.swing.SwingUtilities;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;

public class CheckBoxTest implements Runnable {

    private JCheckBox checkBox;

    private JLabel multiLineLabel;

    private JFrame frame;

    @Override
    public void run() {
        frame = new JFrame("Check Box Test");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        JPanel mainPanel = new JPanel();
        mainPanel.setLayout(new BorderLayout());

        JPanel checkBoxPanel = new JPanel();

        checkBox = new JCheckBox();
        checkBoxPanel.add(checkBox);

        String s = "<html>When in the course of human events it becomes"
                + "<br>necessary for one people to dissolve the political"
                + "<br>bands which have connected them with another and to"
                + "<br>assume among the powers of the earth, the separate"
                + "<br>and equal station to which the Laws of Nature and"
                + "<br>of Nature's God entitle them, a decent respect to the"
                + "<br>opinions of mankind requires that they should declare"
                + "<br>the causes which impel them to the separation.";
        multiLineLabel = new JLabel(s);
        multiLineLabel.setLabelFor(checkBox);
        checkBoxPanel.add(multiLineLabel);

        mainPanel.add(checkBoxPanel, BorderLayout.CENTER);

        JPanel toggleButtonPanel = new JPanel();

        JToggleButton toggleButton = new JToggleButton("Disable Checkbox");
        toggleButton.addChangeListener(new ChangeListener() {
            @Override
            public void stateChanged(ChangeEvent event) {
                JToggleButton toggleButton = (JToggleButton) event.getSource();
                if (toggleButton.isSelected()) {
                    checkBox.setEnabled(false);
                    multiLineLabel.setEnabled(false);
                } else {
                    checkBox.setEnabled(true);
                    multiLineLabel.setEnabled(true);
                }
            }
        });
        toggleButtonPanel.add(toggleButton);

        mainPanel.add(toggleButtonPanel, BorderLayout.SOUTH);

        frame.add(mainPanel);

        frame.pack();
        frame.setLocationByPlatform(true);
        frame.setVisible(true);
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new CheckBoxTest());
    }

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM