MigLayout LC::fill() without resizing components - java

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

Related

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

BorderLayout setSize issue

Before you come with GridBagLayout suggestions, I've tried that but I couldn't get it to work.
I want a frame with the size 800 x 800 and with a center panel of 600x600. Right now, when I run it the center panel is 600x578. Can someone tell me where it goes wrong? It's just 22 pixels.
public void createPlayground()
{
JFrame frame = new JFrame("ForFun Maze");
frame.setSize(WIDTH, HEIGHT);
frame.setResizable(false);
frame.setLayout(new BorderLayout());
JPanel buttonPanel = new JPanel();
buttonPanel.setLayout(new GridLayout(3,1));
buttonPanel.setPreferredSize(new Dimension(100,600));
buttonPanel.setMaximumSize(new Dimension(100,600));
buttonPanel.setBackground(SIDEBAR);
JButton reset = new JButton();
reset.setText("Reset");
reset.addActionListener(new RestartListener());
reset.setSize(100,180);
JButton pause = new JButton();
pause.setText("Pause");
pause.setSize(100,180);
pause.addActionListener(new PauseListener());
JButton quit = new JButton();
quit.setText("Quit");
quit.setSize(100,180);
quit.addActionListener(new QuitListener());
buttonPanel.add(pause);
buttonPanel.add(reset);
buttonPanel.add(quit);
Location[][] array = null;
if(level == 1)
{
array = glevel1;
}
CenterPanel centerPanel = new CenterPanel(array);
centerPanel.setPreferredSize(new Dimension(600,600));
centerPanel.setMinimumSize(new Dimension(600,600));
centerPanel.setBackground(BACKGROUND);
JPanel leftPanel = new JPanel();
leftPanel.setBackground(SIDEBAR);
leftPanel.setPreferredSize(new Dimension(100,600));
JPanel northPanel = new JPanel();
northPanel.setBackground(SIDEBAR);
northPanel.setPreferredSize(new Dimension(800,100));
JPanel bottomPanel = new JPanel();
bottomPanel.setBackground(SIDEBAR);
bottomPanel.setPreferredSize(new Dimension(800,100));
frame.add(northPanel, BorderLayout.NORTH);
frame.add(leftPanel, BorderLayout.WEST);
frame.add(centerPanel, BorderLayout.CENTER);
frame.add(buttonPanel, BorderLayout.EAST);
frame.add(bottomPanel, BorderLayout.SOUTH);
Dimension dim = Toolkit.getDefaultToolkit().getScreenSize();
frame.setLocation(dim.width / 2 - frame.getSize().width / 2, dim.height / 2 - frame.getSize().height / 2);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
System.out.println("Size of centerpane"+centerPanel.getWidth()+"x"+centerPanel.getHeight());
}
Use pack() over setSize() (because you don't take the JFrame insets into account
Use setLocationRelativeTo(null) (after calling pack()) to center the frame.
Your calls to setSize() on JComponent's are useless because the LayoutManager's will override them (same goes for setBounds and setLocation).
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.GridLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
import javax.xml.stream.Location;
public class Example {
public void createPlayground() {
JFrame frame = new JFrame("ForFun Maze");
frame.setResizable(false);
frame.setLayout(new BorderLayout());
JPanel buttonPanel = new JPanel();
buttonPanel.setLayout(new GridLayout(3, 1));
buttonPanel.setPreferredSize(new Dimension(100, 600));
buttonPanel.setMaximumSize(new Dimension(100, 600));
JButton reset = new JButton();
reset.setText("Reset");
reset.setSize(100, 180);
JButton pause = new JButton();
pause.setText("Pause");
pause.setSize(100, 180);
JButton quit = new JButton();
quit.setText("Quit");
quit.setSize(100, 180);
buttonPanel.add(pause);
buttonPanel.add(reset);
buttonPanel.add(quit);
Location[][] array = null;
JPanel centerPanel = new JPanel();
centerPanel.setPreferredSize(new Dimension(600, 600));
centerPanel.setMinimumSize(new Dimension(600, 600));
JPanel leftPanel = new JPanel();
leftPanel.setPreferredSize(new Dimension(100, 600));
JPanel northPanel = new JPanel();
northPanel.setPreferredSize(new Dimension(800, 100));
JPanel bottomPanel = new JPanel();
bottomPanel.setPreferredSize(new Dimension(800, 100));
frame.add(northPanel, BorderLayout.NORTH);
frame.add(leftPanel, BorderLayout.WEST);
frame.add(centerPanel, BorderLayout.CENTER);
frame.add(buttonPanel, BorderLayout.EAST);
frame.add(bottomPanel, BorderLayout.SOUTH);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
System.out.println("Size of centerpane" + centerPanel.getWidth() + "x" + centerPanel.getHeight());
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
#Override
public void run() {
new Example().createPlayground();
}
});
}
}

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

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