EDIT: Using the solutions below, I modified the code to have a JPanel inside JScrollPane. Using JButton, I add JCheckBoxes to the JPanel inside the JScrollPane. This was one problem resolved since JScrollPanecan accepts only one JComponent. The remaining problems were resolved by setting the gridlayout for the JPanel inside the JScrollPane. I kept my original question here for posterity:
ORIGINAL QUESTION: I am trying to dynamically create JCheckBox and add them to JScrollPane, but, alas, I have not achieved much success. I reduced it to a single realization of the concept of evidence.
I have a JScrollPaneon JPanel inside a JFrame. Also on JPanel, I added a button that should add a JCheckBox to the JScrollPane when clicked. Should be simple enough. The code inside the button is as follows:
private void addCheckBoxActionPerformed(java.awt.event.ActionEvent evt) { JCheckBox cb = new JCheckBox("New CheckBox"); jScrollPaneCheckBoxes.add(cb); jScrollPaneCheckBoxes.revalidate(); }
The code seems to work without errors. I have no exceptions, and using the debugger shows that the JCheckBox is actually added to the JScrollPane. Unfortunately, nothing is displayed in the application. I need to know where to look for the problem.
Here is a piece of code that you can simply run. Unfortunately, I threw this together using Netbeans and its graphic designer, and as such, it is a little longer than necessary, especially the generated code. Focus on the jButton1ActionPerformed method, which is where the code above is taken from.
EDIT: this code now does what I need. :-)
package dynamiccheckboxsscce; import javax.swing.JCheckBox; public class Main extends javax.swing.JFrame { public Main() { initComponents(); } @SuppressWarnings("unchecked") private void initComponents() { jScrollPane1 = new javax.swing.JScrollPane(); jPanel1 = new javax.swing.JPanel(); jButton1 = new javax.swing.JButton(); setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE); jScrollPane1.setHorizontalScrollBarPolicy(javax.swing.ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER); jScrollPane1.setPreferredSize(new java.awt.Dimension(250, 250)); jPanel1.setPreferredSize(new java.awt.Dimension(300, 250)); jPanel1.setLayout(new java.awt.GridLayout(0, 2, 10, 10)); jScrollPane1.setViewportView(jPanel1); jButton1.setText("Add Checkbox"); jButton1.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { jButton1ActionPerformed(evt); } }); javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane()); getContentPane().setLayout(layout); layout.setHorizontalGroup( layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(layout.createSequentialGroup() .addContainerGap() .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(layout.createSequentialGroup() .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 309, Short.MAX_VALUE) .addContainerGap()) .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup() .addGap(0, 0, Short.MAX_VALUE) .addComponent(jButton1) .addGap(112, 112, 112))))); layout.setVerticalGroup( layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup() .addContainerGap() .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 11, Short.MAX_VALUE) .addComponent(jButton1) .addContainerGap())); pack(); } private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) { JCheckBox cb = new JCheckBox("New CheckBox"); jPanel1.add(cb); jPanel1.revalidate(); jPanel1.repaint(); } public static void main(String args[]) { try { for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) { if ("Nimbus".equals(info.getName())) { javax.swing.UIManager.setLookAndFeel(info.getClassName()); break; } } } catch (ClassNotFoundException ex) { java.util.logging.Logger.getLogger(Main.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); } catch (InstantiationException ex) { java.util.logging.Logger.getLogger(Main.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); } catch (IllegalAccessException ex) { java.util.logging.Logger.getLogger(Main.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); } catch (javax.swing.UnsupportedLookAndFeelException ex) { java.util.logging.Logger.getLogger(Main.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); } java.awt.EventQueue.invokeLater(new Runnable() { public void run() { new Main().setVisible(true); } }); } private javax.swing.JButton jButton1; private javax.swing.JPanel jPanel1; private javax.swing.JScrollPane jScrollPane1; }
Thanks in advance.