-1

I have already a collection(List) of sentences. So I need to put every single sentences in a check box.The number of check boxes depends on the number of Vector Size. For example, I have 50 sentences, so I need 50 check boxes. Then user can check the desired sentences to be written in a text file. The following class (ViewerFrame) is supposed to build a frame to have many checkboxes the same as the number of sentences in the array list(ManualSummarySys.sentences).Class (ManualSummarySys) is working perfectly.

public class ViewerFrame {

import java.awt.BorderLayout ;
import java.awt.Font ;
import java.awt.GridLayout ;
import java.awt.ScrollPane ;
import java.awt.event.ActionEvent ;
import java.awt.event.ActionListener ;
import java.util.ArrayList ;
import java.util.List ;
import javax.swing.ButtonGroup ;
import javax.swing.JButton ;
import javax.swing.JCheckBox ;
import javax.swing.JComboBox ;
import javax.swing.JFrame ;
import javax.swing.JLabel ;
import javax.swing.JPanel ;
import javax.swing.JRadioButton ;
import javax.swing.border.EtchedBorder ;
import javax.swing.border.TitledBorder ;
import javax.swing.plaf.metal.MetalBorders ;

public class ViewerFrame extends JFrame {

    private static final int FRAME_WIDTH = 10;
    private static final int FRAME_HEIGHT = 5;
    private JLabel sampleField;
    private List<JCheckBox> checkboxes = new ArrayList<JCheckBox>();
    private ActionListener listener;

    public ViewerFrame() {

        sampleField = new JLabel("choose your prefered sentences");
        add(sampleField, BorderLayout.WEST);

        class ChoiceListener implements ActionListener {

            public void actionPerformed(ActionEvent event) {
            }
        }
        listener = new ChoiceListener();
        createControlPanel();

        setSize(FRAME_WIDTH, FRAME_HEIGHT);
        add(this.createCheckBoxes());

    }

    public void createControlPanel() {
        JFrame sizeGroupPanel = createCheckBoxes();


        JPanel controlPanel = new JPanel();
        controlPanel.setLayout(new GridLayout(3, 1));

        controlPanel.add(sizeGroupPanel);
        controlPanel.add(styleGroupPanel);

        add(controlPanel, BorderLayout.LINE_START);

    }

    public JFrame createCheckBoxes() {
        JFrame frame = new JFrame("Sentences");
        frame.setBorder(new TitledBorder(new EtchedBorder(), "Sentences"));
        for (int i = 0; i < ManualSummarySys.sentences.size(); i++) {
            JCheckBox checkbox = new JCheckBox(ManualSummarySys.sentences.get(i));
            checkboxes.add(checkbox);
            checkbox.addActionListener(listener);
            frame.add(checkbox);

        }
        return frame;
    }
}
mKorbel
  • 109,525
  • 20
  • 134
  • 319
IsmailSab'i
  • 1
  • 1
  • 1
  • 3
    So apart from the fact that the `JFrame` uses a `BorderLayout` by default, which means you'll only ever see a single check box (the last one), and you're trying to add a window to a window, what's your question? – MadProgrammer Mar 18 '13 at 02:42

3 Answers3

3

You extend from JFrame, which is generally not advisable, but lets move on from this...

Then you call createControlPanel, which calls createCheckBoxes, which creates a new JFrame.

In createCheckBoxes you fail to set the layout manager for this frame, there by using the default BorderLayout, meaning that only one check box will actually be visible (the last one added).

Back in createControlPanel you try and add the frame that was created in createCheckBoxes to a panel. You also try and add a object called styleGroupPanel which I can't find a reference to anywhere in your code...

Back in the constructor, you call setSize, this is generally unadvisable, instead you should be using pack.

Finally, you call createCheckBoxes AGAIN and try and add a frame to a frame.

You can't add windows to anything else. Instead of trying to create another window in createCheckBoxes why not just return a JPanel instead?

MadProgrammer
  • 343,457
  • 22
  • 230
  • 366
2

Why does createCheckBoxes() return a JFrame of all things? And then you add this JFrame to another JFrame? Sorry, but that makes no sense at all.

For my money, if I wanted to display a mess of checkboxes like that, I'd use a DefaultTableModel placed in a JTable. One column would be a Boolean column for the check box and the other would be the String column for the line of text.

Hovercraft Full Of Eels
  • 283,665
  • 25
  • 256
  • 373
0

okay, I am pasting this code if it helps someone with a similar question. do note that here - the scroll pane contains the JPanel within, and the Scroll Pane is presented to the User on a JDialog (or showConfirmDialog). also has the "Select All" checkbox on top of the checkbox list.

public class Test implements ItemListener{

List<String> testset;
JCheckBox selectallcheckbox = new JCheckBox();
List<JCheckBox> testscheckboxes = new ArrayList<JCheckBox>();
List<String> cancel = Arrays.asList("cancel");

public UserInputTest(List<String> tests) {
    this.testset = tests;
}

public List<String> GetCheckBoxes() {

    // Create Content Pane as JScrollPane and adding JPanel into scroll pane.  
    JPanel testspanel = new JPanel();
    testspanel.setLayout(new BoxLayout(testspanel, BoxLayout.Y_AXIS));

    JScrollPane pane = new JScrollPane(testspanel,
                                JScrollPane.VERTICAL_SCROLLBAR_ALWAYS, 
                                JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
    pane.setMinimumSize(new Dimension(400, 400));
    pane.setPreferredSize(new Dimension(400, 500));

    // Create Swing Components and adding them to JPanel
    selectallcheckbox.setText("Select All");
    selectallcheckbox.setSelected(false);
    selectallcheckbox.addItemListener(this);

    testspanel.add(selectallcheckbox, 0);
    testspanel.add(Box.createVerticalGlue());

    // Adding each element from string list to one checkbox into the JPanel 
    for(int i = 0; i < testset.size() ; i++) {
        JCheckBox testcheckbox = new JCheckBox();
        testcheckbox.setText(testset.get(i));
        testcheckbox.setSelected(false);
        //testspanel.setComponentZOrder(testcheckbox, i+1);
        testspanel.add(testcheckbox, i+1);
        testspanel.add(Box.createVerticalGlue());
        testscheckboxes.add(i, testcheckbox);
    }

    int x = JOptionPane.showConfirmDialog(null, pane, "Select Tests", JOptionPane.OK_CANCEL_OPTION);

    List<String> InputTestDetails = new ArrayList<String>();
    if (x == JOptionPane.OK_OPTION) {
        for(JCheckBox getvaluechkbox : testscheckboxes) {
            if(getvaluechkbox.isSelected()) {
                InputTestDetails.add(getvaluechkbox.getText());
            }   
        }
        return InputTestDetails;
    }

    else if (x == JOptionPane.CANCEL_OPTION) {
        return cancel; 
    }

    return cancel; 
}


@Override
public void itemStateChanged(ItemEvent e) {

    if(e.getSource() == selectallcheckbox) {    
        if(selectallcheckbox.isSelected() == true){
            for(JCheckBox setchkbox : testscheckboxes) {
                setchkbox.setSelected(true);
            }   
        }
        else {
            for(JCheckBox setchkbox : testscheckboxes) {
                setchkbox.setSelected(false);
            }
        }
    }

}

}

aruns.k
  • 11
  • 1