Search code examples
javaswingawtlayout-managerjcheckbox

Display fullname of JCheckBox item


I had design a JFrame with some components. In that JCheckBox component I had given a name but the full name is not displayed when I run the application. I want display the given name.

Code

 public class FindDemo extends JFrame
 {
 Label l1;
 JCheckBox matchCase,MatchWholeWords;
 TextField tf;
 JButton find_next,cancel;
 public FindDemo()
 {
    l1 = new Label("Find What: ");
    matchCase=new JCheckBox();
    matchCase.setText("Match Case ");
    MatchWholeWords=new JCheckBox("Match Whole Words ");
    tf = new TextField(30);
    find_next = new JButton("Find Next");
    cancel = new JButton("Cancel");

    setLayout(null);
    int label_w = 80;
    int label_h = 20;
    int tf_w    = 120;

    l1.setBounds(10,10, label_w, label_h);
    add(l1);
    tf.setBounds(10+label_w, 10, tf_w, 20);
    add(tf);
    
    matchCase.setBounds(10, 10+label_h+10, label_w, label_h);
    add(matchCase);
    MatchWholeWords.setBounds(10, 10+label_h+35, label_w, label_h);
    add(MatchWholeWords);

    find_next.setBounds(250, 10, 100, 20);
    add(find_next);
    cancel.setBounds(250, 40, 100, 20);
    add(cancel);
    int w = 400;
    int h = 200;
    setSize(w,h);
    Point center = GraphicsEnvironment.getLocalGraphicsEnvironment().getCenterPoint();
    setLocation(center.x-w/2, center.y-h/2);
    setVisible(true);
}
 public static void main(String args[]){
  new FindDemo();
 }
}

Solution

    1. Use Swing components instead of AWT components (LabelJLabel; TextFieldJTextField, ...)
    2. Don't use a null layout manager (setLayout(null)) and setBounds(). Examine Using Layout Managers.
    3. Instead of setSize(w, h) use pack() method.

    full name is not displayed

    Because you use setBounds() and null layout. matchCase.setBounds(10, 10 + label_h + 10, label_w, label_h) you set not enough space for JCheckBox because of that it shows with '...'.

    For example your panel with a GridBagLayout:

    import java.awt.GridBagConstraints;
    import java.awt.GridBagLayout;
    import java.awt.Insets;
    
    import javax.swing.JButton;
    import javax.swing.JCheckBox;
    import javax.swing.JFrame;
    import javax.swing.JLabel;
    import javax.swing.JTextField;
    
    public class TestFrame extends JFrame {
    
        JLabel l1;
        JCheckBox matchCase, MatchWholeWords;
        JTextField tf;
        JButton find_next, cancel;
    
        public TestFrame() {
            l1 = new JLabel("Find What: ");
            matchCase = new JCheckBox();
            matchCase.setText("Match Case ");
            MatchWholeWords = new JCheckBox("Match Whole Words ");
            tf = new JTextField(30);
            find_next = new JButton("Find Next");
            cancel = new JButton("Cancel");
    
            setLayout(new GridBagLayout());
    
            GridBagConstraints c = new GridBagConstraints();
            c.insets = new Insets(5, 5, 5, 5);
            c.gridx = 0;
            c.gridy = 0;
            c.anchor = GridBagConstraints.WEST;
            add(l1, c);
            c.gridx++;
            c.fill = GridBagConstraints.HORIZONTAL;
            c.weightx = 1;
            add(tf, c);
            c.fill = GridBagConstraints.NONE;
            c.weightx = 0;
            c.gridx++;
            add(find_next, c);
            c.gridx = 0;
            c.gridy = 1;
            c.gridwidth = 2;
            add(matchCase, c);
            c.gridx++;
            c.gridx++;
            c.gridwidth = 1;
            c.fill = GridBagConstraints.HORIZONTAL;
            add(cancel, c);
            c.gridy++;
            c.gridx = 0;
            c.gridwidth = 2;
            add(MatchWholeWords, c);
            setLocationRelativeTo(null);
            pack();
            setVisible(true);
        }
    
        public static void main(String[] args) {
            new TestFrame();
        }
    }
    

    enter image description here