0

What I have done?

-Created 10 JCheckBoxes in JFrame. ex A,B,C,..

-Created 10 JFrames for each JCheckBox as an ex., JFrame A,JFrame B,JFrameC,..

-Created ArrayList for storing values of JCheckBoxes whether it is selected or not.

-Created ArrayList for JFrames and each Frame is added into it.

What I want to do?

After selecting multiple CheckBoxes when I will click on 'next' the first JFrame for the First CHECKED BOX will appear.

Consider Example: I selected the check boxes B,C,F,H after that I will click on 'Next' the JFrame B will appear. Now I will enter data and again I will click on 'next' it will check next selected CheckBox and that JFrame will appear as here C. Then JFrame F & lastly JFrame H.

How should I do these?

Ash..
  • 1
  • 5
  • You are probably looking for modal JDialog instead of JFrame. A modal dialog will block the code flow until it is closed. – Guillaume Polet Mar 21 '13 at 12:33
  • Read Andrew Thompson's classic work, The Use of Multiple JFrames. http://stackoverflow.com/questions/9554636/the-use-of-multiple-jframes-good-bad-practice/9554657#9554657 – Gilbert Le Blanc Mar 21 '13 at 12:56
  • @GuillaumePolet Can I add JTextBox,JPanel,JLabel,JButton and Database connectivity in JDialog ? – Ash.. Mar 21 '13 at 15:14
  • `JTextBox,JPanel,JLabel,JButton` yes, you can always add component to JDialog's – Guillaume Polet Mar 21 '13 at 15:47
  • I am again facing same problem. Even I used JDialog its showing all Dialog boxes while used it in 'for loop' – Ash.. Mar 21 '13 at 16:54
  • Have you made sure that: 1) your dialog is modal 2) Your code is running on the EDT (if not, use `SwingUtilities.invokeLater()`) – Guillaume Polet Mar 21 '13 at 17:46

1 Answers1

0

Add whatever input data you want into the InputPanel class.

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.SwingUtilities;

public class TestGUI {
    JFrame frame;
    ArrayList<JCheckBox> checkBoxes = new ArrayList<JCheckBox>();
    JButton next = new JButton("Next");

    public TestGUI() {
        frame = new JFrame();
        frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        frame.setLayout(new BoxLayout(frame.getContentPane(), BoxLayout.Y_AXIS));
        for (int i = 1; i <= 10; i++) {
            JCheckBox box = new JCheckBox(Integer.toString(i));
            box.setName(Integer.toString(i));
            checkBoxes.add(box);
            frame.add(box);
        }
        next.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent arg0) {
                for (int i = 0; i < checkBoxes.size(); i++) {
                    if (checkBoxes.get(i).isSelected()) {
                        InputPanel panel = new InputPanel();
                        int choice = JOptionPane.showConfirmDialog(frame,
                            panel, "Get info for checkbox "
                            + checkBoxes.get(i).getName(),
                            JOptionPane.OK_CANCEL_OPTION);
                        if (choice == JOptionPane.OK_OPTION) {
                            System.out.println(panel.getText());
                        }
                    }
                }
            }
        });
        frame.add(next);
        frame.pack();
        frame.setVisible(true);
    }
    private class InputPanel extends JPanel {
        private JLabel label = new JLabel("Get some info");
        private JTextField text = new JTextField(10);

        public InputPanel() {
            add(label);
            add(text);
        }

        public String getText() {
            return text.getText();
        }
    }

    public static void main(String args[]) {
        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                TestGUI gui = new TestGUI();
            }
        });
    }
}
jmclellan
  • 550
  • 3
  • 12
  • Thnxz a lot buddy.. I made multiple panels here and for each chkboxes I called its respective panel. Now one more trouble is as I am novice programmer how should I make action of "OK" on that respective panel itself. As I click on OK of respective panel it should do activities related to panel & will return to its panel again ! – Ash.. Mar 23 '13 at 05:04
  • You can add as many buttons as you want with their own ActionListeners that do whatever to the panel. The dialog displayed with JOptionPane.showConfirmDialog will be big enough to fit whatever you put in it. If you want full control over how the dialog behaves you will need to create your own instance of JDialog. Just make sure it's set as modal so it blocks until it's finished doing its job. – jmclellan Mar 24 '13 at 00:09