-5

I am trying to create GUI like given in first picture, but I am not able to do it.here is the image I am getting only one combo1, combo2, combo3 and serialNoLabel instead of 5 [5 is the size of list]

    ArrayList<String> list; // the size of the list is 5
    JComboBox combo1[] = new JComboBox[list.size()];
    JComboBox combo2[] = new JComboBox[list.size()];
    JComboBox combo3[] = new JComboBox[list.size()];
    JLabel SerialNoLabel[] = new JLabel[list.size()];
    JPanel masterPanel[] = new JPanel[list.size()];

    JDialog masterDialog =  new JDialog();
    masterDialog.setVisible(true);
    masterDialog.setSize(800, 500);
    masterDialog.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
    masterDialog.setVisible(true);
    for(int j =0; j < list.size(); j++) {
        masterPanel[j] = new JPanel();
        SerialNoLabel[j] = new JLabel(list.get(j));
        masterPanel[j].add(SerialNoLabel[j]);
        combo1[j] = new JComboBox();
        masterPanel[j].add(combo1[j]);
        combo2[j] = new JComboBox();
        masterPanel[j].add(combo2[j]);
        combo3[j] = new JComboBox();
        masterPanel[j].add(combo3[j]);
        masterDialog.add(masterPanel[j]);
        masterDialog.revalidate();
    }
  • Don't you have a layout for you masterDialog? Also can you print screen what you get? With a quick look I really do believe that it's a layout issue. – LBes Aug 23 '16 at 12:00
  • When you add one of the masterPanels to the masterDialog, you're adding it to the center of a BorderLayout. You're effectively overlaying your masterPanels, so only one shows. You need to have a mainPanel (JPanel) with a FlowLayout. You would add the masterPanels to the mainPanel, and finally, add the mainPanel to the masterDialog. – Gilbert Le Blanc Aug 23 '16 at 12:02
  • @LBes I am using MigLayout for masterDialog. – Varshal Davda Aug 23 '16 at 12:04
  • @GilbertLeBlanc Can you explain me in brief... I am new to swing – Varshal Davda Aug 23 '16 at 12:05
  • I've not used a MigLayout. I made an assumption based on your tiny code fragment. Put a self-contained (don't use the MigLayout), **runnable** example of your problem in your question, and I'll fix it so it produces the GUI in your drawing. – Gilbert Le Blanc Aug 23 '16 at 12:16
  • @GilbertLeBlanc This is the only code that I have written to create this GUI. – Varshal Davda Aug 23 '16 at 12:44

1 Answers1

3

I believe it's a layout issue leading your masterPanels to be on top of each other.

So I would do something like this:

JPanel mainPanel = new JPanel();
FlowLayout experimentLayout = new FlowLayout();
mainPanel.setLayout(experimentLayout);
for(int j =0; j < list.size(); j++) {
        masterPanel[j] = new JPanel();
        SerialNoLabel[j] = new JLabel(list.get(j));
        masterPanel[j].add(SerialNoLabel[j]);
        combo1[j] = new JComboBox();
        masterPanel[j].add(combo1[j]);
        combo2[j] = new JComboBox();
        masterPanel[j].add(combo2[j]);
        combo3[j] = new JComboBox();
        mainPanel.add(masterPanel[j]);
    }

Of course you could other layouts as well. But I believe you want to go for a FlowLayout. See the documentation about FlowLayout here.

You can learn more about other layouts here

LBes
  • 3,366
  • 1
  • 32
  • 66