Which is the most suitable layout for this interface? I have tried FlowLayout, but can't get the SUBMIT button place in the correct position.
I recommend miglayout, it is very easy to use and it always handles, what I am throwing at it.
In this case, I would create two panels. The first panel has a "wrap 3" constraint as a parameter for the miglayout constructor, the second only has one button, which is added with a "alignx center, aligny center" constraint.
Here is an example:
public class TestApplet extends JApplet{
#Override
public void init() {
super.init();
setSize(400, 300);
setLayout(new MigLayout("fill, insets 0"));
JPanel leftPanel = new JPanel(new MigLayout("wrap 3"));
for (int i = 0; i < 9; i++) {
leftPanel.add(new JButton(""+i));
}
add(leftPanel);
JPanel rightPanel = new JPanel(new MigLayout());
rightPanel.add(new JButton("submit"), "alignx center, aligny center");
add(rightPanel);
}
}
You only have to sort the numbers on the buttons by your needs.
First Grid Layout with two columns.
in first column add panal1 and in second add submit with BorderLayout.CENTER.
In that panal1 you can use 3x3 grid layout.
I tried this out on a JFrame and you can do the same in your applet.
Create a Grid of One row two columns.
Create a Box Layout on the second Panel with Page Axis and then ensure you have vertical glues with button in center.
Create a Grid of 3x3 and add buttons.
Code sample:
JFrame frame = new JFrame();
frame.setLayout(new GridLayout(1, 2));
JPanel leftPanel = new JPanel(new GridLayout(3, 3));
for(int i=0;i<9;i++)
{
JButton button = new JButton();
button.setText(i+"");
leftPanel.add(button);
}
frame.add(leftPanel);
JPanel rightPanel = new JPanel();
BoxLayout layout = new BoxLayout(rightPanel, BoxLayout.PAGE_AXIS);
rightPanel.setLayout(layout);
JButton button = new JButton("Submit");
button.setAlignmentX(
Component.CENTER_ALIGNMENT);
rightPanel.add(Box.createVerticalGlue()); //Ensure this order
rightPanel.add(button);
rightPanel.add(Box.createVerticalGlue());
frame.add(rightPanel);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
Related
I am creating a GUI using GridLayout to arrange multiple panels that are using FlowLayout. I am using pack to determine the size of the frame. If I have only a TextArea then it sizes correctly. If I have only other components it sizes correctly. But if I use both p1 and p2 in the code below the panel with the buttons does not size properly. The test code I am using is below. If I use only p1 or p2 it sizes properly, but not with both: Here is the image I am getting. I don't want all that extra space between the buttons and the textarea.
If I do textarea alone it sizes properly.:
public class GUIPractice extends JFrame
{
//declare GUI objects
JPanel panel;
JPanel p1, p2;
JButton btnAdd, btnShow;
JScrollPane js;
JTextArea taOutput;
//constructor
public GUIPractice()
{
//give frame a title
setTitle("GUI Practice");
panel =new JPanel(new GridLayout(0, 1));
btnAdd = new JButton("Add");
btnShow = new JButton("Show");
taOutput = new JTextArea(10, 20);
js = new JScrollPane(taOutput, JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED,
JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
p1 = new JPanel();
p1.add(btnAdd);
p1.add(btnShow);
p2 = new JPanel();
p2.add(js);
panel.add(p1);
panel.add(p2);
//add the panel to the frame
add(panel);
}
public static void main(String[] args)
{
GUIPractice frame = new GUIPractice();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.pack();
frame.setVisible(true);
}
}
It sizes correctly. The GridLayout means that all cells will be the same size. Since the text area is bigger than the buttons, you see extra space in the panel containing the buttons.
I would suggest you just use the default BorderLayout of the frame.
There is no need for your "panel" using the GridLayout.
You just use:
add(p1, BorderLayout.PAGE_START);
add(p2, BorderLayout.CENTER);
Now the p1 and p2 will be the proper height.
Also, there really is no need for "p2". You can just add the scroll pane directly to the frame.
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'm not able to set the height of a JComboBox, I searched in the web but didn't found the right answer.
As you can see from the image below the combo box fills nearly all the panel height and I'd like it to have a smaller height.
I tried setting size with getPreferredSize() method but it didn't work, it worked only for other components like the button.
My code
private JComponent firstPanel()
{
JPanel panel = new JPanel();
panel.setLayout(new BorderLayout());
JLabel spesaAnnuaSingola = new JLabel();
spesaAnnuaSingola.setText("Spesa Annua Singola");
panel.add(spesaAnnuaSingola, BorderLayout.NORTH);
JComboBox<String> listaSpese = new JComboBox<String>();
panel.add(listaSpese, BorderLayout.CENTER);
JTextField speseAnnuaSingolaTF = new JTextField();
speseAnnuaSingolaTF.setText("");
speseAnnuaSingolaTF.setEditable(false);
panel.add(speseAnnuaSingolaTF, BorderLayout.LINE_START);
JButton button = new JButton("CALCOLA")
{
public Dimension getPreferredSize()
{
return new Dimension(150,50);
};
};
JPanel leftflowpanel = new JPanel( new FlowLayout(FlowLayout.LEFT) );
leftflowpanel.add(speseAnnuaSingolaTF);
panel.add(leftflowpanel, BorderLayout.SOUTH);
JPanel rightflowpanel = new JPanel( new FlowLayout(FlowLayout.RIGHT) );
rightflowpanel.add(button);
panel.add(rightflowpanel, BorderLayout.SOUTH);
return panel;
}
And then:
public StatsPanel()
{
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setExtendedState(JFrame.MAXIMIZED_BOTH);
GridBagConstraints c = new GridBagConstraints();
// I will need a grid layout
this.setLayout(new GridLayout(1, 1, 30, 30));
JPanel panelLeft = new JPanel(new BorderLayout());
panelLeft.setBorder(BorderFactory.createEmptyBorder(20, 10, 20, 50));
panelLeft.add(firstPanel(), BorderLayout.NORTH);
c.fill = GridBagConstraints.VERTICAL;
c.gridx = 0;
c.gridy = 0;
this.add(panelLeft);
}
JComboBox<String> listaSpese = new JComboBox<String>();
panel.add(listaSpese, BorderLayout.CENTER);
You add your combo box to the CENTER of the BorderLayout, which gets all the extra space of the frame. Don't add the combo box to the CENTER.
Instead you will need to nest panels. So create a panel for the NORTH of the BorderLayout. Then this panel will contain both your label and your combo box. Maybe use a vertical BoxLayout for this panel. Then both the label and the combo box will be displayed at their preferred heights.
Read the section from the Swing on Layout Manager for more information. The point is you can nest multiple panels each using a different layout to achieve your desired layout.
I want to create a JInternalFrame with some components in it.
My aim is to design a bash console in Java.
My frame is made of 4 components:
JTextArea included into a JScrollPane
JLabel with the text "Cmd:"
JTextField
JButton with the text "Send"
And I have the following code:
Box box = Box.createHorizontalBox();
box.add(Box.createVerticalStrut(5));
box.add(this.cmd_label);
box.add(Box.createVerticalStrut(5));
box.add(this.cmd_input);
box.add(Box.createVerticalStrut(5));
box.add(this.submit);
box.add(Box.createVerticalStrut(5));
Box mainBox = Box.createVerticalBox();
mainBox.add(Box.createHorizontalStrut(5));
mainBox.add(this.result_scroll);
mainBox.add(Box.createHorizontalStrut(5));
mainBox.add(box);
mainBox.add(Box.createHorizontalStrut(5));
add(mainBox);
So when the frame has not been maximized, I have a correct look:
But when I maximize it, all components are incorrectly located:
So, here is my question: How can I set a weight to the components to fix their location every time, or, how can I fix it?
Thanks.
I think this would be better done with a BorderLayout. In a BorderLayout, the component specified as the center component will expand to fill as much space as possible, and the other components will remain at their preferred sizes.
int hgap = 5;
int vgap = 5;
internalFrame.getContentPane().setLayout(new BorderLayout(hgap, vgap));
internalFrame.getContentPane().add(this.result_scroll, BorderLayout.CENTER);
JPanel bottomPanel = new JPanel();
bottomPanel.add(this.cmd_label);
bottomPanel.add(this.cmd_input);
bottomPanel.add(this.submit);
internalFrame.getContentPane().add(bottomPanel, BorderLayout.SOUTH);
Here try this code, is this behaviour exceptable :
import java.awt.*;
import javax.swing.*;
public class LayoutExample
{
private void createAndDisplayGUI()
{
JFrame frame = new JFrame("LAYOUT EXAMPLE");
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
JPanel centerPanel = new JPanel();
centerPanel.setLayout(new BorderLayout(5, 5));
centerPanel.setBorder(
BorderFactory.createEmptyBorder(2, 2, 2, 2));
JTextArea tarea = new JTextArea(10, 10);
tarea.setBackground(Color.DARK_GRAY.darker());
tarea.setForeground(Color.WHITE);
tarea.setCaretColor(Color.WHITE);
tarea.setLineWrap(true);
JScrollPane scrollPane = new JScrollPane(tarea);
centerPanel.add(scrollPane, BorderLayout.CENTER);
JPanel footerPanel = new JPanel();
footerPanel.setLayout(new BorderLayout(5, 5));
footerPanel.setBorder(
BorderFactory.createEmptyBorder(2, 2, 2, 2));
JLabel cmdLabel = new JLabel("Cmd : ");
JTextField tfield = new JTextField(10);
JPanel buttonPanel = new JPanel();
buttonPanel.setBorder(
BorderFactory.createEmptyBorder(2, 2, 2, 2));
JButton sendButton = new JButton("SEND");
footerPanel.add(cmdLabel, BorderLayout.LINE_START);
footerPanel.add(tfield, BorderLayout.CENTER);
buttonPanel.add(sendButton);
footerPanel.add(buttonPanel, BorderLayout.LINE_END);
frame.getContentPane().add(centerPanel, BorderLayout.CENTER);
frame.getContentPane().add(footerPanel, BorderLayout.PAGE_END);
frame.pack();
frame.setLocationByPlatform(true);
frame.setVisible(true);
}
public static void main(String... args)
{
SwingUtilities.invokeLater(new Runnable()
{
public void run()
{
new LayoutExample().createAndDisplayGUI();
}
});
}
}
OUTPUT :
Base problem here is what I consider a bug in JTextField's max layout hint: it's unbounded in both horizontal and vertical dimension. The latter is pure nonsense for a component designed for showing a single line of text. To fix, subclass and let it return its pref for the height, like:
JTextField cmdInput = new JTextField() {
#Override
public Dimension getMaximumSize() {
Dimension max = super.getMaximumSize();
max.height = getPreferredSize().height;
return max;
}
};
As BoxLayout respects maxSize, the excess height now will be given to the top box only.
On the long run, consider switching to a third party manager which allows fine-tuning in a all-in-one-panel approach. Yeah, here comes my current favourite: MigLayout. Compare the following lines to all the nesting and border tricks above and have fun :-)
MigLayout layout = new MigLayout("wrap 3, debug",
"[][grow, fill][]", // 3 columns, middle column filled and allows growing
"[grow, fill][]"); // two rows, first filled and allows growing
JComponent content = new JPanel(layout);
// the scrollPane in the first row spanning all columns
// and growing in both directions
content.add(new JScrollPane(new JTextArea(20, 20)), "span, grow");
// auto-wrapped to first column in second row
content.add(new JLabel("Cmd:"));
content.add(new JTextField());
content.add(new JButton("Submit"));
I started using MiGLayout about a month and half ago and everything is simple and works great. There's only one issue I still have that I haven't been able to fix.
Let's say I want to have a row that has two buttons on the right-most side and a centered title, the title doesn't actually get centered when I do it this way:
("this" is a JPanel)
this.add(labelTitle, "split, span, center");
this.add(closeButton, "east");
this.add(mainMenuButton, "east");
What happens is that "labelTitle" is centered in the remaining space available after the buttons are placed, but I actually want it to be centered relative to the whole JPanel, not just the remaining space.
What parameters could I use to get the desired effect? I know I could use absolute positioning, but I don't want to do that because it defeats the purpose of using MiGLayout in the first place in my case.
Can it be something like this you are looking for?
Cheers!
public static void main(String[] args)
{
JFrame frame = new JFrame();
JPanel panel = new JPanel(new MigLayout("debug"));
panel.add(new JLabel("Label Title"), "x2 min(b1.x - unrel, (container.w+pref)/2)");
panel.add(new JButton("Close Button"), "id b1, pushx, alignx right");
panel.add(new JButton("Main Menu Button"), "alignx right");
frame.add(panel);
frame.setSize(800, 200);
frame.setLocationRelativeTo(null);
frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
frame.setVisible(true);
}
You can use the JXLayer and put the buttons in the glasspane.
JButton closeButton = new JButton("Close");
JButton mainMenuButton = new JButton("Menu");
JLabel labelTitle = new JLabel("Application");
JPanel panel = new JPanel();
panel.setLayout(new MigLayout(new LC().fillX()));
panel.add(labelTitle, new CC().alignX("center").spanX());
JPanel buttonPanel = new JPanel();
buttonPanel.setLayout(new MigLayout(new LC().fillX()));
buttonPanel.add(closeButton, new CC().alignX("right").split());
buttonPanel.add(mainMenuButton, new CC().alignX("right"));
buttonPanel.setOpaque(false);
JXLayer<JPanel> mainPanel = new JXLayer<JPanel>();
mainPanel.setView(panel);
mainPanel.setGlassPane(buttonPanel);
JFrame frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setContentPane(mainPanel);
frame.setSize(400, 600);
frame.setVisible(true);
When creating your JPanel, use the following MigLayout initializer:
new MigLayout("","[]push[center]push[]","")
If you don't know about constraints, check here: MigLayout Whitepaper
This is assuming you don't have anything else in this JPanel...