3

I'm developing a game called GalaxyWar, and I am trying to make a map selection menu. I found a problem that when I am using a BoxLayout with BoxLayout.Y_AXIS on a JPanel with setAlignmentX(CENTER_ALIGNMENT), the subcomponents (JPanel's) with assigned size, take up the entire height of the panel (all together), instead of the assigned height!

Here is my code:

scrollPane = new JScrollPane();
    scrollPane.setBounds(160, 11, 452, 307);
    add(scrollPane);

    mapContainer = new JPanel();
    mapContainer.setAlignmentX(CENTER_ALIGNMENT);
    mapContainer.setAlignmentY(JPanel.TOP_ALIGNMENT);
    mapContainer.setLayout(new BoxLayout(mapContainer, BoxLayout.Y_AXIS));
    scrollPane.setViewportView(mapContainer);

    JPanel demoPanel = new JPanel();
    demoPanel.setLayout(null);
    demoPanel.setBackground(Color.YELLOW);
    demoPanel.setSize(50, 100);
    mapContainer.add(demoPanel);

I've researched on this for long, but couldn't find any solutions so far.

Amal Murali
  • 75,622
  • 18
  • 128
  • 150
Victor2748
  • 4,149
  • 13
  • 52
  • 89
  • 1
    1) `scrollPane.setBounds(160, 11, 452, 307);` What is that supposed to achieve? Java GUIs might have to work on a number of platforms, on different screen resolutions & using different PLAFs. As such they are not conducive to exact placement of components. To organize the components for a robust GUI, instead use layout managers, or [combinations of them](http://stackoverflow.com/a/5630271/418556), along with layout padding & borders for [white space](http://stackoverflow.com/q/17874717/418556). .. (cont) – Andrew Thompson May 28 '14 at 00:58
  • .. (cont) 2) `demoPanel.setSize(50, 100);` See [Should I avoid the use of set(Preferred|Maximum|Minimum)Size methods in Java Swing?](http://stackoverflow.com/q/7229226/418556) (Yes.) 3) For better help sooner, post an [MCVE](http://stackoverflow.com/help/mcve) (Minimal Complete and Verifiable Example). – Andrew Thompson May 28 '14 at 00:59
  • My scrollPane is placed on another JPanel with absolute layout, thats why I used .setBounds() method! – Victor2748 May 28 '14 at 01:12
  • 2
    *"My scrollPane is placed on another JPanel with absolute layout"* Then they both need fixing (as opposed to justifying). – Andrew Thompson May 28 '14 at 01:21
  • 2
    `I Researched alot of websites, finding no help,` - all the websites recommend that you should NOT use a null layout. – camickr May 28 '14 at 01:44
  • Also, review the relevant section of the tutorial, cited [here](http://stackoverflow.com/a/18805146/230513). – trashgod May 28 '14 at 05:04

1 Answers1

0

try to check out

setPreferredSize() 
setMaximumSize()
setMinimumSize()

set all 3 to the same value.

If it still doesn't work, you can try to put the panel, of which you are trying to set the size to fixed, inside another panel.

  • See [Should I avoid the use of set(Preferred|Maximum|Minimum)Size methods in Java Swing?](http://stackoverflow.com/q/7229226/418556) (Yes.) – Andrew Thompson May 28 '14 at 10:26