Java JFrame method pack() - java

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.

Related

Achieving a gui similiar to the picture?

My plan is to get a similiar output, but for some reason, I am only getting the south panel...
My logic is to have 1 Main panel with North Center South.
In the North I will puth the Jlabel and Textfield and align it to the right.
In the Center I wil leave it empty
In the South I will Add a BoxLayout y-axis in the first row another panel with centered boxlayout
Another BoxLayout in the second row of the South BoxLayour row, I will add another Boxlayout and align it to the left.
Here is my code:
public static void main(String args[]) {
EventQueue.invokeLater(new Runnable() {
#Override
public void run() {
JFrame frame = new JFrame();
frame.setSize(new Dimension(500,600));
JPanel MainPanel = new JPanel();
frame.add(MainPanel);
JPanel NorthPanel = new JPanel(); //upper panel to add boxx layout and inside it 2 panls
JPanel ToPanel = new JPanel(); //inside north
JPanel SubjectPanel = new JPanel(); //inside north
NorthPanel.setLayout(new BoxLayout(NorthPanel, BoxLayout.Y_AXIS));
MainPanel.add(NorthPanel, BorderLayout.NORTH);
JLabel SubjectLabel = new JLabel("Subject"); SubjectLabel.setAlignmentX(Component.RIGHT_ALIGNMENT);
JTextField SubjectTextField = new JTextField(20); SubjectTextField.setAlignmentX(Component.RIGHT_ALIGNMENT);
JLabel ToLabel = new JLabel("To"); ToLabel.setAlignmentX(Component.RIGHT_ALIGNMENT);
JTextField ToTextField = new JTextField(20); ToTextField.setAlignmentX(Component.RIGHT_ALIGNMENT);
ToPanel.add(ToLabel);
ToPanel.add(ToTextField);
ToPanel.add(SubjectLabel);
ToPanel.add(SubjectTextField);
NorthPanel.add(ToPanel);
JPanel CenterPanel = new JPanel(); //Center panel blank
MainPanel.add(CenterPanel, BorderLayout.CENTER);
///
JPanel SouthPanel = new JPanel();
NorthPanel.setLayout(new BoxLayout(NorthPanel, BoxLayout.Y_AXIS));
JPanel FontPanels = new JPanel();
FontPanels.setLayout(new BoxLayout(FontPanels, BoxLayout.PAGE_AXIS));
FontPanels.add(new JButton("Bold"));
FontPanels.add(new JButton("Italic"));
FontPanels.add(new JButton("Underlined"));
FontPanels.add(new JButton("Undo"));
FontPanels.add(new JButton("Redo"));
FontPanels.setAlignmentX(Component.CENTER_ALIGNMENT);
JPanel OptionPanel = new JPanel();
OptionPanel.setLayout(new BoxLayout(OptionPanel, BoxLayout.PAGE_AXIS));
FontPanels.setLayout(new BoxLayout(FontPanels, BoxLayout.PAGE_AXIS));
OptionPanel.add(new JButton("Send"));
OptionPanel.setAlignmentX(Component.CENTER_ALIGNMENT);
SouthPanel.add(FontPanels);
SouthPanel.add(OptionPanel);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
});
}
You haven't added anything to the JFrame - so naturally it is displaying a blank JFrame.
For each component to appear, you'll need to do frame.add(component);
I leave the layout manager of the frame up to you.

Box Layout components move when button is visible and invisible

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.

JPanel stacking multiple buttons

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

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