too much empty space inside jpanel with gridlayout - java

I have a JPanel and inside I use a GridLayout like this:
JPanel panel = new JPanel(new GridLayout(0, 1, 0, 0));
JPanel p1 = new JPanel(new FlowLayout());
JLabel label = new JLabel("SOMETHING");
JTextField tf = new JTextField(30);
JPanel p2 = new JPanel();
JTextArea txt = new JTextArea(6, 30);
JScrollPane sp = new JScrollPane(txt);
p1.add(label);
p1.add(tf);
p2.add(sp);
panel.add(p1);
panel.add(p2);
Unfortunately, the space between the JTextArea and the upper elements if very big.
What can I do to bring the JTextArea up?
http://img20.imageshack.us/img20/1086/screenshot1412201213550.png

Use BorderLayout and add the top panel to NORTH and the scroll pane to the CENTER.
Screenshot of the code below:
public static void main(String[] args) {
JFrame frame = new JFrame("Test");
frame.add(new JPanel(new FlowLayout()) {{
add(new JLabel("something"));
add(new JTextField(30));
}}, BorderLayout.NORTH);
frame.add(new JScrollPane(new JTextArea(6, 30)), BorderLayout.CENTER);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.pack();
frame.setVisible(true);
}

Related

Packing three JPanels within a JFrame

I'm trying to fit three JPanel (dataPanel, tablePanel and btnPanel), so this is how far I got:
The thing is that the middle panel (tablePanel) is cropped on the top and the header is not visible. Also I added a JScrollPane to it that doesn't appear either. I'm new to Java Swing, so here's my code:
JFrame frame = new JFrame("Crear pedido");
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
frame.setSize(500,500);
//frame.setSize(300, 150);
// or frame.pack() to "pack" all the components in this frame
frame.setVisible(true); // show it
frame.setResizable(false);
frame.setLocationRelativeTo(null);
JPanel dataPanel = new JPanel();
dataPanel.setLayout(new BoxLayout(dataPanel, BoxLayout.X_AXIS));
//frame.add((Component) BorderFactory.createEmptyBorder(30,0,0,0));
dataPanel.setBorder(new EmptyBorder(20,0,30,0));
frame.add(dataPanel, BorderLayout.NORTH);
JLabel id = new JLabel("ID:");
JLabel date = new JLabel("Fecha:");
JLabel dept = new JLabel("Departamento");
JTextField idInput = new JTextField (5);
idInput.setMaximumSize(new Dimension(70,25));
JFormattedTextField dateInput = new JFormattedTextField(df);
dateInput.setMaximumSize(new Dimension(70,25));
JComboBox deptSelect = new JComboBox(selectArray);
deptSelect.setMaximumSize(new Dimension(70,25));
id.setAlignmentX(Component.CENTER_ALIGNMENT);
date.setAlignmentX(Component.CENTER_ALIGNMENT);
dept.setAlignmentX(Component.CENTER_ALIGNMENT);
idInput.setAlignmentX(Component.CENTER_ALIGNMENT);
dateInput.setAlignmentX(Component.CENTER_ALIGNMENT);
deptSelect.setAlignmentX(Component.CENTER_ALIGNMENT);
dataPanel.add(Box.createRigidArea(new Dimension(30,0)));
dataPanel.add(id);
dataPanel.add(idInput);
dataPanel.add(Box.createRigidArea(new Dimension(30,0)));
dataPanel.add(date);
dataPanel.add(dateInput);
dataPanel.add(Box.createRigidArea(new Dimension(30,0)));
dataPanel.add(dept);
dataPanel.add(deptSelect);
JPanel productPanel = new JPanel();
productPanel.setLayout(new BoxLayout(productPanel, BoxLayout.Y_AXIS));
frame.add(productPanel, BorderLayout.CENTER);
JTable table = new JTable(data, headerProductos);
table.setMaximumSize(new Dimension(500,250));
table.setAlignmentX(Component.CENTER_ALIGNMENT);
table.setAlignmentY(Component.CENTER_ALIGNMENT);
table.getTableHeader().setForeground(Color.blue);
table.setEnabled(false);
JScrollPane scroll = new JScrollPane(table);
productPanel.add(table);
table.add(scroll);
JPanel btnPanel = new JPanel();
btnPanel.setLayout(new BoxLayout(btnPanel, BoxLayout.X_AXIS));
btnPanel.setBorder(new EmptyBorder(150,0,0,0));
frame.add(btnPanel, BorderLayout.SOUTH);
Replace lines:
productPanel.add(table);
table.add(scroll);
with:
productPanel.add(scroll);

JPanel inside JPanel in JAVA

public static void main(String[] args) {
JTextField text = new JTextField();
JFrame frame = new JFrame();
frame.setLayout(new BorderLayout());
JPanel panel = new JPanel();
panel.setLayout(new GridLayout(5, 4));
JPanel panel2 = new JPanel();
panel2.setLayout(new FlowLayout());
JLabel imgLabel1 = new JLabel(new ImageIcon("C:\\Users\\Arthur\\Downloads\\abs.jpg"));
JLabel imgLabel2 = new JLabel(new ImageIcon("C:\\Users\\Arthur\\Downloads\\abss.jpg"));
imgLabel1.setPreferredSize(new Dimension(100,100));
imgLabel2.setPreferredSize(new Dimension(100,100));
panel2.add(imgLabel1);
panel2.add(imgLabel2);
for(int i=0; i<20; i++){
panel.add(panel2);
}
frame.add(text, BorderLayout.NORTH);
frame.add(panel, BorderLayout.CENTER);
frame.setPreferredSize(new Dimension(1280,700));
frame.pack();
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
I want to make a memory game, I need to put two images in each cell of the JPanel of 4x5. For this I created a JPanel 1x2 with two images inside and put it in the JPanel of 4x5. But the result is:
Result:
So, if understand correctly, you're problem is, you're not seeing 20 new panels, only one.
The problem is, a component can only reside in a single container, once, so doing something like...
for (int i = 0; i < 20; i++) {
panel.add(panel2);
}
is the equivalent of doing something like...
panel.add(panel2);
You actually need to create a new instance of the component on each iteration of the loop
What I would suggest you do is create a "wrapper" or "card" panel which can contain the two images. In my testing I just used coloured panels, but you get the idea...
public class WrapperPane extends JPanel {
public WrapperPane() {
setLayout(new FlowLayout());
add(makePanel(Color.RED));
add(makePanel(Color.GREEN));
// This is just for demonstration purposes
setBorder(new LineBorder(Color.DARK_GRAY));
}
protected JPanel makePanel(Color background) {
JPanel panel = new JPanel();
panel.setBackground(background);
panel.setPreferredSize(new Dimension(100, 100));
return panel;
}
}
The you'd just have to do something like...
JTextField text = new JTextField();
JFrame frame = new JFrame();
frame.setLayout(new BorderLayout());
JPanel panel = new JPanel();
panel.setLayout(new GridLayout(5, 4));
for (int i = 0; i < 20; i++) {
panel.add(new WrapperPane());
}
frame.add(text, BorderLayout.NORTH);
frame.add(panel, BorderLayout.CENTER);
// Don't do this, just let the content make it's own
// calculations
//frame.setPreferredSize(new Dimension(1280, 700));
frame.pack();
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
And you'd end up with something like...

Two labels in the middle of center

I want to place two labels in the middle of the center of a window. I get it working with 1 label and the following code:
Screenshot: http://abload.de/img/scr1g6u0f.png
public static void main(String[] args)
{
JFrame contentPane = new JFrame();
contentPane.setBounds(100, 100, 450, 300);
JPanel centerPanel = new JPanel(new BorderLayout());
JLabel label = new JLabel("center1");
centerPanel.add(label, BorderLayout.CENTER);
contentPane.add(centerPanel, BorderLayout.CENTER);
contentPane.setVisible(true);
}
Now I want another label next to the first label. I tried to use a flowlabel, but they are placed at the top of the BorderLayout.CENTER
Screenshot: http://abload.de/img/scr2a3u26.png
public static void main(String[] args)
{
JFrame contentPane = new JFrame();
contentPane.setBounds(100, 100, 450, 300);
JPanel centerPanel = new JPanel(new BorderLayout());
JLabel label1 = new JLabel("center1");
JLabel label2 = new JLabel("center2");
JPanel flowPanel = new JPanel(new FlowLayout());
flowPanel.add(label1);
flowPanel.add(label2);
centerPanel.add(flowPanel, BorderLayout.CENTER);
contentPane.add(centerPanel, BorderLayout.CENTER);
contentPane.setVisible(true);
}
Thanks!
Use a GridBagLayout without constraints:
JPanel centerPanel = new JPanel(new GridBagLayout());
JLabel label1 = new JLabel("center1");
JLabel label2 = new JLabel("center2");
JPanel flowPanel = new JPanel(new FlowLayout());
flowPanel.add(label1);
flowPanel.add(label2);
centerPanel.add(flowPanel);

MigLayout LC::fill() without resizing components

I have this code:
public static void main(String[] args) {
JPanel panel1 = new JPanel(new MigLayout(new LC().fillX()));
panel1.add(new JTextField("text1"), "span, grow");
panel1.add(new JTextField("another text field"), "span, grow");
panel1.add(new JTextField("text3"), "span, grow");
JPanel panel2 = new JPanel(new MigLayout());
JTextArea textArea = new JTextArea();
textArea.setColumns(15);
textArea.setRows(7);
JScrollPane jsp = new JScrollPane(textArea);
panel2.add(jsp, "span, grow");
JFrame frame = new JFrame();
frame.setLayout(new GridLayout(1, 2));
frame.add(panel1);
frame.add(panel2);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
which produces this:
But, I am trying to get the JTextFields to space out evenly.
So, I change:
JPanel panel1 = new JPanel(new MigLayout(new LC().fillX()));
to
JPanel panel1 = new JPanel(new MigLayout(new LC().fill()));
(fill() works the same as combining fillX() and fillY()) which produces:
However, I do not wish for the JTextFields to resize, only the gaps between them to increase. Is there a way to accomplish this with MigLayout?
I figured it out. It is because I was using the grow attribute for each component. The proper attribute to use is growx.
public static void main(String[] args) {
JPanel panel1 = new JPanel(new MigLayout(new LC().fill()));
panel1.add(new JTextField("text1"), "span, growx");
panel1.add(new JTextField("another text field"), "span, growx");
panel1.add(new JTextField("text3"), "span, growx");
JPanel panel2 = new JPanel(new MigLayout());
JTextArea textArea = new JTextArea();
textArea.setColumns(15);
textArea.setRows(7);
JScrollPane jsp = new JScrollPane(textArea);
panel2.add(jsp, "span, grow");
JFrame frame = new JFrame();
frame.setLayout(new GridLayout(1, 2));
frame.add(panel1);
frame.add(panel2);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}

Swing: Place jTables and jButtons

I have a jFrame with 2 jTables (inserted in 2 jScrollPanes). Then, I have 3 jButtons for each jTable. How can I place them to have the following result:
I don't know very well what Layout to use to manage it.
Thanks!
I have this, but I can't see the buttons:
JButton addButton1 = new JButton();
JButton deleteButton1 = new JButton();
JButton playButton1 = new JButton();
JButton addButton2 = new JButton();
JButton deleteButton2 = new JButton();
JButton playButton2 = new JButton();
JFrame frame = new JFrame();
frame.setLayout(new GridLayout(1, 2));
JPanel panel1 = new JPanel();
panel1.setLayout(new FlowLayout());
panel1.setBorder(javax.swing.BorderFactory.createTitledBorder("List 1"));
JPanel panel2 = new JPanel();
panel2.setLayout(new FlowLayout());
panel2.setBorder(javax.swing.BorderFactory.createTitledBorder("List 2"));
JPanel panel3 = new JPanel();
panel3.setLayout(new FlowLayout());
panel3.add(addButton1);
panel3.add(deleteButton1);
panel3.add(playButton1);
JPanel panel4 = new JPanel();
panel4.setLayout(new FlowLayout());
panel4.add(addButton2);
panel4.add(deleteButton2);
panel4.add(playButton2);
JScrollPane tableContainer1 = new JScrollPane(table1);
panel1.add(tableContainer1, BorderLayout.CENTER);
JScrollPane tableContainer2 = new JScrollPane(table2);
panel2.add(tableContainer2, BorderLayout.CENTER);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLayout(new FlowLayout());
frame.getContentPane().add(panel1);
frame.getContentPane().add(panel2);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
Edit: Gah! Someone beat me to it...Here's a slightly different approach regardless!
There's many ways to do this, but I would try the following:
JFrame > BoxLayout using X_AXIS
JPanel #1 > BorderLayout
[BorderLayout.NORTH] JPanel for buttons > FlowLayout using FlowLayout.LEFT
[BorderLayout.CENTER] JScrollPane with Table #1
JPanel #2 > BorderLayout
[BorderLayout.NORTH] JPanel for buttons > FlowLayout using FlowLayout.LEFT
[BorderLayout.CENTER] JScrollPane with Table #2
Using BoxLayout and BorderLayout.CENTER will ensure that the tables resize with the frame and fill up as much space as they can.
Here's a simple example:
public class TwoTableJFrameTest extends JFrame
{
public TwoTableJFrameTest()
{
setTitle("Two Table Layout");
setLayout(new BoxLayout(getContentPane(), BoxLayout.X_AXIS));
JPanel table1Panel = new JPanel(new BorderLayout(5, 5));
JPanel table1ButtonPanel = new JPanel(new FlowLayout(FlowLayout.LEFT));
table1ButtonPanel.add(new JButton("Button 1"));
table1ButtonPanel.add(new JButton("Button 2"));
table1ButtonPanel.add(new JButton("Button 3"));
JTable table1 = new JTable(new DefaultTableModel(new Object[]{"Column 1", "Column 2"}, 10));
table1Panel.add(table1ButtonPanel, BorderLayout.NORTH);
table1Panel.add(new JScrollPane(table1));
JPanel table2Panel = new JPanel(new BorderLayout(5, 5));
JPanel table2ButtonPanel = new JPanel(new FlowLayout(FlowLayout.LEFT));
table2ButtonPanel.add(new JButton("Button 1"));
table2ButtonPanel.add(new JButton("Button 2"));
table2ButtonPanel.add(new JButton("Button 3"));
JTable table2 = new JTable(new DefaultTableModel(new Object[]{"Column 1", "Column 2"}, 10));
table2Panel.add(table2ButtonPanel, BorderLayout.NORTH);
table2Panel.add(new JScrollPane(table2));
add(table1Panel);
add(table2Panel);
pack();
}
}
Steps:
Give the main JFrame a GridLayout that has two columns and one row.
Add to this JFrame 2 JPanels.
These 2 panels will each have a FlowLayout Y-Axis BoxLayout.
Add to each of these 2 panels a JPanel that holds your 3 buttons.
Add the table to each of the 2 panels.
This is why you couldn't see the buttons, you forgot to do this:
panel1.add(panel3);
panel2.add(panel4);
Anyway, here's the working code:
JButton addButton1 = new JButton();
JButton deleteButton1 = new JButton();
JButton playButton1 = new JButton();
JButton addButton2 = new JButton();
JButton deleteButton2 = new JButton();
JButton playButton2 = new JButton();
JFrame frame = new JFrame();
frame.setLayout(new GridLayout(1, 2));
JPanel panel1 = new JPanel();
panel1.setLayout(new BoxLayout(panel1, BoxLayout.Y_AXIS));
panel1.setBorder(javax.swing.BorderFactory.createTitledBorder("List 1"));
JPanel panel2 = new JPanel();
panel2.setLayout(new BoxLayout(panel2, BoxLayout.Y_AXIS));
panel2.setBorder(javax.swing.BorderFactory.createTitledBorder("List 2"));
JPanel panel3 = new JPanel();
panel3.setLayout(new FlowLayout());
panel3.add(addButton1);
panel3.add(deleteButton1);
panel3.add(playButton1);
JPanel panel4 = new JPanel();
panel4.setLayout(new FlowLayout());
panel4.add(addButton2);
panel4.add(deleteButton2);
panel4.add(playButton2);
panel1.add(panel3);
panel2.add(panel4);
JScrollPane tableContainer1 = new JScrollPane(table1);
panel1.add(tableContainer1, BorderLayout.CENTER);
JScrollPane tableContainer2 = new JScrollPane(table2);
panel2.add(tableContainer2, BorderLayout.CENTER);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLayout(new FlowLayout());
frame.getContentPane().add(panel1);
frame.getContentPane().add(panel2);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
P.S.
You'll notice I changed the layout of panel1 & panel2 from FlowLayout to a Y-Axis BoxLayout. This is because the buttons appeared beside the tables, not above. Changing the layout to a Y-Axis BoxLayout fixed that.

Categories