Fahdie
Fahdie

Reputation: 231

Checkbox align in 3 rows

Hello i am coding a JFrame which has alot of checkboxes which are dynamically generated i want to make sure these checkboxes come in 4 different columns and the submit button at the end please help

Here is the code

final JFrame frame=new JFrame("Button Frame");
            //JCheckBox[] checkBoxes = new JCheckBox[6];
            final JCheckBox[] checkBoxes= //= {new JCheckBox("bool"), new JCheckBox("list"), new JCheckBox("3"), new JCheckBox("4"), new JCheckBox("5"), new JCheckBox("6")};
            {
                new JCheckBox("alist"),new JCheckBox("arithmetic"),new JCheckBox("ASCIInumbers"),new JCheckBox("bag"),    
                new JCheckBox("basicSize"),new JCheckBox(" basis_emit"),new JCheckBox("bitstring"),new JCheckBox("bit"),        
                new JCheckBox("blast"),new JCheckBox("bool"),new JCheckBox("canonical"),new JCheckBox("Coder"),       
                new JCheckBox("combin"),new JCheckBox("complex"),new JCheckBox("ConseqConv"),new JCheckBox("container"),
                new JCheckBox("Decode"),new JCheckBox("DeepSyntax"),new JCheckBox("defCNF"),new JCheckBox("divides"),
                new JCheckBox("Encode"),new JCheckBox("EncodeVar"),new JCheckBox("extended_emit"),new JCheckBox("extreal"),new JCheckBox("fcp"),       
                new JCheckBox("finite_map"),new JCheckBox("fixedPoint"),new JCheckBox("float"),new JCheckBox("fmaptree"),   
                new JCheckBox("frac"),new JCheckBox("gcdset"),new JCheckBox("gcd"),new JCheckBox("HolSmt"),new JCheckBox("hrat"),    
                new JCheckBox("hreal"),new JCheckBox("ieee"),new JCheckBox("ind_type"), new JCheckBox("inftree"),
                new JCheckBox("int_arith"),new JCheckBox("integer_word"),new JCheckBox("integerRing"),new JCheckBox("integer"),     
                new JCheckBox("integral"),new JCheckBox("intExtension"),new JCheckBox("intreal"),new JCheckBox("lbtree"),new JCheckBox("lebesgue"),   
                new JCheckBox("lim"),new JCheckBox("list"),new JCheckBox("llist"),new JCheckBox("logroot"),   
                new JCheckBox("marker"),new JCheckBox("measure"),new JCheckBox("nets"),new JCheckBox("normalForms"),   
                new JCheckBox("numeral_bit"),new JCheckBox("numeral"),new JCheckBox("numpair"),new JCheckBox("numposrep"),    
                new JCheckBox("numRing"),new JCheckBox("num"),new JCheckBox("Omega_Automata"),new JCheckBox("Omega"),new JCheckBox("one"),
                new JCheckBox("operator"),new JCheckBox("option"),new JCheckBox("pair"),     
                new JCheckBox("Past_Temporal_Logic"),new JCheckBox("path"),new JCheckBox("patricia_casts"),new JCheckBox("patricia"),  
                new JCheckBox("poly"),new JCheckBox("poset"),new JCheckBox("powser"),        
                new JCheckBox("pred_set"),new JCheckBox("prelim"),new JCheckBox("prim_rec"),new JCheckBox("primeFactor"),new JCheckBox("probability"),    
                new JCheckBox("quantHeuristics"),new JCheckBox("quote"),      
                new JCheckBox("quotient_list"),new JCheckBox("quotient_option"),new JCheckBox("quotient_pair"),new JCheckBox("quotient_pred_set"),new JCheckBox("quotient_sum"),     
                new JCheckBox("quotient"),new JCheckBox("ratRing"),new JCheckBox("rat"),new JCheckBox("real_sigma"),
                new JCheckBox("realax"),new JCheckBox("real"),new JCheckBox("relation"),new JCheckBox("res_quan"),     
                new JCheckBox("rich_list"),new JCheckBox("ringNorm"),new JCheckBox("ring"),new JCheckBox("sat"),new JCheckBox("semi_ring"),     
                new JCheckBox("seq"),new JCheckBox("set_relation"),new JCheckBox("sorting"),    
                new JCheckBox("state_option"),new JCheckBox("state_transformer"),new JCheckBox("string_num"),new JCheckBox("string"), 
                new JCheckBox("sum_num"),new JCheckBox("sum"),new JCheckBox("Temporal_Logic"),   
                new JCheckBox("topology"),new JCheckBox("transc"),new JCheckBox("update"),new JCheckBox("util_prob"),new JCheckBox("while"),new JCheckBox("words")};

            int numb=checkBoxes.length;
            //List<Checkbox> checkboxes = new ArrayList<Checkbox>();
            int align = BoxLayout.PAGE_AXIS;

            frame.setLayout(new FlowLayout(align));
           // String labels[] = {"A", "B", "C", "D", "E", "F"};
            for (int i = 0; i < numb; i++) {
                //checkBoxes[i].setText(labels[i]);
                frame.add(checkBoxes[i]);
               // Checkbox checkbox = new Checkbooks(labels[i]);
                //checkboxes.add(checkbox); //for further use you add it to the list
                //frame.add(checkbox);
            }
            JButton button = new JButton("Submit");
            frame.add(button);

        }


            frame.setSize(600,600);
            frame.setVisible(true);
            button.addActionListener(new ActionListener() {
                int numb=checkBoxes.length;

                @Override
                public void actionPerformed(ActionEvent arg0) {
                    for(int a=0;a<numb;a++)
                    {
                        if (checkBoxes[a].isSelected()) {

                            //execute("app load [\""+checkBoxes[a].getText()+"Theory\"]; open "+checkBoxes[a].getText()+"Theory;");
                            JOptionPane.showMessageDialog(frame, checkBoxes[a].getText());
                        }
                    }

Here is the image of my current view enter image description here }

Upvotes: 1

Views: 1092

Answers (2)

nIcE cOw
nIcE cOw

Reputation: 24616

Just use a JPanel. Set it's layout to BorderLayout.

Take another, JPanel, set it's layout to GridLayout with 0 rows and 4 columns. Now simply add the JCheckBoxes to it.

Take another, JPanel add JButton to it.

Now add the JPanel containing JCheckBoxes, to the first panel, at BorderLayout.CENTER and the third JPanel, at BorderLayout.PAGE_END. That is it.

Here try this code example:

import java.awt.*;
import javax.swing.*;

public class AlignCheckBoxExample {

    private static final String[] data = {
        "alist", "arithmetic", "ASCIInumbers", "bag",
        "basicSize", "basis_emit", "bitstring", "bit",
        "blast", "bool", "canonical", "coder", "combin",
        "complex", "ConseqConv", "container", "Decode",
        "DeepSyntax", "defCNF", "divides", "Encode",
        "EncodeVar", "extended_emit", "extreal", "fcp",
        "finite_map", "fixedPoint", "float", "fmaptree",
        "frac", "gcdset", "gcd", "HolSmt", "hrat",
        "hreal", "ieee", "ind_type", "inftree",
        "int_arith", "integer_word", "integerRing", "integer",
        "integral", "intExtension", "intreal", "lbtree", "lebesgue",
        "lim", "list", "llist", "logroot",
        "marker", "measure", "nets", "normalForms",
        "numeral_bit", "numeral", "numpair", "numposrep"
    };

    private JCheckBox[] checkBox;
    private JButton submitButton;

    private void displayGUI() {
        JFrame frame = new JFrame("Align JComboBox Example");
        frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);

        JPanel contentPane = new JPanel();
        contentPane.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));
        contentPane.setLayout(new BorderLayout(5, 5));

        checkBox = new JCheckBox[data.length];
        JPanel centerPanel = new JPanel();
        centerPanel.setLayout(new GridLayout(0, 4, 5, 5));
        for (int i = 0; i < data.length; i++) {
            checkBox[i] = new JCheckBox(data[i]);
            centerPanel.add(checkBox[i]);
        }
        contentPane.add(centerPanel, BorderLayout.CENTER);

        JPanel footerPanel = new JPanel();
        submitButton = new JButton("Submit");
        footerPanel.add(submitButton);
        contentPane.add(footerPanel, BorderLayout.PAGE_END);

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

    public static void main(String[] args) {
        Runnable runnable = new Runnable() {
            @Override
            public void run() {
                new AlignCheckBoxExample().displayGUI();
            }
        };
        EventQueue.invokeLater(runnable);
    }
}

Here is the output:

JCHECKBOX EXAMPLE

Upvotes: 2

camickr
camickr

Reputation: 324108

Don't use a FlowLayout. Instead you should be using a GridLayout.

Read the section from the Swing tutorial on How to Use GridLayout for more information and examples.

If you don't want the button as part of the Grid then you will need to nest panels with different layout managers.

Upvotes: 5

Related Questions