Packing three JPanels within a JFrame - java

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

Related

Java GUI Fill Empty spaces

I have the following Java GUI for my project. For some reason, the middle and right panels have been put below leaving empty spaces - marked in red. Is there a way to pull the panels up to fill the spaces.
These empty spaces does not look good. I have tried to check all code but cannot pinpoint where to correct.
Thanks in advance.
public static JPanel main= new JPanel(),
left= new JPanel(),middle= new JPanel(),right = new JPanel();
Container c = getContentPane();
c.setLayout(new FlowLayout()) ;
JPanel pane = new JPanel();
pane.setLayout(new BorderLayout());
pane.add(new JLabel("Enter word/SQL: "), "North");
pane.add(tf, "Center"); //enter keyword
JPanel second = new JPanel();
second.setLayout(new GridLayout(8, 1));
second.add(messageIDText);
//This is the start of adding panels
//create a left side panal and add the sub panels to it
JPanel left = new JPanel();
JPanel middle = new JPanel();
JPanel center = new JPanel();
JTextArea output = new JTextArea("", 60, 60);
JPanel sixth = new JPanel();
JPanel seventh = new JPanel();
JPanel eigth = new JPanel(new GridLayout(8,1));
JPanel labels7 = new JPanel(new GridLayout(5,1));
JPanel controls7 = new JPanel(new GridLayout(5,1));
JPanel tenth = new JPanel();
sixth.setLayout(new BorderLayout(5,5));
JPanel fourth = new JPanel();
fourth.add(firstButton);
sixth.add(labels, BorderLayout.WEST);
sixth.add(controls, BorderLayout.CENTER);
sixth.add(controlsButtons, BorderLayout.EAST);
labels.add(new JLabel("Proposal:"));
controls.add(proposalNumberText);
controlsButtons.add(freeQueryButton);
//add border
pane.setBorder(BorderFactory.createCompoundBorder(new
EmptyBorder(10,10,10,10), BorderFactory.createEtchedBorder()));
fourth.setBorder(BorderFactory.createCompoundBorder(new
EmptyBorder(10,10,10,10), BorderFactory.createEtchedBorder()));
//-------LEFT SIDE -- PARAMETERS
left.setLayout(new BoxLayout(left, BoxLayout.PAGE_AXIS));
JLabel headerLabel,blank;
headerLabel = new JLabel("Navigate Records", JLabel.LEFT);
Font f = headerLabel.getFont();
headerLabel.setFont(f.deriveFont(f.getStyle() ^ Font.BOLD)); // bold
left.add(headerLabel, BorderLayout.PAGE_END);
left.add(fourth, BorderLayout.PAGE_END);
main.add(left);
//---------MIDDLE PART - Messages - tabbed pane
JTabbedPane jtp = new JTabbedPane();
getContentPane().add(jtp);
//jtp.setSize(jtp.getPreferredSize());
JPanel jpA = new JPanel(),jpB = new JPanel(),
jpC = new JPanel(),jpD = new JPanel();
JScrollPane scrollPane2 = new JScrollPane(),jp = new
JScrollPane(activeTSText), jp2 = new JScrollPane(analyseWordsText),
jp3 = new JScrollPane(markedMessageText);
JLabel labelA = new JLabel();
labelA.setText("");
jpA.add(labelA);
jpA.add(jp);
jtp.addTab("Message", jpA);
middle.add(jtp);
main.add(middle);
//----------RIGHT PART - .. //paramters and results
JPanel jj = new JPanel();
jj.setLayout(new BoxLayout(jj, BoxLayout.PAGE_AXIS));
stateJTable.setRowHeight(20); //add JTable
JScrollPane js3 =new JScrollPane(stateJTable);
js3.setVisible(true);
//JTabbedPane jtp2 = new JTabbedPane();
//JPanel StateSubstatesTabOnLeft = new JPanel(),
StateSubstates_PostProcessedTabOnLeft = new JPanel();
JPanel firstleftTabinRightSideOfFrame = new JPanel(),
secondLeftTabinRightSideOfFrame = new
JPanel(),rightTabInRightSideOfFrame = new JPanel();
jj.add(js3);
//downbelow, the state jtable is added to the panel here through the
//scrollpane which contains the jtable
right.add(jj) ; //stateJTable);// add table in panel using add() method
main.add(right);
proposalNumberText.requestFocusInWindow();
I have solved the problem by using Borderlayout and assigning the main panel to it.
The assigning the WEST, CENTER and EAST regions to it.
I have added the code below in the code above at places where the main panel is being added to.
Thank you for the advice to start debugging by using MRE approach.
BorderLayout layout = new BorderLayout();
main.setLayout(layout);
main.add(left,BorderLayout.WEST);
main.add(middle,BorderLayout.CENTER);
main.add(right,BorderLayout.EAST);

background color and equal sized jradiobuttons

I have three radio buttons with background colors as shown below.
I need to stretch all of them to same size so that the background colors are uniform(with same width).Tried adding setWidth(Dimension d) but it's not working.
public class TrafficLights {
JFrame frame;
JRadioButton stop,go,wait;
JTextField signal;
ButtonGroup grp;
Dimension dim = new Dimension(200,30);
public TrafficLights(){
frame = new JFrame("Traffic Lights");
frame.setLayout(new BoxLayout(frame.getContentPane(),BoxLayout.Y_AXIS));
stop = new JRadioButton("Red");
stop.setBackground(Color.RED);
stop.setSize(dim);
wait = new JRadioButton("Orange");
wait.setBackground(Color.ORANGE);
wait.setSize(dim);
go = new JRadioButton("Green");
go.setBackground(Color.GREEN);
go.setSize(dim);
grp = new ButtonGroup();
grp.add(stop);grp.add(wait);grp.add(go);
frame.getContentPane().add(stop);
frame.getContentPane().add(wait);
frame.getContentPane().add(go);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
frame.setMinimumSize(new Dimension(300,200));
frame.pack();
frame.setLocationRelativeTo(null);
}
Use a JPanel with a GridLayout, then add the buttons to the panel and the the panel to the frame:
JPanel panel = new JPanel( new GridLayout(0, 1) );
panel.add(button1);
...
frame.add(panel, BorderLayout.PAGE_START);
You can use a GridLayout(int rows, int cols):
frame = new JFrame("Traffic Lights");
frame.setLayout(new BoxLayout(frame.getContentPane(), BoxLayout.Y_AXIS));
JPanel panel = new JPanel( new GridLayout(3, 1) );
frame.add(panel);
stop = new JRadioButton("Red");
stop.setBackground(Color.RED);
stop.setSize(dim);
wait = new JRadioButton("Orange");
wait.setBackground(Color.ORANGE);
wait.setSize(dim);
go = new JRadioButton("Green");
go.setBackground(Color.GREEN);
go.setSize(dim);
grp = new ButtonGroup();
grp.add(stop);
grp.add(wait);
grp.add(go);
panel.add(stop);
panel.add(wait);
panel.add(go);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
frame.setMinimumSize(new Dimension(300, 200));
frame.pack();
frame.setLocationRelativeTo(null);
For more infos see: GridLayout

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

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.

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

Categories