Search code examples
javaswingnetbeansjradiobutton

How do I auto populate jRadioButtons with information?


I have been having an issue and I dont know how to solve it. In my code below you can see that I am trying to create a GUI that auto populates 20 jRadioButtons with the information provided, but when I run the program there are no buttons visible. Can someone please point me to what I am doing incorrectly? package person;

    import java.util.ArrayList;
    import java.util.List;
    import javax.swing.*;

    public class PersonInfoUI extends javax.swing.JFrame {

        public PersonInfoUI() {
            initComponents();
            ButtonGroup buttonGroup1 = new ButtonGroup();
            JRadioButton jRadioButton1 = new JRadioButton();
            personSelectorUI.add(jRadioButton1);
            personSelectorUI.revalidate();
            personSelectorUI.repaint();

        }
        @SuppressWarnings("unchecked")
        // <editor-fold defaultstate="collapsed" desc="Generated Code">                          
        private void initComponents() {

            personSelectorUI = new javax.swing.JPanel();
            exitButton = new javax.swing.JButton();
            clearButton = new javax.swing.JButton();
            personInfoOutput = new javax.swing.JTextField();

            setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

            exitButton.setFont(new java.awt.Font("Times New Roman", 1, 18)); //          NOI18N
            exitButton.setText("Exit");
            exitButton.addActionListener(new java.awt.event.ActionListener() {
                public void actionPerformed(java.awt.event.ActionEvent evt) {
                    exitButtonActionPerformed(evt);
                }
            });

            clearButton.setFont(new java.awt.Font("Times New Roman", 1, 18)); //     NOI18N
            clearButton.setText("Clear");
            clearButton.addActionListener(new java.awt.event.ActionListener() {
                public void actionPerformed(java.awt.event.ActionEvent evt) {
                    clearButtonActionPerformed(evt);
                }
            });

            javax.swing.GroupLayout personSelectorUILayout = new    
                javax.swing.GroupLayout(personSelectorUI);
            personSelectorUI.setLayout(personSelectorUILayout);
            personSelectorUILayout.setHorizontalGroup(

    personSelectorUILayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEA    DING)
                .addGroup(personSelectorUILayout.createSequentialGroup()
                    .addContainerGap()

    .addGroup(personSelectorUILayout.createParallelGroup(javax.swing.GroupLayout.Ali    gnment.LEADING)
                        .addComponent(personInfoOutput,     
    javax.swing.GroupLayout.Alignment.TRAILING)
                        .addGroup(javax.swing.GroupLayout.Alignment.TRAILING,    
    personSelectorUILayout.createSequentialGroup()
                            .addComponent(exitButton,   
    javax.swing.GroupLayout.DEFAULT_SIZE, 344, Short.MAX_VALUE)

    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                            .addComponent(clearButton,  
    javax.swing.GroupLayout.PREFERRED_SIZE, 349, 
    javax.swing.GroupLayout.PREFERRED_SIZE)))
                    .addContainerGap())
            );
            personSelectorUILayout.setVerticalGroup(

    personSelectorUILayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEA    DING)
                .addGroup(personSelectorUILayout.createSequentialGroup()
                    .addGap(151, 151, 151)
                    .addComponent(personInfoOutput,   
    javax.swing.GroupLayout.PREFERRED_SIZE, 121,   
    javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(personSelectorUILayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(exitButton, javax.swing.GroupLayout.DEFAULT_SIZE, 35, Short.MAX_VALUE)
                    .addComponent(clearButton, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addContainerGap())
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(personSelectorUI, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(personSelectorUI, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
        );

        pack();
    }// </editor-fold>                        

    private void clearButtonActionPerformed(java.awt.event.ActionEvent evt) {                                            
        personInfoOutput.setText("");
    }                                           

    private void exitButtonActionPerformed(java.awt.event.ActionEvent evt) {                                           
        System.exit(0);
    }                                          

    public static void main(String args[]) {
        ButtonGroup buttonGroup1 = new ButtonGroup();

        List<Person> people = new ArrayList<>();
            people.add(new Person ("Nate Stoll", true, true, 1981));
            people.add(new Person ("Ashley Stoll", true, true, 1985));
            people.add(new Person ("Brooke Jackson", true, true, 1972));
            people.add(new Person ("Reed Stoll", true, true, 1983));
            people.add(new Person ("Reeda Stoll", true, true, 1942));
            people.add(new Person ("John Stoll", true, true, 1940));
            people.add(new Person ("Clark Kent", true, true, 1912));
            people.add(new Person ("Reed Richards", true, true, 1992));
            people.add(new Person ("Peter Parker", true, true, 1924));
            people.add(new Person ("Charles Xavier", true, true, 1905));
            people.add(new Person ("Bruce Banner", true, true, 1980));
            people.add(new Person ("Cheri Monaghan", true, true, 1979));
            people.add(new Person ("Matthew Groathouse", true, true, 1949));
            people.add(new Person ("John Williams", true, true, 1958));
            people.add(new Person ("Jake Holmes", true, true, 1998));
            people.add(new Person ("Bradley Cooper", true, true, 2015));
            people.add(new Person ("Shirley Temple", true, true, 1907));
            people.add(new Person ("Natalie Stoll", true, true, 1900));
            people.add(new Person ("Lindsay Gonzalez", true, true, 1970));
            people.add(new Person ("Tommy Chong", true, true, 1957));
    for(Person aPers : people){
        final JRadioButton jRadioButton1 = new JRadioButton(aPers.getName());
        jRadioButton1.addActionListener(new java.awt.event.ActionListener() {

        @Override
        public void actionPerformed(java.awt.event.ActionEvent evt) {
            if (jRadioButton1.isSelected()){
                personInfoOutput.setText(jRadioButton1.getText()); }
                }
                });
                buttonGroup1.add(jRadioButton1);
        }
                new PersonInfoUI().setVisible(true);

        java.awt.EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                new PersonInfoUI().setVisible(true);
            }
         });
}


    // Variables declaration - do not modify                     
    private javax.swing.JButton clearButton;
    private javax.swing.JButton exitButton;
    private static javax.swing.JTextField personInfoOutput;
    private static javax.swing.JPanel personSelectorUI;
    // End of variables declaration                   

}

Thanks!


Solution

  • You never add your newly created JRadioButton to any container whose ancestor hierarchy leads to a top level window. In other words, for your JRadioButtons to be displayed in the GUI, the need to be added to something, usually a JPanel, and that JPanel has to be displayed on your GUI -- you're not doing this. Myself, I'd create a JPanel that uses a GridLayout, perhaps one with 1 column and variable rows, I'd add my JRadioButtons to this, I'd add the JPanel to a JScrollPane and I'd add the JScrollPane somewhere to my GUI. I recommend avoiding use of GroupLayout and NetBeans drag-and-drop GUI creation for creating something like this, since GroupLayout doesn't like your adding new components at run-time. I'm also not sure why you're trying to add your JRadioButtons in the static main method, which adds its own unnecessary complexity. If you're hard-coding the Strings, why not do this in a constructor?

    To understand how to do this, you'll need to read the tutorials on use of JPanels and layout managers. You can find links to the Swing tutorials and to other Swing resources here: Swing Info.

    As an aside -- it looks like what you really want to use is a JTable, one that has names in a column and that has a checkbox in a column.