In the below example, in the west side of the border layout, there is a parent panel which has a BoxLayout and couple of panels inside. The problem is that the west panel covers the whole area from top to bottom. The FlowLayout used for child panels inside the parent panel consume a lot of area. Is it possible to compress each JPanel according to the components? Also, it should remain the same even when the window is maximized?
import java.awt.BorderLayout;
import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JLabel;
import javax.swing.BoxLayout;
import javax.swing.JTextField;
import javax.swing.JCheckBox;
import javax.swing.JButton;
public class Sample extends JFrame {
private JPanel contentPane;
private JTextField textField;
private JTextField textField_1;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
Sample frame = new Sample();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the frame.
*/
public Sample() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 450, 300);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
contentPane.setLayout(new BorderLayout(0, 0));
setContentPane(contentPane);
JPanel panel = new JPanel();
contentPane.add(panel, BorderLayout.NORTH);
JLabel lblNewLabel = new JLabel("New label");
panel.add(lblNewLabel);
JPanel panel_1 = new JPanel();
contentPane.add(panel_1, BorderLayout.WEST);
panel_1.setLayout(new BoxLayout(panel_1, BoxLayout.Y_AXIS));
JPanel panel_2 = new JPanel();
panel_1.add(panel_2);
textField = new JTextField();
panel_2.add(textField);
textField.setColumns(2);
textField_1 = new JTextField();
panel_2.add(textField_1);
textField_1.setColumns(2);
JPanel panel_3 = new JPanel();
panel_1.add(panel_3);
JCheckBox chckbxNewCheckBox = new JCheckBox("New check box");
panel_3.add(chckbxNewCheckBox);
JPanel panel_4 = new JPanel();
panel_1.add(panel_4);
JButton btnNewButton = new JButton("New");
panel_4.add(btnNewButton);
JButton btnNewButton_1 = new JButton("New");
panel_4.add(btnNewButton_1);
}
}
One approach is to add panel_1 to an enclosing panel. The default FlowLayout conforms itself to the preferred size of the enclosed components when you pack() the enclosing Window. I've added a gray panel to CENTER as a placeholder; resize the frame to see the effect.
JPanel flowPanel = new JPanel();
flowPanel.add(panel_1);
As tested:
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JLabel;
import javax.swing.BoxLayout;
import javax.swing.JTextField;
import javax.swing.JCheckBox;
import javax.swing.JButton;
public class Sample extends JFrame {
private JPanel contentPane;
private JTextField textField;
private JTextField textField_1;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
#Override
public void run() {
try {
Sample frame = new Sample();
frame.pack();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the frame.
*/
public Sample() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
contentPane.setLayout(new BorderLayout(0, 0));
setContentPane(contentPane);
JPanel panel = new JPanel();
contentPane.add(panel, BorderLayout.NORTH);
JLabel lblNewLabel = new JLabel("New label");
panel.add(lblNewLabel);
JPanel panel_1 = new JPanel();
panel_1.setLayout(new BoxLayout(panel_1, BoxLayout.Y_AXIS));
JPanel panel_2 = new JPanel();
panel_1.add(panel_2);
textField = new JTextField();
panel_2.add(textField);
textField.setColumns(2);
textField_1 = new JTextField();
panel_2.add(textField_1);
textField_1.setColumns(2);
JPanel panel_3 = new JPanel();
panel_1.add(panel_3);
JCheckBox chckbxNewCheckBox = new JCheckBox("New check box");
panel_3.add(chckbxNewCheckBox);
JPanel panel_4 = new JPanel();
panel_1.add(panel_4);
JButton btnNewButton = new JButton("New");
panel_4.add(btnNewButton);
JButton btnNewButton_1 = new JButton("New");
panel_4.add(btnNewButton_1);
JPanel flowPanel = new JPanel();
flowPanel.add(panel_1);
contentPane.add(flowPanel, BorderLayout.WEST);
contentPane.add(new JPanel(){
#Override
public Dimension getPreferredSize() {
return new Dimension(320, 240);
}
#Override
public Color getBackground() {
return Color.lightGray;
}
}, BorderLayout.CENTER);
}
}
Related
When I use the WindowBuilder preview, the code appears as it should, but when I run the program, it just brings up a blank frame. It looks fine in the preview, but as soon as I run it, it just brings up a blank frame.
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.EventQueue;
import java.awt.Point;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import java.awt.CardLayout;
import java.awt.GridLayout;
import javax.swing.JLabel;
import java.awt.Font;
import javax.swing.SwingConstants;
public class Frame extends JFrame {
/**
*
*/
private static final long serialVersionUID = 1L;
private JPanel teamSelect;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
JFrame frame = new JFrame();
frame.setForeground(Color.WHITE);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLocation(new Point(100, 50));
frame.setSize(1800, 900);
frame.setTitle("Hockey");
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the frame.
*/
public Frame() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 450, 300);
teamSelect = new JPanel();
teamSelect.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(teamSelect);
teamSelect.setLayout(new CardLayout(0, 0));
JPanel divisionGrid = new JPanel();
teamSelect.add(divisionGrid, "name_3885325667274");
divisionGrid.setLayout(new GridLayout(2, 2, 0, 0));
JPanel metroDivision = new JPanel();
divisionGrid.add(metroDivision);
metroDivision.setLayout(new BorderLayout(0, 0));
JLabel lblMetropolitanDivision = new JLabel("Metropolitan Division");
lblMetropolitanDivision.setHorizontalAlignment(SwingConstants.CENTER);
lblMetropolitanDivision.setFont(new Font("Eras Bold ITC", Font.PLAIN, 16));
metroDivision.add(lblMetropolitanDivision, BorderLayout.NORTH);
JPanel atlanticDivision = new JPanel();
divisionGrid.add(atlanticDivision);
atlanticDivision.setLayout(new BorderLayout(0, 0));
JLabel lblAtlanticDivision = new JLabel("Atlantic Division");
lblAtlanticDivision.setHorizontalAlignment(SwingConstants.CENTER);
lblAtlanticDivision.setFont(new Font("Eras Bold ITC", Font.PLAIN, 16));
atlanticDivision.add(lblAtlanticDivision, BorderLayout.NORTH);
JPanel centralDivision = new JPanel();
divisionGrid.add(centralDivision);
centralDivision.setLayout(new BorderLayout(0, 0));
JLabel lblNewLabel = new JLabel("Central Division");
lblNewLabel.setFont(new Font("Eras Bold ITC", Font.PLAIN, 16));
lblNewLabel.setHorizontalAlignment(SwingConstants.CENTER);
centralDivision.add(lblNewLabel, BorderLayout.NORTH);
JPanel pacificDivision = new JPanel();
divisionGrid.add(pacificDivision);
pacificDivision.setLayout(new BorderLayout(0, 0));
JLabel lblNewLabel_1 = new JLabel("Pacific Division");
lblNewLabel_1.setFont(new Font("Eras Bold ITC", Font.PLAIN, 16));
lblNewLabel_1.setHorizontalAlignment(SwingConstants.CENTER);
pacificDivision.add(lblNewLabel_1, BorderLayout.NORTH);
}
Replace
new JFrame();
by
new Frame();
And call pack() before setVisible(true).
Note that just adding a System.out.println() or using the debugger to step through your code would have allowed you to find this simple mistake. A debugger is a wonderful tool. use it.
I have problem with tabs in the JPanel. I know how to make new tabs in Mainframe, but I don't know how to make tabs into JPanel which is located in Mainframe.
Here are the pictures:
I have program looking like this -
http://www.bildites.lv/viewer.php?file=vklfhvfdfpwpcxllfqv.png
But I want to make it look like this -
http://www.bildites.lv/viewer.php?file=bvbrp4qfx2krn9bkx30j.png
And Here is my code of the blue JPanel:
package gui;
import java.awt.Color;
import javax.swing.JPanel;
public class CallsPanel extends JPanel {
private MainFrame frame;
Color color = new Color(99, 184, 255); // steelblue
public CallsPanel(MainFrame frame) {
this.frame = frame;
this.setLocation(0, 0);
this.setSize(300, 380);
this.setLayout(null);
this.setBackground(color);
this.initContent();
}
// -------------------------------------------------------------------------
// Declare New Things
private void initContent() {
// Add New Things
}
// -------------------------------------------------------------------------
}
Thanks a lot to people that will help!
JTabbedPane tabPane = new JTabbedPane();
JPanel panel1 = new JPanel();
JPanel panel2 = new JPanel();
JLabel label1 = new JLabel("Tab 1");
JLabel label2 = new JLabel("Tab 2");
panel1.add(label1);
panel2.add(label2);
tabPane.add("Tab 1", panel1);
tabPane.add("Tab 2", panel2);
this.add(tabPane);
Play around with the size/color/shape of the tabPane and see what works for you. But this is the basic of a tabPane.
See this simple runnable example
import java.awt.BorderLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTabbedPane;
import javax.swing.SwingUtilities;
import javax.swing.border.EmptyBorder;
public class MyPanel extends JPanel {
JButton button = new JButton("Button");
JTabbedPane tabPane = new JTabbedPane();
public MyPanel(){
JPanel panel1 = new JPanel();
JPanel panel2 = new JPanel();
tabPane.add("Panel 1", panel1);
tabPane.add("Panel 2", panel2);
tabPane.setBorder(new EmptyBorder(10, 10, 10, 10));
setLayout(new BorderLayout());
add(tabPane, BorderLayout.CENTER);
add(button, BorderLayout.SOUTH);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
#Override
public void run() {
JFrame frame = new JFrame();
frame.add(new MyPanel());
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLocationByPlatform(true);
frame.setSize(300, 300);
frame.setVisible(true);
}
});
}
}
I seem to be having some major issues with JLayeredPane. I have a BorderLayout() pane, and I'd like for the West-side element to contain a few JLayeredPane's on top of each other, so I can switch between them to show the right information.
The west pane should be 200 pixels wide and should be as long as the total window is. In my sample code I have added two layers to the JLayeredPanel, but they don't show up. They should be in the west pane.
Here is my code:
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JLayeredPane;
import javax.swing.JPanel;
public class Main {
private static JFrame mainFrame = new JFrame();
private static JPanel mainPane = new JPanel();
public Main(){}
public static void initGui(){
JLayeredPane westPanel = new JLayeredPane();
westPanel.setPreferredSize(new Dimension(200,0));
westPanel.setBackground(Color.blue);
JPanel layerOne = new JPanel();
layerOne.add(new JLabel("This is layer 1"));
westPanel.add(layerOne, new Integer(0), 0);
JPanel layerTwo = new JPanel();
layerTwo.add(new JLabel("This si layer 2"));
westPanel.add(layerTwo, new Integer(1), 0);
JPanel centerPanel = new JPanel();
centerPanel.setBackground(Color.yellow);
JPanel eastPanel = new JPanel();
eastPanel.setPreferredSize(new Dimension(200,0));
eastPanel.setBackground(Color.red);
mainPane = new JPanel(new BorderLayout());
mainPane.add(westPanel, BorderLayout.WEST);
mainPane.add(centerPanel, BorderLayout.CENTER);
mainPane.add(eastPanel, BorderLayout.EAST);
mainFrame = new JFrame("Learning to use JLayeredPane");
mainFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
mainFrame.setBounds(200, 200, 800, 500);
mainFrame.setContentPane(mainPane);
mainFrame.setVisible(true);
}
public static void main(String[] args) {
initGui();
}
}
What this results in:
JLayeredPane uses a null layout and so you are responsible for stating the size and location of all components added to it. If not they will default to a location of [0, 0] and a size of [0, 0].
try this, its working
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JLayeredPane;
import javax.swing.JPanel;
public class Main {
private static JFrame mainFrame = new JFrame();
private static JPanel mainPane = new JPanel();
public Main(){}
public static void initGui(){
JLayeredPane westPanel = new JLayeredPane();
westPanel.setLayout(null);
westPanel.setPreferredSize(new Dimension(200,0));
westPanel.setBackground(Color.blue);
JPanel layerOne = new JPanel();
layerOne.add(new JLabel("This is layer 1"));
layerOne.setBounds(0, 0, 100, 100);
westPanel.add(layerOne, new Integer(0), 0);
JPanel layerTwo = new JPanel();
layerTwo.add(new JLabel("This si layer 2"));
layerTwo.setBounds(0, 100, 100, 100);
westPanel.add(layerTwo, new Integer(1), 0);
JPanel centerPanel = new JPanel();
centerPanel.setBackground(Color.yellow);
JPanel eastPanel = new JPanel();
eastPanel.setPreferredSize(new Dimension(200,0));
eastPanel.setBackground(Color.red);
mainPane = new JPanel();
mainPane.setLayout(new BorderLayout());
mainPane.add(westPanel, BorderLayout.WEST);
mainPane.add(centerPanel, BorderLayout.CENTER);
mainPane.add(eastPanel, BorderLayout.EAST);
mainFrame = new JFrame("Learning to use JLayeredPane");
mainFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
mainFrame.setBounds(200, 200, 800, 500);
mainFrame.setContentPane(mainPane);
mainFrame.setVisible(true);
}
public static void main(String[] args) {
initGui();
}
}
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).
I am having some problem with CardLayout. I have a panel and a Next button on it. upon clicking on it i want to display the 2nd panel. In my code, when i click on the Next buton, the next panel is not displayed. Can someone help me solve this ?
package com.test;
import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import java.awt.CardLayout;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
public class CardLay extends JFrame {
private JPanel contentPane;
private CardLayout ca;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
CardLay frame = new CardLay();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the frame.
*/
public CardLay() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 450, 300);
ca =new CardLayout(0, 0);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
contentPane.setLayout(ca);
JPanel panel = new JPanel();
panel.setLayout(null);
contentPane.add("1",panel);
JButton btnNext = new JButton("NEXT");
btnNext.setBounds(131, 93, 117, 29);
btnNext.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
ca.show(contentPane,"1");
System.out.println("button clicked");
}
});
panel.add(btnNext);
JPanel panel_1 = new JPanel();
contentPane.add(panel_1, "name_1353086933711396000");
JCheckBox chckbxNewCheckBox = new JCheckBox("New check box");
panel_1.add(chckbxNewCheckBox);
}
}
You need to call:
ca.show(contentPane, "name_1353086933711396000");
For this to work you will have to add the second panel like this:
contentPane.add("name_1353086933711396000", panel_1);
When using CardLayout make sure to keep navigation buttons on a separate container other then the 'cards' themselves, so that they can be visible throughout the navigation process. Here you could place a new navigation container in the frame's BorderLayout.SOUTH position. For sequential navigation, the methods previous and next are available.
Also avoid using absolute positioning (null layout). See Doing Without a Layout Manager (Absolute Positioning).
public CardLay() {
setDefaultCloseOperation(EXIT_ON_CLOSE);
setSize(500, 400);
ca = new CardLayout(0, 0);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
contentPane.setLayout(ca);
JPanel panel1 = new JPanel();
panel1.add(new JButton("Test Button"));
contentPane.add("card1", panel1);
JPanel panel2 = new JPanel();
contentPane.add("card2", panel2);
JCheckBox chckbxNewCheckBox = new JCheckBox("New check box");
panel2.add(chckbxNewCheckBox);
JPanel navigationPanel = new JPanel();
JButton btnPrevious = new JButton("< PREVIOUS");
btnPrevious.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
ca.previous(contentPane);
}
});
navigationPanel.add(btnPrevious);
JButton btnNext = new JButton("NEXT >");
btnNext.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
ca.next(contentPane);
}
});
navigationPanel.add(btnNext);
add(contentPane);
add(navigationPanel, BorderLayout.SOUTH);
}
Recommended: How to Use CardLayout