Border layout doesn't work as intended - java

I would like to achieve the below layout.
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 :

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);
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.

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.

Related

Using JScrollPane on JLabel in Java

Somehow I don't the scrollpane to show up. What do I need to change?
bigP = new JLabel();
setLayout(new BorderLayout());
JPanel helper = new JPanel(new FlowLayout());
helper.add(bigP);
helper.setPreferredSize(new Dimension(500,600));
helper.add(new JScrollPane(bigP, JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,
JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS));
picPane = new JPanel(new BorderLayout());
picPane.add(helper,BorderLayout.CENTER);
picPane.setMaximumSize(new Dimension(500, 600));
picPane.setVisible(true);
add(picPane, BorderLayout.CENTER);
After an image is chosen this line is called:
bigP.setIcon(img);
I figured out that I most certainly will need the helper-panel as the BorderLayout would only take one component (as far as I understood).
Unfortunately my scrollpane won't show up at all though the picture does.
helper.setPreferredSize(new Dimension(500,600));
Don't hardcode a preferred size. The panel will determine its own preferred size based on the components added to the panel.
JPanel helper = new JPanel(new FlowLayout());
helper.add(bigP);
sc = new JScrollPane(bigP,JScrollPane
Also a component can only have a single parent. In the above code you attempt to add "bigP" to "helper". But then in the next statement you add it to the scrollpane, so "bigP" is removed from the "helper" panel and will only appear in the scrollpane.
//pic.add(bigP,BorderLayout.CENTER);
pic.add(helper,BorderLayout.CENTER);
Also you never add the scroll pane to the "pic" panel. The code should be:
//pic.add(bigP,BorderLayout.CENTER);
//pic.add(helper,BorderLayout.CENTER);
pic.add(sc, BorderLayout.CENTER);
So now you should have a structure that looks like:
- pic
- sc
- bigP
It would also help if you use more descriptive names so everybody knows what those variable are.

Java GUI development manually

I am trying to make a panel to have 4 items on top. these are a JLabel, JTextField, JLabel and JTextField.
In the center I need a JTextArea and to the left of it a JList that is scrollable.
On the bottom I need 3 buttons.
What would be the best layout manager for this and how should I go about it?
Would having just 3 columns be a good idea?
Heres what I have so far:
JPanel panel = new JPanel();
JTextField IDLabel = new JLabel("ID: ");
IDLabel.setBounds(10, 10, 80, 25);
panel.add(IDLabel);
JTextArea IDText = new JTextField(5);
IDText.setBounds(100, 10, 160, 25);
panel.add(IDText);
JLabel TitleLabel = new JLabel("Title: ");
TitleLabel.setBounds(10, 10, 80, 25);
panel.add(TitleLabel);
JTextField TitleText = new JTextField(10);
TitleText.setBounds(100, 10, 160, 25);
panel.add(TitleText);
JList list = new JList(new String[]{"test1", "test22"});
list.setFixedCellWidth(150);
list.setFixedCellHeight(50);
list.setFont(new Font("Serif",Font.BOLD,16));
list.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
panel.add(list);
JTextArea BodyArea = new JTextArea();
BodyArea.setSize(200, 200);
BodyArea.setText("Test area");
panel.add(BodyArea);
You will mostly likely need to use a combination of layouts (AKA compound layouts), for example
North Panel
Create a JPanel and assign it a FlowLayout or GridBagLayout or GridLayout depending on what you want to achieve.
Add the JLabel, JTextField, JLabel, JTextField to it.
Center Panel
Create a JPanel with BorderLayout. Add the JTextArea to the CENTER position and the JList to the WEST position
South Panel
Craete a JPanel with a FlowLayout or GridBagLayout or GridLayout depending on what you want to achieve.
Add the buttons to it.
Putting it together
Create a JPanel with a BorderLayout, add the "north" panel to the NORTH position, the "center" panel to the CENTER position and the "south" panel to the SOUTH position
You could use a single container and a GridBagLayout, but that's a lot of work.
Take a look at Laying Out Components Within a Container for more details
Maybe start with a BorderLayout for the main layout. Then you can add components to the PAGE_START (NORTH), LINE_START (WEST) and CENTER and PAGE_END (SOUTH). Check out the section from the Swing tutorial on Using Layout Manager, for more information and examples.
Of course you would also use panels when you want to display multiple components in a single area. So your buttons would first be added to panels.
Also, follow standard naming conventions. Variable names should NOT start with an upper case character( ie. BodyArea, TitleText). You got variable like (panel, list) correct so be consistent.

center a panel in a box layout row in java

I have this code below to create a page inside of a tab.
I want each layout in one row of the overall box layout but i want the elements to stay in their original size and not expand to fill the width of the overall window. does anyone know what lines of code i need to change or what is the best way of doing this?! The image attached shows what it looks like at the moment
public void createPage4() {
panel4 = new JPanel();
panel4.setLayout(new BoxLayout(panel4, BoxLayout.Y_AXIS));
navigatePanel = new JPanel();
navigatePanel.setLayout(new BoxLayout(navigatePanel, BoxLayout.X_AXIS));
previousButton.setText("Previous");
previousButton.setEnabled(false);
navigatePanel.add(previousButton);
navigatePanel.add(Box.createHorizontalStrut(10));
indexTextField.setHorizontalAlignment(JTextField.CENTER);
navigatePanel.add(indexTextField);
navigatePanel.add(Box.createHorizontalStrut(10));
ofLabel.setText("of");
navigatePanel.add(ofLabel);
navigatePanel.add(Box.createHorizontalStrut(10));
maxTextField.setHorizontalAlignment(JTextField.CENTER);
maxTextField.setEditable(false);
navigatePanel.add(maxTextField);
navigatePanel.add(Box.createHorizontalStrut(10));
nextButton.setText("Next");
nextButton.setEnabled(false);
navigatePanel.add(nextButton);
panel4.add(navigatePanel);
displayPanel = new JPanel();
displayPanel.setLayout(new GridLayout(5, 2, 4, 4));
firstNameLabel.setText("First Name:");
displayPanel.add(firstNameLabel);
displayPanel.add(firstNameTextField);
lastNameLabel.setText("Last Name:");
displayPanel.add(lastNameLabel);
displayPanel.add(lastNameTextField);
panel4.add(displayPanel);
}
image
BoxLayout accepting Min, Max and PreferredSize that came from JComponents
I want each layout in one row of the overall box layout but i want the elements to stay in their original size and not expand to fill the width of the overall window
I'd be to use proper LayoutManager, FlowLayout accepting only PreferredSize, and/or all JComponents layed by GridBagLayout without defininitions of GridBagConstraints stays unchanged on containers resize
doesn't make me sence (my view) for why reason (sure this is your job), but for better help sooner post an SSCCE
The easiest way is to add your panel4 to an other panel that uses GridBagLayout and then add that panel to the container. Then it will be centered and nothing will stretch on resize.
JPanel centeredPanel = new JPanel(new GridBagLayout());
centeredPanel.add(panel4); // add this panel to the container
You should also construct the textfields with a specified number of columns, like
indexTextField = new JTextField(20);

Getting appropriate dimensions of a Swing component

I was trying out JLayeredPane. So, in the following code, I created a JLayeredPane and a JLabel. I added the label to the layered pane, which I added to a JPanel. This panel was then added to a JFrame.
public static void main(String[] args) {
frame = new JFrame("LayeredPane Example");
frame.setPreferredSize(new Dimension(500,500));
layeredPane = new JLayeredPane();
layeredPane.setPreferredSize(new Dimension(400, 400));
JLabel label = new JLabel("Label on LayeredPane");
label.setLocation(200, 200);
System.out.println("Width " + label.getWidth() );
label.setBounds(20, 20, 400, 40);
layeredPane.add(label);
layeredPane.setLayer(label, 10, 1);
frame.setLayout(new BorderLayout());
JPanel panel = new JPanel();
panel.add(layeredPane);
frame.add(panel);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.pack();
frame.setVisible(true);
}
Now the problem is that if I do not have the statement label.setBounds(20, 20, 400, 40);, then the label does not appear on the layered pane. This raises two questions:
Why is setBounds so important?
Probably a part of my previous questions answer, the label had an initial height and width of 0 before setting bounds, which might be the reason setBounds is important. In that case, I want to know how can I determine appropriate bounds for a Swing component when I am adding it to a JLayeredPane. (If my bounds are less than the appropriate size of the component, the component will appear hidden)
Edit:
The first question was answered earlier in more detail here.
Regarding:
Why is setBounds so important: A JLayeredPane uses essentially a null layout, and whenever you use null layouts, you the coder are completely responsible for both the size and position of the components that you add. That's simply the rules of this layout.
How to determine the appropriate bounds: One thing I've done is simply to let the component tell me what its preferredSize is and then use it for its size:
myJLabel.setSize(myJLabel.getPreferredSize());
Another thing I've done is to use non-opaque JPanels for each layer of my JLayeredPane, give these JPanels appropriate layouts and then add my components to the appropriate layer JPanel. I then use a listener to be sure that the layer JPanel's size matches that of its JLayeredPane container.

Java swing layouts and/or labels not in order

I have got a window that should display the following:
JLablel "Have you used GUI before?" on the top, centered
two radioButtons "Yes" and "No" below it, somewhat in the center, a little bit towards the left
a JButton "NEXT" in the bottom-right corner
All three elements should have green font and darkGrey background.
The problem is that the window which is showing up, does not look like I would like it to.
And this is my code:
yesButton = new JRadioButton(yes);
//yesButton.setMnemonic(KeyEvent.VK_B); // doesn't work?
yesButton.setActionCommand(yes);
noButton = new JRadioButton(no);
// noButton.setMnemonic(KeyEvent.VK_C); // doesn't work?
noButton.setActionCommand(no);
ButtonGroup group = new ButtonGroup();
group.add(yesButton);
group.add(noButton);
nextButton = new JButton("NEXT");
nextButton.setActionCommand(next);
yesButton.addActionListener(this);
noButton.addActionListener(this);
nextButton.addActionListener(this);
JPanel radioPanel = new JPanel(new GridLayout(0, 1));
radioPanel.add(yesButton);
radioPanel.add(noButton);
add(radioPanel, BorderLayout.WEST);
// setBorder(BorderFactory.createEmptyBorder(20,20,20,20));
// radioPanel.setBorder(new EmptyBorder(250, 250, 20, 20));
// there is no difference between the above two, right?
String q = "Have you used GUI before?";
JPanel area = new JPanel(new BorderLayout());
area.setBackground(Color.darkGray);
JLabel textLabel2 = new JLabel("<html><div style=\"text-align: center;\">"
+ q + "</html>", SwingConstants.CENTER);
textLabel2.setForeground(Color.green);
Font font2 = new Font("SansSerif", Font.PLAIN, 30);
textLabel2.setFont(font2);
//textLabel2.setBorder(new EmptyBorder(0, 0, 250, 0)); //top, left, bottom, right
area.add(textLabel2, BorderLayout.NORTH);
area.add(nextButton, BorderLayout.EAST);
add(area, BorderLayout.CENTER);
I feel I'm nearly there, thanks for any help!
--EDIT--
A screenshot:
You need to use nested panels.
for the BorderLayout.NORTH you can add the JLabel directly. You will need to set the horizontal text alignment to center.
for the radio buttons you can create a JPanel with a FlowLayout and then add the buttons to the panel and add the panel to the CENTER.
for the button you add the button to a panel using a FlowLayout that is right aligned, then add the panel to the SOUTH.
There are other choices. You could also use a Vertical BoxLayout as the layout of the main panel and then add child panels to it.
You won't be able to get much control with just a BorderLayout. Try something else like MigLayout or one of the other many many layout managers Java has (GridBag, Box, etc).
In MigLayout it would look something like:
area.setLayout(new MigLayout("fill"));
area.add(textLabel2, "wrap");
area.add(radioPanel, "wrap");
area.add(nextButton, "tag right");

Categories