align left and right two JLabels in a "North" BorderLayout - java

I am using BorderLayout for my application.
setLayout(new BorderLayout());
I need to align two JLabels in left and right in the "NORTH" of the JPanel.
Here is my code:
JPanel top = new JPanel();
top.add(topTxtLabel);
top.add(logoutTxtLabel);
add(BorderLayout.PAGE_START, top);
So I need topTxtLabel in left and logoutTxtLabel in right.
I tried to implement Border Layout again to use "WEST" and "EAST", but it didn't worked. Ideas?

Assuming your application consists of a JFrame with BorderLayout you could try this: Set the layout mode of your JPanel again to BorderLayout. Add the panel in the north of the frame. Then add the 2 JLabels in the east and west. You can also replace the JFrame with another JPanel.
import java.awt.BorderLayout;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
public class Main
{
public static void main(String[] args)
{
new Main();
}
Main()
{
JFrame frame = new JFrame("MyFrame");
frame.setLayout(new BorderLayout());
JPanel panel = new JPanel(new BorderLayout());
JLabel left = new JLabel("LEFT");
JLabel right = new JLabel("RIGHT");
JPanel top = new JPanel(new BorderLayout());
top.add(left, BorderLayout.WEST);
top.add(right, BorderLayout.EAST);
panel.add(top, BorderLayout.NORTH);
frame.add(panel, BorderLayout.NORTH);
frame.add(new JLabel("Another dummy Label"), BorderLayout.SOUTH);
frame.pack();
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}

Related

JScrollPane not working correctly when adding JPanel (with FlowLayout) into JScrollPane

I tried to add a JPanel (with FlowLayout) to a JScrollPane but the ScrollBar is not working. I want to place buttons as grid but it places horizontally.
import javax.swing.*;
import java.awt.*;
public class Test {
public static void main(String[] args){
JFrame frame = new JFrame("Test");
frame.setVisible(true);
frame.setSize(1000,500);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel panel = new JPanel();
panel.setLayout(new FlowLayout(FlowLayout.LEFT));
JScrollPane pane = new JScrollPane(panel);
pane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
pane.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
frame.add(pane);
for (int i=0;i<100;i++){
panel.add(new JButton("Label"));
}
}
}
I want to place buttons as grid but it places horizontally.
That's because you do not set the preferred size of the JPanel and because you add the JPanel to a JScrollPane you are effectively giving the JPanel infinite width and FlowLayout will lay out all its components in a single row until it reaches the width limit of the JPanel but because the width is infinite, all the JButtons appear on the same line. Also, because you set the horizontal scrollbar policy to NEVER, there is no way to scroll the JPanel.
You should call method setVisible(true) after you have added all the components.
Note that in the below code I use GridLayout rather than FlowLayout because FlowLayout will not display a grid of JButton. Also note that I call method pack() rather than method setSize().
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
public class Test {
public static void main(String[] args) {
JFrame frame = new JFrame("Test");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel panel = new JPanel();
panel.setLayout(new GridLayout(0, 10, 5, 5));
JScrollPane pane = new JScrollPane(panel);
pane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
pane.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
for (int i=0;i<100;i++){
panel.add(new JButton("Label"));
}
frame.add(pane);
frame.pack();
frame.setVisible(true);
}
}
Here is a screen capture:
Note that, by default, JScrollPane will size itself so as to display all the JButtons. If you want the JScrollPane to only display a few rows, then you need to set its preferred size, for example
pane.setPreferredSize(new Dimension(710, 150));
EDIT
If you insist on using FlowLayout then you need to set the preferred size for both the JPanel and the JScrollPane.
import java.awt.Dimension;
import java.awt.FlowLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
public class Test {
public static void main(String[] args) {
JFrame frame = new JFrame("Test");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel panel = new JPanel();
panel.setPreferredSize(new Dimension(710, 315));
panel.setLayout(new FlowLayout(FlowLayout.LEFT));
for (int i = 0; i < 100; i++) {
panel.add(new JButton("Label"));
}
JScrollPane pane = new JScrollPane(panel);
pane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
pane.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
pane.setPreferredSize(new Dimension(720, 160));
frame.add(pane);
frame.pack();
frame.setVisible(true);
}
}
Here is a screen capture.

Java: Having Multiple Components on a JFrame

I want to have multiple components on a JFrame. I don't want any UI formatting. I just want multiple components to be drawn. My full code is to large to post so here is my JFrame set up.
JPanel jPanel = new JPanel();
jPanel.setLayout(new FlowLayout());
jPanel.add(board.getPieceAt(0,0));
jPanel.add(board);
frame.add(jPanel);
frame.setSize(FRAME_WIDTH, FRAME_HEIGHT);
frame.setLocationRelativeTo(null);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
board and board.getPieceAt(0,0); are JComponents
No combination of frame.add() or adding panels seems to get both to render. It's always the last one added that gets drawn.
This example renders two components. I updated my code above to mimic this example as much as possible and it still results in an empty frame. To me it seems that I'm doing exactly what the example is doing yet I get a different result.
import java.awt.FlowLayout;
import java.awt.BorderLayout;
import javax.swing.*;
public class TestFrameExample {
public static void main(){
JFrame frame = new JFrame("JFrame Example");
JPanel panel = new JPanel();
panel.setLayout(new FlowLayout());
JLabel label = new JLabel("This is a label!");
JButton button = new JButton();
button.setText("Press me");
panel.add(label);
panel.add(button);
frame.add(panel);
frame.setSize(300, 300);
frame.setLocationRelativeTo(null);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
}
How do I get both components to render on the JFrame?

Creating JSplitPane with 3 panels

I would like to create a window, with 3 jPanels, separated by splitPane-s. The left, and the right one should be resizable by the user, and the one in the middle should fill the remaining space.
I've created it, but if I move the first splitPane, then the second one is also moving. And I'm not sure, if I use the best method for what I want.
import java.awt.BorderLayout;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JSplitPane;
public class MyWindow extends JFrame {
public MyWindow() {
this.setLayout(new BorderLayout());
JPanel leftPanel = new JPanel();
JPanel centerPanel = new JPanel();
JPanel centerPanel2 = new JPanel();
JPanel rightPanel = new JPanel();
JSplitPane sp = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT, leftPanel, centerPanel);
JSplitPane sp2 = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT, centerPanel2, rightPanel);
centerPanel.setLayout(new BorderLayout());
this.add(sp, BorderLayout.CENTER);
centerPanel.add(sp2, BorderLayout.CENTER);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setSize(500, 500);
this.setVisible(true);
}
}
What you're doing looks pretty weird to me i.e adding the centerPanel to the split pane, then adding the split pane to the centerPane. Not sure, but I think that the latter negates the former.
All you need to do is add the first split pane to the second split pane.
import java.awt.BorderLayout;
import java.awt.Color;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JSplitPane;
import javax.swing.SwingUtilities;
public class MyWindow extends JFrame {
public MyWindow() {
this.setLayout(new BorderLayout());
JPanel leftPanel = new JPanel();
leftPanel.setBackground(Color.BLUE);
JPanel centerPanel = new JPanel();
centerPanel.setBackground(Color.CYAN);
JPanel rightPanel = new JPanel();
rightPanel.setBackground(Color.GREEN);
JSplitPane sp = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT, leftPanel, centerPanel);
JSplitPane sp2 = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT, sp, rightPanel);
this.add(sp2, BorderLayout.CENTER);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setSize(500, 500);
this.setVisible(true);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable(){
public void run() {
new MyWindow();
}
});
}
}

how can i set the size of two JPanel in one JFrame?

how can i modify the size of the panel in the JFrame
am doing a calculator, the first panel will hold the JTextField which i suppose to be small
the second panel will hold the JButtons which suppose to be bigger
JFrame frame = new JFrame(new GridLayout(2, 1));
JPanel panel1 = new JPanel();
JPanel panel2 = new JPabel();
frame.add(panel1);
frame.add(panel2);
i've been trying to make panel1 smaller than panel2 yet nothing worked!
GridLayout would not be an appropriate choice in this scenario since it ignores the preferred sizes of the components inside the container and displays them all at an equal size instead.
I'd suggest using a BorderLayout. You can find a demonstration and description of that layout manager as well as a few others in Oracle's tutorial, A Visual Guide to Layout Managers.
Here's another example using BorderLayout which might be more relevant to your problem.
import java.awt.*;
import javax.swing.*;
public class Test {
public static void main(String []args){
SwingUtilities.invokeLater(new Runnable() {
public void run() {
JFrame frame = new JFrame();
JPanel panel1 = new JPanel();
JPanel panel2 = new JPanel();
panel1.setBorder(BorderFactory.createTitledBorder("Panel 1"));
panel2.setBorder(BorderFactory.createTitledBorder("Panel 2"));
frame.add(panel1, BorderLayout.NORTH);
frame.add(panel2, BorderLayout.CENTER);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(new Dimension(200, 200));
frame.setVisible(true);
}
});
}
}
Edit: The JFrame's content pane uses a BorderLayout by default, hence the absence of a call to setLayout. Source

How to divide a JPanel into left and right segments?

I want to divide a JPanel into left and right segments. How do I do that ? After that, I will place panels in the left and right half.
If there is no need to resize them, you can simply use a BorderLayout and insert your panels in the BorderLayout.EAST and BorderLayout.WEST:
JPanel panel = new JPanel( new BorderLayout() );
panel.add( leftPanel, BorderLayout.WEST );
panel.add( rightPanel, BorderLayout.EAST );
You could also consider using a JSplitPane which allows to resize the UI:
JSplitPane pane = new JSplitPane( JSplitPane.HORIZONTAL_SPLIT,
leftPanel, rightPanel );
It is very easy if you use a JSPlitPane.
there are two ways
use GridLayout
use JSplitPane (with hidden divider)
JPanel panel = new JPanel(new BorderLayout());
panel.add(c1, BorderLayout.WEST);
panel.add(c2, BorderLayout.EAST);
JPanel panel = new JPanel(new GridLayout(1, 2));
panel.add(c1);
panel.add(c2);
Use a JSplitPane or a GridLayout
You can use SplitPane as Costis Aivalis suggested.
Or
Use Border Layout Manager on JPanel.
Put your left side components in WEST side and put your right side components in EAST side of layout manager.
JPanel panel = new JPanel(new BorderLayout());
panel.add(c1, BorderLayout.WEST);
panel.add(c2, BorderLayout.EAST);
JPanel leftPanel = new JPanel();
JPanel rightPanel = new JPanel();
JSplitPane pane = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT, leftPanel, rightPanel);
JPanel example = new JPanel(new GridLayout(1,2));
example.add(p1);
example.add(p2);
import java.awt.BorderLayout;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JSplitPane;
public class Display{
JFrame frame=new JFrame("Drawing");
North north;
South south;
East east;
West west;
Center center;
public int width=600,height=600;
public Display() {
// TODO Auto-generated constructor stub
frame.setSize(width,width);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLocationRelativeTo(null);
frame.setResizable(true);
north=new North(frame);
south=new South(frame);
east=new East(frame);
west=new West(frame);
center=new Center(frame);
frame.setLayout(new BorderLayout());
JSplitPane pane2=new JSplitPane(JSplitPane.VERTICAL_SPLIT,west,east);
frame.add(pane2);
frame.setVisible(true);
}
}

Categories