Swing: Place jTables and jButtons - java

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.

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);

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);
}

GUI Layout for User Interface

I am trying to get a userinterface page to display correctly on my page. It has to display a certain way on the screen but I cant get the correct Lay out to show up. Any suggestions or Help?! I've tried many things but Java can get a bit confusing. ATTACHED IS A LINK FOR IMAGE OF HOW ITS SUPPOSED TO LOOK
https://courses.eas.asu.edu/cse205/current/assignments/assignment6/assignment6.html
public CreatePanel(Vector accountList, TransferPanel tPanel)
{
this.accountList = accountList;
this.transferPanel = tPanel;
JLabel label1 =new JLabel("Account ID: ");
JLabel label2 = new JLabel("Amount: ");
JTextField field1 = new JTextField();
field1.setPreferredSize(new Dimension(250,70));
JTextField field2 = new JTextField();
field2.setPreferredSize(new Dimension(250,70));
button1 = new JButton("Create an Account");
JTextArea textArea = new JTextArea();
textArea.setPreferredSize(new Dimension(500, 600));
textArea.append("No account");
textArea.setEditable(true);
JPanel panel1 = new JPanel();
panel1.setLayout(new GridLayout(3,3));
panel1.add(label1);
panel1.add(field1);
panel1.add(label2);
panel1.add(field2);
JPanel panel2 = new JPanel();
panel2.setLayout(new BorderLayout());
panel2.add(button1, BorderLayout.SOUTH);
JPanel panel3 = new JPanel();
panel3.setLayout(new BorderLayout());
panel3.add(textArea, BorderLayout.WEST);
add(panel1);
add(panel3);
add(panel2);
//ActionListener listener = new ButtonListener();
//button1.addActionListener(listener);
}

too much empty space inside jpanel with gridlayout

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);
}

Java JFrame method pack()

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.

Categories