How to stack multiple buttons on top of each other? The buttons should be positioned at the bottom of the application frame. I am trying to find a combination but with no luck, for example:
final JPanel content = new JPanel(new BorderLayout());
content.add(chartPanel);
content.add(button1, BorderLayout.SOUTH);
content.add(button2, BorderLayout.PAGE_END);
setContentPane(content);
The buttons just overlap.
final JPanel content = new JPanel(new BorderLayout());
content.add(chartPanel);
final JPanel buttonPanel = new JPanel(new BorderLayout());
buttonPanel.add(button1, BorderLayout.NORTH);
buttonPanel.add(button2, BorderLayout.SOUTH);
content.add(buttonPanel, BorderLayout.SOUTH);
setContentPane(content);
Related
I have main JPanel which is Borderlayout with added 4 JPANELS: NORTH(Green), WEST(Red), CENTER(Gray), SOUTH(Blue). I want to reduce width size of WEST(Red) Jpanel, or increase width size of Center(Grey) Jpanel.
Screenshot:
Here is my code:
frame = new JFrame("FreshPos baza podataka");
frame.setExtendedState(JFrame.MAXIMIZED_BOTH);
// Main paneel
JPanel panel = new JPanel();
panel.setLayout(new BorderLayout());
panel.setBorder( BorderFactory.createEmptyBorder(10,10,10,10) );
frame.getContentPane().add(panel);
//West panel;
JPanel panelWest = new JPanel(new GridLayout(14,0,0,2));
panelWest.setPreferredSize(new Dimension(300, 300));
panelWest.setBorder( BorderFactory.createEmptyBorder(100,0,0,0) );
panel.add(panelWest, BorderLayout.WEST);
panelWest.setBackground(Color.red);
for (int i = 0; i < MAX_TABLES; i++) {
buttonsTables[i] = new JButton(tables[i]);
buttonsTables[i].setMaximumSize(new Dimension(Integer.MAX_VALUE, buttonsTables[i].getMinimumSize().height));
panelWest.add(buttonsTables[i]);
panelWest.add(Box.createVerticalStrut(10));
}
//South panel;
JPanel southPanel = new JPanel(); // Donji layout za dugmice
southPanel.setBorder( BorderFactory.createEmptyBorder(20,0,0,0) );
panel.add(southPanel, BorderLayout.SOUTH);
southPanel.setBackground(Color.BLUE);
JButton buttonDodaj = new JButton("Dodaj");
southPanel.add(buttonDodaj);
JButton buttonIzmeni = new JButton("Izmeni");
southPanel.add(buttonIzmeni);
JButton butonObrisi = new JButton("Obrisi");
southPanel.add(butonObrisi);
//North panel;
JPanel northPanel = new JPanel(); // Donji layout za dugmice
northPanel.setBorder( BorderFactory.createEmptyBorder(0,10,0,0) );
panel.add(northPanel, BorderLayout.NORTH);
northPanel.setBackground(Color.green);
JButton buttonImport = new JButton("Importuj fajl");
buttonImport.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
importActionPerformed(evt);
}
});
northPanel.add(buttonImport, BorderLayout.WEST);
JButton ButtonRecord = new JButton("Snimi fajl");
northPanel.add(ButtonRecord, BorderLayout.WEST);
// Central panel
JPanel centerPanel = new JPanel(new BorderLayout());
centerPanel.setBackground(Color.GRAY);
panel.add(centerPanel, BorderLayout.CENTER);
I want to reduce width size of WEST(Red) Jpanel
panelWest.setBorder( BorderFactory.createEmptyBorder(100,0,0,0) );
So why is the width of your Border so large?
A Border is for "extra" space around the components.
So the width of your panel is the width of the buttons plus the width of the border.
Edit:
panelWest.setPreferredSize(new Dimension(300, 300));
Don't hardcode a preferred size. The layout manager will calculate the size based on the above logic. Get rid of that statement.
Edit 2:
// buttonsTables[i].setMaximumSize(new Dimension(Integer.MAX_VALUE, buttonsTables[i].getMinimumSize().height));
Get rid of any logic that attempts to control the size of a component. The point of using layout managers is to let the layout manager do the size calcualtions.
So for your buttons panel you need to nest panels to prevent the buttons from taking all the space.
You can do something like:
JPanel wrapper = new JPanel();
wrapper.add(buttonsPanel);
...
//panel.add(panelWest, BorderLayout.WEST);
panel.add(wrapper, BorderLayout.WEST);
By default a JPanel uses a FlowLayout which will respect the preferred size of any component added to it.
Another option is to use a GridBagLayout with the wrapper panel. By default the panel will then be displayed in the "center" of the available space. So it will be vertically centered and you won't need the EmptyBorder.
I hava an JButton and want to position to the bottom left of an JPanel. This is my code:
panel = new JPanel(new FlowLayout(FlowLayout.LEFT));
panel.setPreferredSize(new Dimension(130, 300));
panel.add(jlabel1);
panel.add(jlabel2);
panel.add(button, BorderLayout.SOUTH);
Later in the code, I update the Jlabels (if it matters):
panel.remove(jlabel1);
panel.remove(jlabel2);
//Some other code
panel.add(jlabel1)
panel.add(jlabel2)
Through all this, I want the JButton to stay in the bottom of the JPanel. How can I fix this? Nothing happens with the BorderLayout.SOUTH. Thanks.
The short answer is that BorderLayout.SOUTH isn't doing anything because the panel to which you are adding button doesn't have a BorderLayout.
Check out the Java Tutorials section on layout managers for a better explanation
https://docs.oracle.com/javase/tutorial/uiswing/layout/index.html
Meanwhile, this code (made up on-the-spot and completely untested) should give you some ideas about the direction you need to go:
panel = new JPanel(new BorderLayout());
panel.setPreferredSize(new Dimension(130, 300));
JPanel buttonPanel = new JPanel(new FlowLayout(FlowLayout.LEFT));
buttonPanel.add(button);
panel.add(buttonPanel, BorderLayout.SOUTH);
JPanel labelPanel = newJPanel(new FlowLayout(FlowLayout.LEFT));
labelPanel.add(jLabel1);
labelPanel.add(jLabel2);
panel.add(labelPanel, BorderLayout.CENTER));
JPanel panel = new JPanel();
panel.setLayout(new BoxLayout(panel, BoxLayout.PAGE_AXIS));
JPanel topPanel = new JPanel(new FlowLayout());
.....
JPanel centrePanel = new JPanel(new FlowLayout(10, 0));
........
JScrollPane scrollPane = new JScrollPane(table);
scrollPane.setPreferredSize(new Dimension(100, 160));
centrePanel.add(glListScrollPane);
........
........
JPanel bottomPanel = new JPanel();
bottomPanel.setLayout(new GridLayout(0, 2));
......
........
panel.add(topPanel, BorderLayout.CENTER);
panel.add(centrePanel, BorderLayout.CENTER);
panel.add(bottomPanel, BorderLayout.CENTER);
frame.add(panel);
frame.add(standardButtonPanel);
public void lockScreen(boolean editable) {
standardButtonPanel.button1.setVisible(editable);
......
}
When doing edit and un-edit. the panel is changing its position a little bit.
I have used BoxLayout as I wanted to have the components have there own size and users can resize the screen also.
Is there any other approach? Where I can fix the layout problem?
Instead of using setVisible, try using setEnabled as it dosent hide the button (hence does not affect the UI) but makes it so that the end-user cannot press the button.
I am trying to use a nested JPanel, which I can then reuse in different parts of my application, eg navigation bar at the top of the page. I am having trouble setting the orientation of the items, eg I want the button to be above the textfield.
If I create them individually and add them directly to the JPanel they come out one on top of the other as inteneded, as below:
final JFrame frame = new JFrame("Nested Layout Example");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
final JPanel gui = new JPanel(new BorderLayout(5,5));
gui.setBorder( new TitledBorder("BorderLayout(5,5)") );
JButton button = new JButton("Button");
JButton button1 = new JButton("Button1");
gui.add(button, BorderLayout.NORTH);
gui.add(button1, BorderLayout.SOUTH);
frame.setContentPane(gui);
frame.pack();
frame.setLocationRelativeTo(null);
try {
// 1.6+
frame.setLocationByPlatform(true);
frame.setMinimumSize(frame.getSize());
} catch(Throwable ignoreAndContinue) {
}
frame.setVisible(true);
However, if I create a nested JPanel and put it inside another JPanel, so I can reuse it, they come out side by side, as below:
final JFrame frame = new JFrame("Nested Layout Example");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
final JPanel gui = new JPanel(new BorderLayout(5,5));
gui.setBorder( new TitledBorder("BorderLayout(5,5)") );
JPanel container = new JPanel();
JButton button = new JButton("Button");
JButton button1 = new JButton("Button1");
container.add(button, BorderLayout.NORTH);
container.add(button1, BorderLayout.SOUTH);
gui.add(container);
frame.setContentPane(gui);
frame.pack();
frame.setLocationRelativeTo(null);
try {
// 1.6+
frame.setLocationByPlatform(true);
frame.setMinimumSize(frame.getSize());
} catch(Throwable ignoreAndContinue) {
}
frame.setVisible(true);
I have tried setting the componenetOrientation,
container.setComponentOrientation(ComponentOrientation.);
but there is no option for vertical
I have tried setting the componenetOrientation
Please note the problem has nothing to do with component orientation: it's a layout manager problem as explained below.
However, if I create a nested JPanel and put it inside another JPanel, so I can reuse it, they come out side by side
Here:
JPanel container = new JPanel();
...
container.add(button, BorderLayout.NORTH);
container.add(button1, BorderLayout.SOUTH);
The default layout manager for panels is FlowLayout and it will ignore BorderLayout constraints. You'll have to set BorderLayout as layout manager not only to gui panel but to container panel as well.
JPanel container = new JPanel(new BorderLayout());
...
container.add(button, BorderLayout.NORTH);
container.add(button1, BorderLayout.SOUTH);
Try setting a layout for your JPanel. There are many Layouts available in the package java.awt. Some of them are BorderLayout, GridBagLayout, CardLayout, FlowLayout, GridLayout.
I have just added One line to your code, and now, it puts the buttons in the way you want:
final JFrame frame = new JFrame("Nested Layout Example");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
final JPanel gui = new JPanel(new BorderLayout(5,5));
gui.setBorder( new TitledBorder("BorderLayout(5,5)") );
JPanel container = new JPanel();
container.setLayout(new GridLayout(2,1)); // This is the line that I have added.
JButton button = new JButton("Button");
JButton button1 = new JButton("Button1");
container.add(button, BorderLayout.NORTH);
container.add(button1, BorderLayout.SOUTH);
gui.add(container);
frame.setContentPane(gui);
frame.pack();
frame.setLocationRelativeTo(null);
try {
// 1.6+
frame.setLocationByPlatform(true);
frame.setMinimumSize(frame.getSize());
} catch(Throwable ignoreAndContinue) {
}
frame.setVisible(true);
If you want to add more buttons, you can edit that line. The first Parameter of the constructor method of GridLayout is the number of vertical columns, and the second is the number of horizontal columns.
I have a frame with 4 JPanels and 1 JScrollPane, the 4 panels are in border layout north, east, south, west and the scrollpane in the center.
I have been trying to get the pack method for a frame functioning but when run you just get the title bar of the window.
Any Ideas?
JFrame conFrame;
JPanel panel1;
JPanel panel2;
JPanel panel3;
JPanel panel4;
JScrollPane listPane;
JList list;
Object namesAr[];
...
...
...
namesAr= namesA.toArray();
list = new JList(namesAr);
list.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
list.setLayoutOrientation(JList.HORIZONTAL_WRAP);
list.setVisibleRowCount(-3);
list.addListSelectionListener(this);
listPane = new JScrollPane(list);
panel1 = new JPanel();
panel2 = new JPanel();
panel3 = new JPanel();
panel4 = new JPanel();
conFrame.setLayout(new BorderLayout());
panel1.setPreferredSize(new Dimension(100, 100));
panel2.setPreferredSize(new Dimension(100, 100));
panel3.setPreferredSize(new Dimension(100, 100));
panel4.setPreferredSize(new Dimension(100, 100));
panel1.setBackground(Color.red);
panel2.setBackground(Color.red);
panel3.setBackground(Color.red);
panel4.setBackground(Color.red);
conFrame.pack();
conFrame.add(panel1, BorderLayout.NORTH);
conFrame.add(panel2, BorderLayout.EAST);
conFrame.add(panel3, BorderLayout.SOUTH);
conFrame.add(panel4, BorderLayout.WEST);
conFrame.add(listPane, BorderLayout.CENTER);
conFrame.setVisible(true);
You need to add the panels to the frame "before" you do the pack() otherwise there is nothing to pack.
Also, the default layout for a frame is the BorderLayout.