Search code examples
javaswinglayoutlayout-managerpanels

Rearranging buttons and jlist in a panel


I created buttons and a jlist. All my buttons are in another panel called buttonPanels, and I wanted to add that panel and the jList to another panel called jPanelSouth. I tried positioning my buttons by using the ______.setLocation(x,y) but the buttons don't seem to move anywhere within the panel (same with jlist).

 //southPanel and stuff
  jPanelSouth = new JPanel();
  jPanelSouth.setBorder(BorderFactory.createLineBorder(Color.black, 5));

  //buttons
  buttonPanel = new JPanel(new GridLayout(3,1));
  drawCardBtn = new JButton("Draw Card");
  moveBtn = new JButton("Move");
  playCardBtn = new JButton("Play Card");


  buttonPanel.add(drawCardBtn);
  buttonPanel.add(moveBtn);
  buttonPanel.add(playCardBtn);

  roomList = new JList(roomNumbers);
  roomList.setVisibleRowCount(4);
  roomList.setFixedCellWidth(100);
  roomList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);


  jPanelSouth.add(buttonPanel);
  jPanelSouth.add(roomList);

This is how my window looks right now

This is how I want it to look like

I'm not really sure what layout I should be using in this case


Solution

  • You should change a layout in your jPanelSouth. For example, try add this line to your code.

    jPanelSouth.setLayout(new BoxLayout(jPanelSouth, BoxLayout.Y_AXIS));
    

    Read some more about swing layouts here.