0

i creating new jpanels using loop. but how i using different object name. here is my code:

for(int i=0; i<panelnumbers.length(); i++){
    MainConfig.page21.addNewPanel(MainConfig.page21.pos1, "INFORMACIÓN No. " + (i + 2));
}

public static void addNewPanel(int y, String title) {
    Add a = new Add(title);
    jLayeredPane3.add(a);
    a.setBounds(0, y, 1333, 450);
    jPanel1.setPreferredSize(new Dimension(1333, (pos + 480)));
    jLayeredPane3.setPreferredSize(new java.awt.Dimension(1333, (pos + 480)));
    jLayeredPane3.validate();
    jScrollPane1.getViewport().setViewPosition(new Point(0, (pos + 480)));
}
mKorbel
  • 109,525
  • 20
  • 134
  • 319

2 Answers2

1

i creating new jpanels using loop. but how i using different object name.

Community
  • 1
  • 1
mKorbel
  • 109,525
  • 20
  • 134
  • 319
1

You are using absolute coordinates, a layout manager gives a better GUI across platforms, later Windows versions, accessibility etcetera.

MainConfig.page21.setLayout(new BoxLayout(MainConfig.page21, BoxLayout.Y_AXIS));

Also normally one would not need the JPanel, an event listener would know how to retrieve the panel.

Nevertheless you could maintain an array of JPanels:

List<JPanel> panels = new ArrayList<>();

In addPanel create a local variable, and add that

public void addNewPanel(int y, String title) { // Not static
    JPanel panel = new JPane();
    ...
    panels.add(panel);
}
Joop Eggen
  • 107,315
  • 7
  • 83
  • 138