1

I've trying (unsuccessfully) to add loop generated JButtons to a JPanel. The thing is that the JPanel is over JScrollPane. Here is my code:

String categoria = this.cmbCategoria.getSelectedItem().toString();
    String[] partidos = myEstadio.buscarPartidos(categoria).split("/");        
    JButton b;
    for(String p : partidos){
        b = new JButton(p);
        this.panelScroll.add(b, BorderLayout.CENTER);
        System.out.println(p);
        System.out.println(b.getLocationOnScreen());

As you can see, I'm printing the Label and the Button's location to be sure it exists.

It exists and the label too but it doesn't show up. The JPanel has a BorderLayout layout and I'm using NetBeans 8.0.2

mKorbel
  • 109,525
  • 20
  • 134
  • 319
Andrés Buitrago
  • 205
  • 1
  • 2
  • 9
  • You can't put multiple components in the same position in `BorderLayout`. Use another layout manager. – kiheru May 29 '15 at 16:56

2 Answers2

0

In fact you are putting newly created Button instances to the same place. One over another. Use other layout constraints (such as NORTH, SOUTH, ...), a different LayoutManager or even better, nested LayoutManagers such as FlowLayout within BorderLayout. Moreover, you should add it to the JPanel.

Würgspaß
  • 4,660
  • 2
  • 25
  • 41
  • BorderLayout didn't worked but I tried with GridBagLayout and works like a charm! I don't know why I didn't work if I already used it in other projects. However the GridBagLayout puts the button after the other and I want it under the other, like a list. Any idea of how to do that. I've been searching on the Constraints but nothing seems to work. THanks. – Andrés Buitrago May 29 '15 at 19:56
  • That's far beyond scope here but here is a nice tutorial: http://docs.oracle.com/javase/tutorial/uiswing/layout/visual.html Anyway keep in mind: nesting different layout managers is the key to success. For a list of buttons use JPanel with FlowLayout, but add it to another JPanel with BorderLayout, for example. That way you can add other elements to different sections. – Würgspaß May 29 '15 at 20:10
0

Make sure you have marked visibility true of components in case if you are not seeing no buttons at all. If you are seeing one button then use different layout manager

kshitiz
  • 29
  • 1
  • 5