3

I try to add JButtons in the JList to be like a board game.

Here's my code:

public class Board {

public Board() {
    JList list = new JList();
    list.setLayout(new GridLayout(10, 10));
    list.setDragEnabled(true);

    Container container = new Container();
    JFrame frame = new JFrame();
    JPanel panel1 = new JPanel();
    JPanel panel2 = new JPanel();
    frame.add(container);
    container.add(panel1);

    for (int j = 0; j < 99; j++) {
        list.add(createButton());
    }

    panel2.add(list);
    container.add(panel2);
    panel2.setLayout(new GridLayout());
    panel1.setBounds(50, 150, 150, 150);
    panel1.setBackground(Color.YELLOW);
    panel2.setBounds(650, 150, 500, 500);
    panel2.setBackground(Color.RED);

    frame.setSize(1366, 768);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setVisible(true);
}

public JButton createButton() {
    JButton button = new JButton();
    button.setBackground(Color.BLUE);
    return button;
}
}

If I put 100 repetitions I get this: enter image description here

If I put 99 repetitions i get this:

enter image description here

So my question is how can I fill the above board?

Castiblanco
  • 1,200
  • 4
  • 13
  • 32
Spyros Chiotakis
  • 239
  • 1
  • 10
  • 1
    This [example](http://stackoverflow.com/questions/4674268/how-do-i-make-my-custom-swing-component-visible/4674686#4674686) show one way to dray icons around a grid using panels and labels. – camickr Mar 29 '13 at 00:23

2 Answers2

4

The reason it's happening is because you're adding all the buttons to a JList, which isn't really how you're meant to use a JList (usually you modify the model backing the list in order to add/remove items from the list). The list is internally doing something that takes up the first slot.

If you change this line:

JList list = new JList();

to:

JPanel list = new JPanel();

your layout will work (you have to remove the setDragEnabled line as well, and change 99 to 100). Whether there's some capability of JList that you want I'm not sure, but that's why your layout isn't working properly.

Ash
  • 9,296
  • 2
  • 24
  • 32
  • I used JList because I need to make drag and drop later on. Actually i'm creating a battleship game and I can't use JPanel for that according to http://docs.oracle.com/javase/tutorial/uiswing/dnd/defaultsupport.html – Spyros Chiotakis Mar 28 '13 at 23:58
  • I've always found setting up dnd to be painful, but I dunno, using a JList just seems like the wrong approach. – Ash Mar 29 '13 at 00:01
  • I think so too I spent a lot of time trying to make something out of JList.I will see if I can come up with something through JPanel.Thanks for the answer! – Spyros Chiotakis Mar 29 '13 at 00:12
2

You should create your JList :

DefaultListModel dataModel = new DefaultListModel();
JList list = new JList(dataModel);
list.setLayoutOrientation(JList.HORIZONTAL_WRAP);

Set a ListCellRenderer as follows:

 final JButton button = createButton();
    list.setCellRenderer(new ListCellRenderer() {
             @Override
             public Component getListCellRendererComponent(JList list, Object value, int index, boolean isSelected, boolean cellHasFocus) {
                  return button;
             }
    });

Add your items as follows: // doesn't matter what value are you adding, you will decide what information do you need when rendering an item

    for (int j = 0; j < 99; j++) {
        dataModel.add(j, j); 
    }

And an example using this: How To Create custom JList

Edit: some other useful references: Drag and drop Drag and drop inside JList

Gyuri Majercsik
  • 2,141
  • 1
  • 17
  • 29