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).
Related
This question already has answers here:
Add Object to JPanel after button click
(3 answers)
Closed 7 years ago.
I'm trying to get this code to stack each JLabel vertically with a new message (when add message is called). That way you can see every message by scrolling. I'm new to this so any thoughts? Right now, the code just replaces the textLabel with the new message.
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.FlowLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.SwingConstants;
import javax.swing.JScrollPane;
import javax.swing.border.LineBorder;
import javax.swing.BoxLayout;
public class ChatWindow {
static JFrame frame;
static JScrollPane jScrollPane;
public ChatWindow() {
frame = new JFrame("Ring Chat");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JLabel label = new JLabel("Label");
label.setPreferredSize(new Dimension(100, 100));
jScrollPane = new JScrollPane(label);
JLabel textLabel = new JLabel("Welcome to Ring Chat!", SwingConstants.CENTER);
textLabel.setPreferredSize(new Dimension(300, 100));
jScrollPane.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
jScrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
jScrollPane.getViewport().add(textLabel);
frame.add(jScrollPane, BorderLayout.CENTER);
frame.setSize(400, 800);
frame.setVisible(true);
}
public static void addMessage(String msg) {
JLabel textLabel = new JLabel(msg, SwingConstants.CENTER);
textLabel.setPreferredSize(new Dimension(300, 100));
jScrollPane.getViewport().add(textLabel, null);
frame.add(jScrollPane, BorderLayout.CENTER);
}
public static void createWindow() {
ChatWindow stuff = new ChatWindow();
}
public static void updateWindow() {
frame.setVisible(true);
}
}
just add variable of JPanel
static JScrollPane jScrollPane;
static JPanel panel;
initialize it after JLabel
panel = new JPanel();
panel.setSize(500, 500);
panel.setLayout(new GridLayout(20, 1));
jScrollPane = new JScrollPane(panel);
panel.add(label);
JLabel textLabel = new JLabel("Welcome to Ring Chat!", SwingConstants.CENTER);
textLabel.setPreferredSize(new Dimension(300, 100));
panel.add(textLabel);
and in addMessage(String msg) method add new JLabel to JPanel like this.
public static void addMessage(String msg) {
JLabel textLabel = new JLabel(msg, SwingConstants.CENTER);
textLabel.setPreferredSize(new Dimension(300, 100));
panel.add(textLabel);
frame.add(jScrollPane, BorderLayout.CENTER);
}
rest are same..
rather than adding JLabel use JPanel and at runtime add label on that JPanel. It will solve your problem..
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 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'm trying to align a JLabel and a JScrollPane (containing a JTextArea) to the left of a JPanel. When I put the JTextArea directly in the panel, the alignment is correct. The alignment is only incorrect if the JTextArea is in the scroll pane.
import javax.swing.BoxLayout;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
public class Main {
public static void main(String[] args) {
JDialog dialog = new JDialog();
JPanel panel = new JPanel();
panel.setLayout(new BoxLayout(panel, BoxLayout.PAGE_AXIS));
panel.add(new JLabel("My Label"));
// panel.add(new JTextArea(3, 15));
panel.add(new JScrollPane(new JTextArea(3, 15)));
dialog.add(panel);
dialog.pack();
dialog.setVisible(true);
}
}
The first image below is with the scroll pane and the second image is without it. How can I align the scroll pane correctly?
Try to use alignmentX:
import java.awt.Component;
import javax.swing.*;
public class Main {
public static void main(String[] args) {
JDialog dialog = new JDialog();
JPanel panel = new JPanel();
panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS));
JLabel label = new JLabel("My Label");
label.setAlignmentX(Component.LEFT_ALIGNMENT);
panel.add(label);
JScrollPane pane = new JScrollPane(new JTextArea(3, 15));
pane.setAlignmentX(Component.LEFT_ALIGNMENT);
panel.add(pane);
dialog.add(panel);
dialog.pack();
dialog.setVisible(true);
}
}
Replace:
panel.add(new JLabel("My Label"));
By:
JPanel labelPan = new JPanel(new FlowLayout(FlowLayout.LEFT);
labelPan.add(new JLabel("My Label"));
panel.add(labelPan);
I want to know how to change the content of a JFrame at runtime. Like adding a new JPanel and removing the old JPanel.
You can consider using CardLayout to change the active panel in a frame.
Changing JPanel at runtime here is the Code :
package stack;
import java.awt.BorderLayout;
import java.awt.Dimension;
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;
public class RemoveAndAddPanel implements ActionListener{
JFrame frame;
JPanel firstPanel;
JPanel secondPanel;
JPanel controlPanel;
JButton nextButton;
JPanel panelContainer;
JButton preButton;
JPanel contentPane;
public RemoveAndAddPanel() {
JFrame.setDefaultLookAndFeelDecorated(true);
frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
firstPanel = new JPanel();
firstPanel.add(new JLabel("FirstPanel"));
firstPanel.setPreferredSize(new Dimension(100,100));
secondPanel = new JPanel();
secondPanel.add(new JLabel("Second panel"));
secondPanel.setPreferredSize(new Dimension(100,100));
panelContainer = new JPanel();
contentPane = new JPanel(new BorderLayout());
nextButton = new JButton("Next panel");
preButton = new JButton("PreButton");
controlPanel = new JPanel();
nextButton.addActionListener(this);
preButton.addActionListener(this);
preButton.setEnabled(false);
controlPanel.add(preButton);
controlPanel.add(nextButton);
panelContainer.setLayout(new BorderLayout());
panelContainer.add(firstPanel,BorderLayout.CENTER);
contentPane.add(controlPanel, BorderLayout.SOUTH);
contentPane.add(panelContainer,BorderLayout.CENTER);
frame.setContentPane(contentPane);
frame.setVisible(true);
frame.setSize(300,100);
}
#Override
public void actionPerformed(ActionEvent e) {
if(e.getSource() == nextButton) {
panelContainer.removeAll();
panelContainer.setSize(0,0);
panelContainer.setSize(secondPanel.getSize());
panelContainer.add(secondPanel,BorderLayout.CENTER);
panelContainer.revalidate();
nextButton.setEnabled(false);
preButton.setEnabled(true);
}
if (e.getSource() == preButton) {
panelContainer.removeAll();
panelContainer.setSize(0,0);
panelContainer.setSize(firstPanel.getSize());
panelContainer.add(firstPanel,BorderLayout.CENTER);
nextButton.setEnabled(true);
preButton.setEnabled(false);
}
}
public static void main(String args[]) {
new RemoveAndAddPanel();
}
}
JFrame.setContentPane()