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
Related
When I type .setLayout(new BorderLayout());
It appears me this: The method setLayout(LayoutManager) in the type JFrame is not applicable for the arguments (BorderLayout)
I´m a beginner and I was following a video but this does not work and I already watched different videos, thank you so much for your help.
import java.awt.Color;
import java.awt.Dimension;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class BorderLayout {
public static void main(String []args) {
JFrame frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(500, 500);
frame.setLayout(new BorderLayout());
frame.setVisible(true);
JPanel panel1 = new JPanel();
JPanel panel2 = new JPanel();
JPanel panel3 = new JPanel();
JPanel panel4 = new JPanel();
JPanel panel5 = new JPanel();
panel1.setBackground(Color.red);
panel2.setBackground(Color.green);
panel3.setBackground(Color.yellow);
panel4.setBackground(Color.magenta);
panel5.setBackground(Color.blue);
panel1.setPreferredSize(new Dimension(100, 100));
panel2.setPreferredSize(new Dimension(100, 100));
panel3.setPreferredSize(new Dimension(100, 100));
panel4.setPreferredSize(new Dimension(100, 100));
panel5.setPreferredSize(new Dimension(100, 100));
frame.add(panel1, BorderLayout.NORTH);
frame.add(panel2, BorderLayout.WEST);
frame.add(panel3, BorderLayout.EAST);
frame.add(panel4, BorderLayout.SOUTH);
frame.add(panel5, BorderLayout.CENTER);
}
}
Move frame.setVisible(true); to the end of your method. Swing layouts are lazy, they won't "magically" update by themselves, instead, you need to tell it when you want a container to be updated, using revalidate and repaint to schedule a new layout and pass pass, but, as I've said, the easiest thing to do in your case, is simply setup the window before you make it visible.
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import javax.swing.JFrame;
import javax.swing.JPanel;
import java.awt.EventQueue;
public class Main {
public static void main(String[] args) {
new Main();
}
public Main() {
EventQueue.invokeLater(new Runnable() {
#Override
public void run() {
JFrame frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLayout(new BorderLayout());
JPanel panel1 = new JPanel();
JPanel panel2 = new JPanel();
JPanel panel3 = new JPanel();
JPanel panel4 = new JPanel();
JPanel panel5 = new JPanel();
panel1.setBackground(Color.red);
panel2.setBackground(Color.green);
panel3.setBackground(Color.yellow);
panel4.setBackground(Color.magenta);
panel5.setBackground(Color.blue);
panel1.setPreferredSize(new Dimension(100, 100));
panel2.setPreferredSize(new Dimension(100, 100));
panel3.setPreferredSize(new Dimension(100, 100));
panel4.setPreferredSize(new Dimension(100, 100));
panel5.setPreferredSize(new Dimension(100, 100));
frame.add(panel1, BorderLayout.NORTH);
frame.add(panel2, BorderLayout.WEST);
frame.add(panel3, BorderLayout.EAST);
frame.add(panel4, BorderLayout.SOUTH);
frame.add(panel5, BorderLayout.CENTER);
frame.pack();
frame.setVisible(true);
}
});
}
}
That's because the name of your class is the same as BorderLayout layout. Change name of your class and it should work perfectly fine. Never use a keyword or something like that in naming an object/class/method etc.
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);
}
}
The class extends JPanel,
public void createDisplay(){
frame = new JFrame();
frame.setTitle(title);
frame.setSize(new Dimension(width, height));
frame.setVisible(true);
frame.setLocationRelativeTo(null);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setPreferredSize(new Dimension(width, height));
this.setMaximumSize(new Dimension(width, height));
this.setMinimumSize(new Dimension(width, height));
this.setLayout(null); //have tried default and BorderLayout
this.setSize(new Dimension(width, height));
this.setBounds(0, 0, width, height);
//basically trying everything
frame.add(this);
frame.pack();
}
on startup this code works fine and the JPanel completely covers the size of the Parent frame
However my program later tries to add a new JPanel to the class's extend JPanel with:
public void gameOverWindow(){ ;
JPanel panel = new JPanel(new BorderLayout());
panel.setPreferredSize(new Dimension(200, 100));
JLabel label = new JLabel("Game Over");
label.setPreferredSize(new Dimension(40, 15));
//trying setPosition also doesn't work with BorderLayout or FlowLayout
JButton button_01 = new JButton("new");
button_01.setPreferredSize(new Dimension(100, 10));
JButton button_02 = new JButton("exit");
button_02.setPreferredSize(new Dimension(100, 10));
panel.add(label, BorderLayout.NORTH);
panel.add(button_01, BorderLayout.WEST);
panel.add(button_02, BorderLayout.EAST);
this.add(panel);
this.revalidate();
}
This new JPanel appears with the contents within the correct BorderLayout format, however the JPanel itself will remain at the top center of the extended JPanel, I know this is because the default Layout is set to FlowLayout, however setting this to BorderLayout will just cause the panel to take up the entire screen. Setting the Layout to null completely breaks the frame and nothing appears but the Minimize and Close buttons of the Frame. Trying to set the position or Bounds of this JPanel doesn't work with any Layout either. I have read a lot of other post online about this but they all seem to differ and become confusing, how do I gain control of the position of my new JPanel?
Normally I'd recommend using a CardLayout for switching between different views, but it's difficult to ascertain from the available information if that would help or not
Instead, you could make use of compounding layouts. That is wrap one container in another using different layouts.
In this example, I simply use a combination of BorderLayout and GridBagLayout
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.GridBagLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
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 TestPane());
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}
public class TestPane extends JPanel {
public TestPane() {
setLayout(new BorderLayout());
JButton gameOver = new JButton("Game over man");
gameOver.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
JPanel panel = new JPanel(new BorderLayout());
panel.add(new JLabel("Game Over Man", JLabel.CENTER), BorderLayout.NORTH);
panel.add(new JButton("New"), BorderLayout.WEST);
panel.add(new JButton("Exit"), BorderLayout.EAST);
removeAll();
JPanel inner = new JPanel(new GridBagLayout());
inner.add(panel);
add(inner);
revalidate();
repaint();
}
});
JPanel inner = new JPanel(new GridBagLayout());
inner.add(gameOver);
add(inner);
}
#Override
public Dimension getPreferredSize() {
return new Dimension(200, 200);
}
}
}
what is the purpose of removing the components of a different panel instead of just directly adding it to GridBagLayout?
Because they interfere with the layout of other components.
i then want a small Jpanel to popup within and be unobtrusive
You could make use of the frame's glassPane or use a OverlayLayout
For example:
Floating JPanel above a JPanel with BorderLayout
Rectangle is not drawn on top
Placing a marker within the image
Display a message on the screen
Much of this information should have been in your original question, it would have wasted less of each other's time
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
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).