How to add more than one button in Border Layout - java

This is the Picture of Border Layout
I want to add Three buttons at Page_End. is it possible ? and how ?
Note:I cant change layout. else i have to change so much code.

Add a JPanel (or Panel) on PAGE_END
Use some layout on it (again BorderLayout for example).
Add some other components (like buttons) on that JPanel.
** Sample code as requested **
JPanel panel = new JPanel();
JButton button1 = new JButton("Bottom Left");
JButton button2 = new JButton("Bottom Right");
panel.setLayout(new BorderLayout());
panel.add(button1, BorderLayout.LINE_START);
panel.add(button2, BorderLayout.LINE_END);
pane.add(panel, BorderLayout.LINE_END);

Related

How do i use border layouts?

I'm currently working on Layouts in Java. I'm trying to combine different layouts. So i have created a login Screen using Spring Layout, GridLayout and Border Layout.
The MainFrame (JFrame) uses GridLayout. The GridLayout consists of 2 Panel (North Panel and Main Panel). The Main Panel consists of the Jlabel, JTextfield and JButton of which I have no problem of. My problem is in the North Panel which uses Border Layout. It contains a JLabel (lblWelcome). I have been trying to bring the label to the center of the panel using Border Layout but it still aligns to the left. This is the below code:
JLabel lblWelcome = new JLabel("Welcome To The Login Screen");
JPanel northPanel = new JPanel(new BorderLayout());
northPanel.setBackground(Color.green);
northPanel.add(lblWelcome, BorderLayout.CENTER);
Login Screen :
Your JLabel is actually correctly centered, but its text isn't.
Simply change its creation to :
JLabel lblWelcome = new JLabel("Welcome To The Login Screen", SwingConstants.CENTER);

Aligning the JButton and JLabel on JFrame

I am trying to create a JFrame and in which I want the button (Select the Device) to be on top and a text message (Active) which is in the form of Label at the bottom. I am unable to do that and they are all coming up in the same line next to each other.
JFrame f= new JFrame("AutoV");
f.setVisible(true);
f.setSize(600,400);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel p=new JPanel();
p.setBackground(Color.gray);
JButton b=new JButton("Select the Device");
JLabel lab=new JLabel("Active");
lab.setVerticalAlignment(SwingConstants.BOTTOM);
//p.add(b);
p.add(lab);
p.setBorder(BorderFactory.createLineBorder(Color.black));
f.add(p);
Dimension dim1 = Toolkit.getDefaultToolkit().getScreenSize();
f.setLocation(dim1.width/2-f.getSize().width/2, dim1.height/2-f.getSize().height/2);
You should look up different layouts. The default layout of many components is FlowLayout, witch just aligns all elements horizontally, and as small as possible. Setting the panels layout to box or grid layout should do the trick.
https://docs.oracle.com/javase/tutorial/uiswing/layout/visual.html

Can't arrange my elements nicely with borderlayout

I try to order my elements with Borderlayout since Gridlayout makes everything the same size.
What I see is this:
while manually resizing it, I can have the following
Here's part of my code
public InputPanel() {
tfield = new TextField("Search your terms here!");
add(tfield, BorderLayout.PAGE_START);
searchButton = new JButton("Search");
searchButton.addActionListener(this);
add(searchButton, BorderLayout.LINE_START);
clearButton = new JButton("Clear Text");
clearButton.addActionListener(this);
add(clearButton, BorderLayout.LINE_END);
resultsArea = new TextArea();
add(resultsArea, BorderLayout.PAGE_END);
}
It seems like it does not help in arranging. It's just like I have used FlowLayout.
How can I format it properly?
For BorderLayout you should be using NORTH, SOUTH, EAST, WEST and CENTER to place your components. To achive the above layout, you should create one panel that has FLOWLAYOUT, where you add the textfield, seachbutton and clear button. This panel will then be placed inside BorderLayout.NORTH. After this you place the JTextArea inside BorderLayout.NORTH
public InputPanel() {
JPanel topPanel = new JPanel(); // Create a new panel
topPanel.setLayout(FlowLayout()); //Left to right alignment is default for FlowLayout
//Add your textfield and buttons to the panel with flowlayout
tfield = new TextField("Search your terms here!");
topPanel.add(tfield);
searchButton = new JButton("Search");
searchButton.addActionListener(this);
topPanel.add(searchButton);
clearButton = new JButton("Clear Text");
clearButton.addActionListener(this);
topPanel.add(clearButton);
add(topPanel, BorderLayout.NORTH); // Add the panel containing the buttons and textfield in the north
resultsArea = new TextArea();
add(resultsArea, BorderLayout.CENTER); //Add the textarea in the Center
}
This gives me the below appearance:
Seems you have missed GridBagLayout, which is the number one choice for a truly flexible layout manager. With BorderLayout you can also achieve a lot, but only with many levels of nesting, and the code to build it is quite unmanageable.

How to put 6 same sized JButtons into JPanel?

I am trying to put into my JPanel 6 JButtons in such a way that there will be 3 JButtons in one line and beneath another 3 JButtons. Since I know that explicitly JPanel works with Flow Layout Manager, I got such an idea:
JFrame frame = new JFrame();
JPanel panel = new JPanel();
frame.getContentPane().add(panel);
JButton button1 = new JButton();
JButton button2 = new JButton();
JButton button3 = new JButton();
JButton button4 = new JButton();
JButton button5 = new JButton();
JButton button6 = new JButton();
button1.setSize((panel.WIDTH)/3,(panel.HEIGHT)/2);
button2.setSize((panel.WIDTH)/3,(panel.HEIGHT)/2);
button3.setSize((panel.WIDTH)/3,(panel.HEIGHT)/2);
button4.setSize((panel.WIDTH)/3,(panel.HEIGHT)/2);
button5.setSize((panel.WIDTH)/3,(panel.HEIGHT)/2);
button6.setSize((panel.WIDTH)/3,(panel.HEIGHT)/2);
panel.add(button1);
panel.add(button2);
panel.add(button3);
panel.add(button4);
panel.add(button5);
panel.add(button6);
Unfortunately, this does not work, I can not succeed in changing the size of the button. Does anybody have an idea? Thank you so much.
this is job for GridLayout
override getPreferredSize for JPanel
call JFrame.pack(); before JFrame.setVisible(true);
see Initial Thread
You should use a GridLayout for that, if you want more control you can also use GridBagLayout.
If you do not want to use any layout manager, then first make frame.setLayout(null) and also use button1.setBounds(x,y,width,height) for every button instead of setSize(). Lastly frame.setVisible(true).
You should check out some other Layouts, such as the GridLayout, or the GridBagLayout.
This tutorial, from Oracle, might prove useful.
However, when working with Layouts, use setPreferredSize(Dimension size), instead of setSize.
GridLayout is a good idea but if you don't plan to add more components to the panel or only in group of 3, SpringLayout with its makeGrid methods would do the trick too.

JTabbedPane in JPanel?

I have a simple problem when I want to add tabs in my jpanel. The alignment of the tabs get horizontal instead of vertical, wich looks like crap =/.
It looks like this:
If I discard the panel instead and add the tabbedPane directly to the frame, everything works fine.
If you uncomment the three lines of code and remove the getContentPane().add(jtp); you can reproduce my probleme.
working Code:
public class TabbedPane extends JFrame
{
public TabbedPane()
{
setTitle("Tabbed Pane");
setSize(300, 300); // set size so the user can "see" it
JTabbedPane jtp = new JTabbedPane();
// JPanel panel = new JPanel();//uncomment all three lines
// panel.add(jtp);
// getContentPane().add(panel);
getContentPane().add(jtp);//remove me
JPanel jp1 = new JPanel();// This will create the first tab
JPanel jp2 = new JPanel();// This will create the second tab
JLabel label1 = new JLabel();
label1.setText("This is Tab 1");
jp1.add(label1);
jtp.addTab("Tab1", jp1);
jtp.addTab("Tab2", jp2);
JButton test = new JButton("Press");
jp2.add(test);
setVisible(true); // otherwise you won't "see" it
}
public static void main(String[] args)
{
TabbedPane tab = new TabbedPane();
}
}
Thanks a lot!
If I discard the panel instead and add the tabbedPane directly to the frame, everything works fine.
The default layout of JPanel is FlowLayout, which "lets each component assume its natural (preferred) size." The default layout of JFrame is BorderLayout, the CENTER of which ignores preferred size. In either case, invoking setSize() precludes the layout from functioning initially; re-size the frame to see the effect. Instead, use pack(), which "Causes this Window to be sized to fit the preferred size and layouts of its subcomponents."
setDefaultCloseOperation(EXIT_ON_CLOSE);
pack();
setVisible(true); // otherwise you won't "see" it
There are many things I would change in that code, starting with the recommendations of #trashgod. OTOH this is the minimal change needed in order to stretch the tabbed pane to the width/height of the parent container.
// give the panel a layout that will stretch components to available space
JPanel panel = new JPanel(new GridLayout());//uncomment all three lines
panel.add(jtp);
getContentPane().add(panel);
//getContentPane().add(jtp);//remove me
For more details see this answer.
Well firstly you can try this:
JPanel panel = new JPanel();//uncomment all three lines
panel.setLayout(new GridLayout());
JPanel jp1 = new JPanel();// This will create the first tab
JPanel jp2 = new JPanel();// This will create the second tab
JLabel label1 = new JLabel();
label1.setText("This is Tab 1");
jp1.add(label1);
jtp.addTab("Tab1", jp1);
jtp.addTab("Tab2", jp2);
JButton test = new JButton("Press");
jp2.add(test);
getContentPane().add(jtp);
and in the main:
TabbedPane tab = new TabbedPane();
tab.pack();
tab.setVisible(true);
May I suggest using MigLayout to set layouts, it will make your life easier. Hope it helps.
Try GridbagLayout. Once you have mastered it, you can design UI of any sort with this layout.
I agree with prasanth regarding the use of GridBagLayout
I have gone through this problem once and I solved it by adding the JTabbedPaneto the panel via GridBagLayout, make sure you add the JTabbedPane using the ipadx and ipady according to your requirements in your GridBagConstraints object
e.g.
JPanel myPanel=new JPanel();
myPanel.setLayout(new GridBagLayout());
JTabbedPane jTP=new JTabbedPane();
jTP.add("Tab1",new JPanel());//substitute your component instead of "new JPanel"
GridBagConstraints myConstraints=new GridBagConstraints();
myConstraints.ipadx=400;//streches the component being added along x axis - 200 px on both sides
myConstraints.ipady=600;//streches the component being added along y axis - 200 px on both sides
myPanel.add(jTP,myConstraints);
You can adjust both these properties according to what is perfect for your need

Categories