I have this ridiculously simple code (actually directly copied from the miglayout white paper: http://www.miglayout.com/whitepaper.html). I added the panel.setVisible(true) at the end. The problem is, with or without that last line, nothing shows up.
MigLayout layout = new MigLayout("fillx", "[right]rel[grow,fill]", "[]10[]");
JPanel panel = new JPanel(layout);
panel.add(new JLabel("Enter size:"), "");
panel.add(new JTextField(""), "wrap");
panel.add(new JLabel("Enter weight:"), "");
panel.add(new JTextField(""), "");
panel.setVisible(true);
You need to add the panel to a JFrame:
import java.awt.EventQueue;
import java.awt.BorderLayout;
import java.awt.Dimension;
import javax.swing.JPanel;
import javax.swing.JLabel;
import javax.swing.JTextField;
import javax.swing.JFrame;
import net.miginfocom.swing.MigLayout;
public class MigLayoutTest {
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
createAndShowGUI();
}
});
}
private static void createAndShowGUI() {
MigLayout layout = new MigLayout("fillx", "[right]rel[grow,fill]", "[]10[]");
JPanel panel = new JPanel(layout);
panel.add(new JLabel("Enter size:"), "");
panel.add(new JTextField(""), "wrap");
panel.add(new JLabel("Enter weight:"), "");
panel.add(new JTextField(""), "");
//panel.setVisible(true);
JFrame frame = new JFrame("MigLayoutTest");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setPreferredSize(new Dimension(300,200));
frame.setLayout(new BorderLayout());
frame.add(panel, BorderLayout.CENTER);
frame.pack();
frame.setVisible(true);
}
}
JPanel is not a top level frame, but JFrame, JDialog, and JApplet are. See this tutorial on top level containers: http://docs.oracle.com/javase/tutorial/uiswing/components/toplevel.html
Related
I'm trying to get some of the buttons to be bigger and be able to move them around to get them more to what my professor wants them to be but I'm not sure how to do it.
I decided to use a GridBagLayout but my professor never talked about it so I'm not sure if I'm missing anything or how exactly it works.
The image is what he wants us to get it too. Exactly like this.
import java.awt.GridBagLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class GUI {
public static JPanel buttonPanel;
private static JFrame frame;
public static void main(String[] args) {
frame = new JFrame("Layout Question");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(500, 500);
frame.setLocationRelativeTo(null);
frame.setVisible(true);
JPanel mainPanel = new JPanel();
mainPanel.setLayout(new GridBagLayout());
frame.add(mainPanel);
buttonPanel = new JPanel();
mainPanel.add(buttonPanel);
buttonPanel.add(new JButton("hi"));
buttonPanel.add(new JButton("long name"));
buttonPanel.add(new JButton("bye"));
buttonPanel.add(new JButton("1"));
buttonPanel.add(new JButton("2"));
buttonPanel.add(new JButton("3"));
buttonPanel.add(new JButton("4"));
buttonPanel.add(new JButton("5"));
buttonPanel.add(new JButton("6"));
buttonPanel.add(new JButton("7"));
buttonPanel.add(new JButton("Cancel"));
}
}
There are some improvements you can do to your code:
Don't use static variables, and place your program on the EDT, an easy way to do this is:
public static void main(String[] args) {
SwingUtilities.invokeLater(new LayoutManagersExample()::createAndShowGUI);
}
Don't call setSize(...) on your JFrame, it's going to make your window smaller than you think, it's taking the frame decorations into the calculation for the size, instead call frame.pack(), or override the getPreferredSize() of your JPanel, see Should I avoid the use of set(Preferred|Maximum|Minimum)Size methods in Java Swing? for a more in-depth explanation.
Don't call frame.setVisible(true) before you've added all your components to your JFrame, otherwise you'll get strange bugs related to invisible components, that line should be the last one on your code.
Divide and conquer, you can use multiple JPanels with different Layout Managers, and you can combine them and join them together later on.
One possible approach (which isn't exactly as your teacher wants it to be but is close enough) is this one:
import java.awt.BorderLayout;
import java.awt.GridLayout;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
public class LayoutManagersExample {
private JFrame frame;
private JPanel pane;
public static void main(String[] args) {
SwingUtilities.invokeLater(new LayoutManagersExample()::createAndShowGUI);
}
private void createAndShowGUI() {
frame = new JFrame("Layout Question");
pane = new JPanel();
JPanel topPanel = getTopPanel();
JPanel boxesPanel = getBoxesPanel();
JPanel buttonsPanel = getButtonsPanel();
pane.add(boxesPanel);
pane.add(buttonsPanel);
frame.add(pane);
frame.add(topPanel, BorderLayout.NORTH);
frame.add(new JButton("Cancel"), BorderLayout.SOUTH);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//frame.setSize(500, 500);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
private JPanel getButtonsPanel() {
JPanel buttonsPanel = new JPanel();
buttonsPanel.setLayout(new GridLayout(2, 2));
buttonsPanel.add(new JButton("1"));
buttonsPanel.add(new JButton("2"));
buttonsPanel.add(getInnerButtonsPanel());
buttonsPanel.add(new JButton("7"));
return buttonsPanel;
}
private JPanel getInnerButtonsPanel() {
JPanel innerButtonsPanel = new JPanel();
innerButtonsPanel.setLayout(new GridLayout(2, 2));
innerButtonsPanel.add(new JButton("3"));
innerButtonsPanel.add(new JButton("4"));
innerButtonsPanel.add(new JButton("5"));
innerButtonsPanel.add(new JButton("6"));
return innerButtonsPanel;
}
private JPanel getBoxesPanel() {
JPanel boxesPanel = new JPanel();
boxesPanel.setLayout(new BoxLayout(boxesPanel, BoxLayout.PAGE_AXIS));
boxesPanel.add(new JCheckBox("Bold"));
boxesPanel.add(new JCheckBox("Italic"));
boxesPanel.add(new JCheckBox("Underline"));
boxesPanel.add(new JCheckBox("Strikeout"));
return boxesPanel;
}
private JPanel getTopPanel() {
JPanel topPanel = new JPanel();
JPanel topButtonsPanel = new JPanel();
topButtonsPanel.setLayout(new GridLayout());
topButtonsPanel.add(new JButton("hi"));
topButtonsPanel.add(new JButton("long name"));
topButtonsPanel.add(new JButton("bye"));
topPanel.add(new JLabel("Buttons: "));
topPanel.add(topButtonsPanel);
return topPanel;
}
}
Play around with the code, and try to find a different approach by combining the layouts, divide each piece of the window in your head and see how to apply a different layout manager to each of them, divide them in methods as I did to make things easier to follow.
Find a way to left align the elements in the JCheckBoxes for example, and other things
I prepared 2 test cases in the JUnit style (sry for the weird kind of test I dont realyl know how to assert UI)
for the 2nd example I selectively cut the string but I want to be able to paste the long string and have the UI cut it correctly
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.event.WindowEvent;
import javax.swing.BoxLayout;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import org.junit.jupiter.api.Test;
public class testing {
#Test
public void howItsSupposedToLook() {
JFrame frame = new JFrame();
frame.setLayout(new BorderLayout());
frame.setVisible(true);
frame.setSize(1000, 900);
JPanel leftPanel = new JPanel();
leftPanel.setBackground(Color.GREEN);
leftPanel.setLayout(new BoxLayout(leftPanel, BoxLayout.PAGE_AXIS));
// this is how it should look
JLabel fileLabel3 = new JLabel(
"<html><div style=\"padding-left:1px;padding-top:10px\"><div style=\"border:1px;font-size: 10px;line-height: 11px; height:23px;overflow:hidden;width:"
+ (100) + "px;white-space:nowrap;\">" + "fsdfesfevvveeeg" + "</div></div></html>");
leftPanel.add(fileLabel3);
//no more differences between the two from here
frame.add(leftPanel, BorderLayout.LINE_START);
JPanel centerPAnel = new JPanel();
centerPAnel.setBackground(Color.RED);
frame.add(centerPAnel, BorderLayout.CENTER);
frame.validate();
frame.repaint();
boolean open = true;
frame.addWindowListener(new java.awt.event.WindowAdapter() {
#Override
public void windowClosing(WindowEvent winEvt) {
System.exit(0);
}
});
while (open) {
}
}
#Test
public void failedTest() {
JFrame frame = new JFrame();
frame.setLayout(new BorderLayout());
frame.setVisible(true);
frame.setSize(1000, 900);
JPanel leftPanel = new JPanel();
leftPanel.setBackground(Color.GREEN);
leftPanel.setLayout(new BoxLayout(leftPanel, BoxLayout.PAGE_AXIS));
// this is how it actually looks
JLabel fileLabel = new JLabel(
"<html><div style=\"padding-left:1px;padding-top:10px\"><div style=\"border:1px;font-size: 10px;line-height: 11px; height:23px;overflow:hidden;width:"
+ (100) + "px;white-space:nowrap;\">"
+ "fsdfesfevvveeegggggggggggggggggggggggevevsdffffffffffffffffffffffffffffffffffffffffffffffffffffffffh"
+ "</div></div></html>");
fileLabel.setMaximumSize(new Dimension(100, 40));
//no more differences between the two from here
leftPanel.add(fileLabel);
frame.add(leftPanel, BorderLayout.LINE_START);
JPanel centerPAnel = new JPanel();
centerPAnel.setBackground(Color.RED);
frame.add(centerPAnel, BorderLayout.CENTER);
frame.validate();
frame.repaint();
boolean open = true;
frame.addWindowListener(new java.awt.event.WindowAdapter() {
#Override
public void windowClosing(WindowEvent winEvt) {
System.exit(0);
}
});
while (open) {
}
}
}
how its supposed to look (first (manufactured ) example) :
how it actually looks:
to me it feels like the Layout doesnt correclty recognize the length of the JPanel does anyone have an idea on what to do about that ?
Simply use
fileLabel.setPreferredSize(new Dimension(100, 40));
Instead of
fileLabel.setMaximumSize(new Dimension(100, 40));
This setter tells what size is must be used as "preferred" for this component when it placed in container with layout manager
PS: Sorry for my english
I am trying to design similar to a browsers title bar(top of the browser). Left side has tabs and right side has minimize, resize(minimize/maximize),exit button.
For this I tried like.
JPanel panel = new JPanel();
panel.setLayout(new FlowLayout(FlowLayout.RIGHT));
JPanel tabpanel= new JPanel();
tabpanel.setLayout(new FlowLayout(FlowLayout.LEFT));
tabpanel.add(new JButton("Tab 1"));
tabpanel.add(new JButton("Tab 2"));
panel.add(tabpanel);
panel.add(new JButton("Minimize"));
panel.add(new JButton("Resize"));
panel.add(new JButton("Quit"));
Created Quit, Resize, Minimize buttons at the right as my needed but tabs created near Minimize button not LEFT of Frame. I think there should be method or anything to fill it remainng content or should I use another layout? Any help appreciated
I would highly recommend GridBagLayout, it's one of the most flexible and configurable layout managers available, but it does bring with it complexity
public class HeaderPane extends JPanel {
public HeaderPane() {
setLayout(new GridBagLayout());
add(new JButton("Tab 1"));
add(new JButton("Tab 2"));
GridBagConstraints gbc = new GridBagConstraints();
gbc.weightx = 1;
gbc.anchor = GridBagConstraints.BASELINE_TRAILING;
add(new JButton("Minimize"), gbc);
add(new JButton("Maximise"));
add(new JButton("Close"));
}
}
Arrgggh, the complexity burns, it burns 😱 sarcasm
So, this solution is a single container, with a single layout manager. I'm not saying a more complex requirement might benefit from a compounding solution (I'd be tempted to put the min/max/close and tab buttons in there own containers), but as a starting point, it's relatively simple.
Runnable example
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
public class Test {
public static void main(String[] args) {
new Test();
}
public Test() {
EventQueue.invokeLater(new Runnable() {
#Override
public void run() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
ex.printStackTrace();
}
JFrame frame = new JFrame("Testing");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(new HeaderPane(), BorderLayout.NORTH);
frame.add(new JPanel() {
#Override
public Dimension getPreferredSize() {
return new Dimension(600, 200);
}
});
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}
public class HeaderPane extends JPanel {
public HeaderPane() {
setLayout(new GridBagLayout());
add(new JButton("Tab 1"));
add(new JButton("Tab 2"));
GridBagConstraints gbc = new GridBagConstraints();
gbc.weightx = 1;
gbc.anchor = GridBagConstraints.BASELINE_TRAILING;
add(new JButton("Minimize"), gbc);
add(new JButton("Maximise"));
add(new JButton("Close"));
}
}
}
I suggest using BoxLayout as the layout manager of panel to organize the left and right part.
Full demo code below:
import javax.swing.*;
import java.awt.*;
public class Main {
public static void main(String[] args) {
SwingUtilities.invokeLater(()->{
JFrame frame = new JFrame("Solution");
JPanel container = new JPanel();
container.setLayout(new BorderLayout());
JPanel panel = new JPanel();
panel.setLayout(new BoxLayout(panel, BoxLayout.LINE_AXIS));
JPanel tabpanel= new JPanel();
tabpanel.setLayout(new FlowLayout(FlowLayout.LEFT));
tabpanel.add(new JButton("Tab 1"));
tabpanel.add(new JButton("Tab 2"));
panel.add(tabpanel);
panel.add(new JButton("Minimize"));
panel.add(new JButton("Resize"));
panel.add(new JButton("Quit"));
container.add(panel, BorderLayout.PAGE_START);
frame.add(container);
frame.pack();
frame.setSize(new Dimension(500,500));
frame.setVisible(true);
});
}
}
What it looks like:
As suggested in the comments: One solution could be to place the "tabs" and "buttons" into separate panels, and add them in the WEST and EAST of the title panel, which has a BorderLayout:
Here is the MCVE:
import java.awt.BorderLayout;
import java.awt.FlowLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
public class TitleBarLayout
{
public static void main(String[] args)
{
SwingUtilities.invokeLater(() -> createAndShowGui());
}
private static void createAndShowGui()
{
JFrame f = new JFrame();
JPanel mainPanel = new JPanel(new BorderLayout());
JPanel titleBar = new JPanel(new BorderLayout());
JPanel tabPanel = new JPanel();
tabPanel.setLayout(new FlowLayout());
tabPanel.add(new JButton("Tab 1"));
tabPanel.add(new JButton("Tab 2"));
titleBar.add(tabPanel, BorderLayout.WEST);
JPanel buttonsPanel = new JPanel();
buttonsPanel.add(new JButton("Minimize"));
buttonsPanel.add(new JButton("Resize"));
buttonsPanel.add(new JButton("Quit"));
titleBar.add(buttonsPanel, BorderLayout.EAST);
mainPanel.add(titleBar, BorderLayout.NORTH);
f.getContentPane().add(mainPanel);
f.setSize(800, 600);
f.setLocationRelativeTo(null);
f.setVisible(true);
}
}
I'm having an issue with my Java program where I can add a JButton to the panel in JFrame, but when I create an JTextArea object, the JButton disappears?
package sandBox;
import java.awt.BorderLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextArea;
public class Main {
public static void main(String[] args) {
System.out.println("Hello World");
JFrame frame = new JFrame("Hello world");
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(600,500);
frame.setLayout(new BorderLayout());
JButton button2 = new JButton("STOP");
JButton button1 = new JButton("GO");
JTextArea text1 = new JTextArea();
JPanel panel = new JPanel();
panel.setLayout(new BorderLayout());
panel.add(button1, BorderLayout.SOUTH);
panel.add(button2, BorderLayout.NORTH);
frame.add(panel);
}
}
Remember
BorderLayout will only allow a single component to occupy each of the available positions. Adding another component will cover the previous component
Where possible, always call setVisible after you've created the UI
To actually add all your components, your example doesn't actually add the JTextArea to the container
Someone like...
//...
// frame.setVisible(true);
//...
frame.add(text1);
frame.add(panel, BorderLayout.SOUTH);
frame.setVisible(true);
Might help
I'm creating a simple Java JFrame in Eclipse with a label, 2 radio buttons with 2 textfields, and a JButton. When i run the program, the objects inside it are messed up, the buttons and textfields don't show up and sometimes a textfield takes the entire size of the frame. However, when I minimize/maximize the frame and then restore it, they work normally. Here's the code:
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import javax.swing.BorderFactory;
import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.JTextField;
public class myframe {
public static void main(String s[]) {
JFrame frame = new JFrame("Title");
frame.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
// This is an empty content area in the frame
JLabel jlbempty = new JLabel("");
jlbempty.setPreferredSize(new Dimension(500, 400));
frame.getContentPane().add(jlbempty, BorderLayout.CENTER);
frame.pack();
frame.setVisible(true);
frame.setBounds(0, 0, 500, 400);
JPanel panel = new JPanel();
frame.getContentPane().add(panel, BorderLayout.NORTH);
panel.setLayout(new BoxLayout(panel, BoxLayout.PAGE_AXIS));
panel.add(Box.createRigidArea(new Dimension(0,5)));
panel.setBorder(BorderFactory.createEmptyBorder(10,10,10,10));
JLabel label = new JLabel("My label");
panel.add(label);
JPanel buttonPane = new JPanel();
frame.add(buttonPane);
buttonPane.setLayout(new BoxLayout(buttonPane, BoxLayout.LINE_AXIS));
buttonPane.setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10));
JRadioButton cb = new JRadioButton("1");
buttonPane.add(cb);
JTextField tf = new JTextField(0);
tf.setText("");
buttonPane.add(tf);
JPanel panel3 = new JPanel();
frame.add(panel3);
panel3.setLayout(new BoxLayout(panel3, BoxLayout.LINE_AXIS));
panel3.setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10));
JRadioButton cb2 = new JRadioButton("2");
panel3.add(cb2);
JTextField tf2 = new JTextField(0);
tf.setText("");
panel3.add(tf2);
JPanel panel2 = new JPanel();
JButton button = new JButton("click me");
frame.add(panel2);
panel2.add(button);
button.addActionListener(new Action());
panel.add(buttonPane);
panel.add(panel3);
panel.add(panel2);
}
static class Action implements ActionListener{
public void actionPerformed(ActionEvent arg0) {
JFrame frame2 = new JFrame("Clicked");
frame2.setVisible(true);
frame2.setSize(100, 200);
JLabel label2 = new JLabel("You clicked me");
JPanel panel2 = new JPanel();
frame2.add(panel2);
frame2.add(label2);
}
}
}
In your main method you need to do this at the very end:
frame.pack();
frame.setVisible(true);
You should call frame.pack(); again after adding all the components, so that all the container elements can resize to fit their components best.
http://docs.oracle.com/javase/7/docs/api/java/awt/Window.html#pack()
You should also call frame.SetVisible(true); at the very end, so the form is only displayed ones all components are loaded (otherwise you can see a black box while it loads).