Question

I am working on the functionality of adding JCheckBox to Panel and then adding that Panel to JScrollPane. So far i am done with adding different JCheckBox dynamically to Panel but when i add that same Panel to JScrollPane, it does not shows the JCheckBoxes to JScrollPane

Q.1 What might be the possible reason for same thing not appearing in JScrollPane?

Q.2 Even if i added JCheckBox to Panel and then adding that Panel to JScrollPane how do i manage there setSelected functionality i mean how i can add the ActionListener to that dynamically added JCheckBox?

Note: I am using a AbsoluteLayout for Panel

Was it helpful?

Solution

Use for example GridLayout (1 column and multiple rows).

Keep array (or list) of the checkboxes. Go through the list adding ActionListener or you can get the main panel's children components, iterate through them casting to JCheckBox and add the listener.

The worst way is to define preferred size for the panel with AbsoluteLayout.

OTHER TIPS

AbsoluteLayout is not a good solution but with fixed size it should work. Are you calling revalidate on your panel object after you added checkbox?

As for Q2 you can store added checkboxes in a Vector or HashMap ( depends what logic is involved ) and then you can create custom ActionListener that implements mentioned interface. What is more that you can pass reference of your panel to your custom ActionListener and within it's actionPerformed use that reference to call methods on the panel which stores vector of your checkboxes.

Here is my quick example of what I am talking about:

package pkg;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Vector;

import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;

class AddCheckBoxAction implements ActionListener{
    CheckBoxPanel panel;
    public AddCheckBoxAction(CheckBoxPanel panel){
        this.panel = panel;
    }
    @Override
    public void actionPerformed(ActionEvent arg0) {
        panel.addNewCheckBox();

    }

}

class CheckBoxAction implements ActionListener{
    private int id;
    CheckBoxAction(int id){
        this.id = id;
    }
    @Override
    public void actionPerformed(ActionEvent arg0) {
        System.out.println("CheckBox "+this.id+" was clicked");

    }
}

class CheckBoxPanel extends JPanel{
    private JButton addCheckBox = new JButton("Add CheckBox");
    private Vector<JCheckBox> checkBoxes = new Vector<JCheckBox>();
    public CheckBoxPanel(){
        addCheckBox.addActionListener(new AddCheckBoxAction( this ) );
        add(addCheckBox);
    }
    public void addNewCheckBox() {
        JCheckBox chBox = new JCheckBox("CheckBox "+( this.checkBoxes.size()+1 ));
        chBox.addActionListener(new CheckBoxAction(this.checkBoxes.size()+1));
        this.checkBoxes.add(chBox);
        add(chBox);
        this.revalidate();



    }
}
public class DynamicCheckBoxTest {

    /**
     * @param args
     */
    public static void main(String[] args) {
        CheckBoxPanel chD = new CheckBoxPanel();
        JFrame mainFrame = new JFrame();
        JScrollPane scrollP = new JScrollPane( JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS );
        scrollP.setViewportView(chD);
        mainFrame.setSize(320,200);
        mainFrame.getContentPane().add(scrollP);
        mainFrame.setVisible(true);

    }

}

AbsoluteLayout is not a healthy option, Instead, you can use - Containers

I was able to fix your issue with that. Below is the code, do refer it

Container contentPane = getContentPane();
contentPane = getContentPane();
JPanel panel = new JPanel();
List<String> configList = new ArrayList<>();

for( int i = 0; i < configList.size(); i++ )
{
   String configValues = configList.get( i );
   JCheckBox value = new JCheckBox( configValues );
   panel.add( value );
}

JScrollPane scrollPane = new JScrollPane( panel, JScrollPane.VERTICAL_SCROLLBAR_ALWAYS, JScrollPane.HORIZONTAL_SCROLLBAR_NEVER );
scrollPane.setPreferredSize( new Dimension( *SIZE*, *SIZE*) );
add( scrollPane );
contentPane.add( scrollPane, BorderLayout.CENTER );

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top