简体   繁体   中英

Java Swing Checkboxes in a SplitPane

The current layout I am after is simply a split pane horizonal where on the left side are checkboxes and the right side will be output. I will eventually add a submit button the left side after the user checks all wanted items and I will display the result on the right side. The current issue is that I can't even get the background color to show up and the checkboxes are adding wonky. At certain times I can only see one checkbox in the left panel and I am not sure why, and I also set every container to visible and still not able to see it. I add them in the addBoxes function.

import java.awt.Color;
import java.util.ArrayList;

import javax.swing.JCheckBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JSplitPane;

public class CheckBox2 extends JFrame {
        private ArrayList<JCheckBox> boxes = new ArrayList<JCheckBox>();
        JLabel leftLabel;
        JLabel rightLabel;
        JSplitPane splitPane;

    public CheckBox2() {
         leftLabel = new JLabel();
         rightLabel = new JLabel();

        splitPane = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT, new JScrollPane(leftLabel), new JScrollPane(rightLabel) );
        leftLabel.setBackground(Color.BLUE);
        rightLabel.setBackground(Color.RED);
        leftLabel.setVisible(true);
        rightLabel.setVisible(true);
        splitPane.setVisible(true);
        add(splitPane);



    }

    void addBoxes() {
        int i = 0;

        for ( i = 0; i < 1; i++ ) {
            add(new JCheckBox("word" + i ) );
        }
    }
    public static void main(String[] args) {
        // TODO Auto-generated method stub

        CheckBox2 cb = new CheckBox2();
        cb.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        cb.setSize(340, 340);
        cb.setVisible(true);
        cb.addBoxes();
    }



}

Start by having a read of Laying Out Components Within a Container and How to Use Split Panes both which contain plenty of examples.

Swing layouts are lazy. This means that unless you purposefully trigger a layout pass, any changes won't be reflected on the UI (until a layout pass is triggered, such as resizing the window or making it visible for the first time).

While you can call revalidate and repaint on the container your are changing, in your case, simply calling setVisible last will have the same desired effect

Thanks, so with that I'm just getting the last checkbox, checkbox 9 to show up but it's not giving split screen or showing color :(

That's because JFrame , by default, uses a BorderLayout , which only allows a single component to be managed at any of the five available positions. Instead, you need to add you checkboxes to one of the containers in the split pane.

SplitPane和复选框

import java.awt.Color;
import java.awt.Dimension;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.util.ArrayList;
import javax.swing.JCheckBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JSplitPane;

public class CheckBox2 extends JFrame {

  private ArrayList<JCheckBox> boxes = new ArrayList<JCheckBox>();
  JSplitPane splitPane;

  private JPanel leftPanel;
  private JPanel rightPanel;

  public CheckBox2() {

    leftPanel = new JPanel(new GridBagLayout());
    rightPanel = new JPanel(new GridBagLayout()) {
      @Override
      public Dimension getPreferredSize() {
        return new Dimension(200, 200);
      }
    };

    splitPane = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT, leftPanel, new JScrollPane(rightPanel));
    leftPanel.setBackground(Color.BLUE);
    rightPanel.setBackground(Color.RED);
    add(splitPane);

    addBoxes();
  }

  void addBoxes() {
    int i = 0;

    GridBagConstraints gbc = new GridBagConstraints();
    gbc.gridwidth = GridBagConstraints.REMAINDER;
    for (i = 0; i < 10; i++) {
      leftPanel.add(new JCheckBox("word" + i), gbc);
    }
  }

  public static void main(String[] args) {
    // TODO Auto-generated method stub

    CheckBox2 cb = new CheckBox2();
    cb.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
    cb.pack();
    cb.setLocationRelativeTo(null);
    cb.setVisible(true);
  }

}

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