3

I would like to achieve the below layout.

enter image description here

There are 6 panels. The 4 buttons at the top are one panel, and the 3 buttons at the right side of the image are also in one panel. Apart from those two there are 4 other panels as indicated by the borders. I tried the below code but displays everything in a scattered way.

mainPanel.add(topToolBarPanel,BorderLayout.PAGE_START);
mainPanel.add(lefsideToolBarPanel,BorderLayout.LINE_START);
mainPanel.add(descriptionPanel,BorderLayout.LEFT);
mainPanel.add(mapPanel,BorderLayout.CENTER);
mainPanel.add(propertiesPanel,BorderLayout.EAST);
mainPanel.add(tablePanel,BorderLayout.PAGE_END);

How can I achieve the design as shown in the image? I need all the panels to be arranged inside that mainPanel. I cannot use null layout though. Please advice.

After trashgod's answer :

    JPanel gridPanel =  new JPanel(new GridLayout(1, 0));
    gridPanel.add(jInternalFrame1);
    gridPanel.add(descriptionPanel);
    mainPanel.add(gridPanel, BorderLayout.LINE_START);
    mainPanel.add(topToolBarPanel,BorderLayout.PAGE_START);
    mainPanel.add(tablePanel,BorderLayout.PAGE_END);
    mainPanel.add(mapPanel,BorderLayout.CENTER);
    mainPanel.add(PropertiesPanel,BorderLayout.LINE_END);

What I get :

enter image description here

AnOldSoul
  • 4,017
  • 12
  • 57
  • 118
  • You can put 2 things LEFT(EAST) whatever wrap the map panel around another panel where u put the description panel left or use a gridpane that fills the whole frame – Marcel Apr 23 '16 at 10:58
  • when I put two things left, it puts them on another. Grid pane doesn't arrange it this way as well – AnOldSoul Apr 23 '16 at 11:00
  • u don't get it ... – Marcel Apr 23 '16 at 11:02
  • Can you give me an answer in code? Would appreciate it much :) – AnOldSoul Apr 23 '16 at 11:04
  • Here is an image for you https://i.gyazo.com/3f0c82111277b5f459b5a425999873e4.png And yes, i made it with paint thats why it is ugly – Marcel Apr 23 '16 at 11:05
  • I dont understand how to do that with a grid layout! row i doesnt have 4 columns! can you show some example code? – AnOldSoul Apr 23 '16 at 11:24

2 Answers2

4

Add lefsideToolBarPanel and descriptionPanel to a panel having GridLayout; add the new panel to the BorderLayout.

Panel p  new Panel(new GridLayout(1, 0));
p.add(lefsideToolBarPanel);
p.add(descriptionPanel);
//mainPanel.add(lefsideToolBarPanel, BorderLayout.LINE_START);
//mainPanel.add(descriptionPanel, BorderLayout.LEFT);
mainPanel.add(p, BorderLayout.LINE_START);

There is no BorderLayout.LEFT. See also A Visual Guide to Layout Managers.

Addendum: Your updated question shows elements of topToolBarPanel, which should be added to PAGE_START, rather than LINE_START.

//mainPanel.add(topToolBarPanel,BorderLayout.LINE_START);
mainPanel.add(topToolBarPanel,BorderLayout. PAGE_START);

image

The width of the propertiesPanel and height of the tablePanel need to be increased. I used setSize()

For the propertiesPanel, you can override getPreferredSize(), as discussed here. For the tablePanel, override getPreferredScrollableViewportSize() to customize the size of the table's enclosing JScrollPane, for example.

Community
  • 1
  • 1
trashgod
  • 203,806
  • 29
  • 246
  • 1,045
  • It doesn't work as well. I edited the question with the code I tried as you suggested and the output i got :( – AnOldSoul Apr 23 '16 at 11:18
  • i've edited the question again. This time its close. But still not the exactly intended design. :( – AnOldSoul Apr 23 '16 at 11:39
  • `GridLayout(3, 0)` means "_three_ rows and as many columns as necessary;" `GridLayout(1, 0)` means "_one_ row and as many columns as necessary." See [nested layouts](http://stackoverflow.com/a/5630271/230513) for more. – trashgod Apr 23 '16 at 11:47
  • I edited the question again with the image I get. Now the placement is accurate. But the width of the properties panel and height of the tablePanel needs to be increased. I used setSize but it doesn't have any effect. Is it not possible to increase the height and width inside the layout? – AnOldSoul Apr 23 '16 at 11:55
0

I suggest using a JLabel as your "layout" to use exact positioning of yout objects with setBounds(x, y, width, height). It would look similar to this :

JButton button = new JButton("Text or Image");
JLabel backgr = new JLabel();
JFrame frame = new JFrame("JLabel as Layout");

button.setBounds(100, 200, 340, 40);

backgr.add(button);

frame.add(backgr);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(600, 600);
frame.setLocation(40, 40);
frame.validate();
frame.setVisible(true);

I know that this is just a quick example for you, but I think it should do for explanation... so just add everything on the backgr JLabeland your good to go. Quick and dirty example but the a way to go.

MrMechanik
  • 73
  • 1
  • 5