What I am trying to achieve is
Create a custom component (mypanel) that extends JPanel with JLabels and JButtons in it arranged via GridBagLayout.
Have a JFrame that would display multiple mypanel in a vertical stack and have its height change accordingly, depending on the number of mypanels added to it (width of the JFrame = width of mypanel).
When the JFrame's height becomes greater than the screen height, have a vertical scrollbar appear for scrolling
I have created mypanel successfully but having lot of trouble with the adding to the JFrame and setting its size, scrollbars part.
this is the code for my jframe
this.window = new JFrame("ADesktop Notifications");
this.window_panel = new JPanel();
this.window_panel_scroll = new JScrollPane(this.window_panel);
this.window.setBounds(this.top_left_x,this.top_left_y, this.width, this.height);
this.window_panel.setLayout(new FlowLayout());
this.window_panel.setAutoscrolls(true);
this.window.add(this.window_panel);
Try this example out (for dynamic expanding JFrame).
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
public class DynaFrame extends JFrame{
private JPanel basePnl = new JPanel();
public DynaFrame(){
this.setTitle("Dynamic panel addition");
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//this.setSize(600, 700);
this.add(getMainPanel());
this.setLocationRelativeTo(null);
this.pack();
this.setVisible(true);
}
public static void main(String[] args){
SwingUtilities.invokeLater(new Runnable() {
public void run() {
new DynaFrame();
}
});
}
public JPanel getMainPanel(){
basePnl.setLayout(new BoxLayout(basePnl, BoxLayout.Y_AXIS));
basePnl.add(getRowPanel());
return basePnl;
}
public JPanel getRowPanel(){
JPanel pnl = new JPanel();
GridLayout gLayout = new GridLayout();
gLayout.setColumns(4);
gLayout.setRows(1);
pnl.setLayout(gLayout);
pnl.add(new JLabel("Filetype"));
pnl.add(new JTextField());
pnl.add(new JButton("Browse"));
JButton addBtn = new JButton("Add");
addBtn.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent arg0) {
basePnl.add(getRowPanel());
DynaFrame.this.pack();
}
});
pnl.add(addBtn);
return pnl;
}
}
Related
I am trying to resize the center panel of my BorderLayout but the size is not changing. It keeps filling the rest of the frame that is available. I have tried setting the preferred size but has no effect. I would like how size of the frame but only need a portion of the center to be actually a panel for later use.
import javax.swing.*;
import javax.swing.border.Border;
import java.awt.*;
public class Gui extends JFrame {
Border blackline = BorderFactory.createLineBorder(Color.black);
private JPanel board;
private JPanel buttons;
private JButton setMissing, by4, by8;
public Gui(){
setUpGui();
}
public void setUpGui(){
this.setSize(1000,1000);
this.setTitle("Comp361 Assignment One");
addButtons();
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
board = new JPanel();
board.setPreferredSize(new Dimension(400,400));
this.add(board, BorderLayout.CENTER);
//Sboard.setBackground(Color.Gray);
board.setBorder(blackline);
this.setVisible(true);
}
public void addButtons(){
buttons = new JPanel(new FlowLayout());
setMissing = new JButton("Set X");
by4 = new JButton("4 by 4");
by8 = new JButton("8 by 8");
buttons.add(setMissing);
buttons.add(by4);
buttons.add(by8);
this.add(buttons,BorderLayout.PAGE_START);
}
public static void main (String[] args){
new Gui();
}
}
For extra padding around the central panel, you might put it to a panel with GridBagLayout (with no constraint) to center it, then add the GBL panel to the CENTER of the BorderLayout.
I'm writing this swing application and I'm using multiple panels in a BoxLayout format, but it seems to be that the empty space between the panels is being divided up between them and it looks really ugly. How do you adjust and customize how much space is put between panels? Sorry if this is a repost; I was unable to find an older post.
import java.awt.*;
import java.awt.datatransfer.Clipboard;
import java.awt.datatransfer.StringSelection;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
class gui extends JFrame implements ActionListener{
JFrame frame = new JFrame("Ark Admin Spawn Commands");
JPanel panel = new JPanel();
JComboBox dinos;
JComboBox corruptedDinos;
public JSlider dinoLevel;
JLabel input;
JLabel dino;
JLabel title;
gui(){
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel mainPanel = new JPanel();
mainPanel.setLayout(new BoxLayout(mainPanel, BoxLayout.Y_AXIS));
JPanel panelOne = new JPanel();
JPanel panelTwo = new JPanel();
JPanel panelThree = new JPanel();
mainPanel.add(panelOne);
mainPanel.add(panelTwo);
mainPanel.add(panelThree);
title = new JLabel("Spawning Dinos");
String[] dinoNames= {"The Island:"};
String[] corruptedDinoNames = {"Extinction:"};
dinos = new JComboBox(dinoNames);
corruptedDinos = new JComboBox(corruptedDinoNames);
dinoLevel = new JSlider(JSlider.HORIZONTAL, 1, 600, 400);
dinoLevel.setMajorTickSpacing(20);
dinoLevel.setPaintTicks(true);
input = new JLabel("Select Level: ");
event e = new event();
dinoLevel.addChangeListener(e);
dinos.addActionListener(this);
corruptedDinos.addActionListener(this);
panelOne.add(title);
panelTwo.add(input);
panelTwo.add(dinoLevel);
panelThree.add(dinos);
panelThree.add(corruptedDinos);
this.getContentPane().add(mainPanel);
this.setTitle("Ark Admin Spawn Commands");
this.setSize(600, 600);
this.setVisible(true);
}
public static void main (String args[]) {
new gui();
}
public class event implements ChangeListener{
#Override
public void stateChanged(ChangeEvent e) {
int value = dinoLevel.getValue();
input.setText("Level: " + value);
}
}
#Override
public void actionPerformed(ActionEvent arg0) {
}
}
but it seems to be that the empty space between the panels is being divided up between them
Correct. A BoxLayout will attempt to allocate extra space to all components.
However, it will respect the maximum size of each panel.
So to prevent the panels height from growing you can use code like:
JPanel panelOne = new JPanel()
{
#Override
public dimension getMaximumSize()
{
Dimension d = getPreferredSize()
d.width = Integer.MAX_VALUE;
return d;
}
};
Or because the default layout manager of the frame is a BorderLayout, you can just use:
//this.getContentPane().add(mainPanel);
add(mainPanel, BorderLayout.PAGE_START); // getContentPane() is not needed
The PAGE_START of the BorderLayout respects the preferred height of the component.
The assignment is simple, all we need to do is have the code create a window with a red panel with a single button and label. Here is the code thus far as well as the tester class.
I got the label to display on the window, but its in a weird place. I cant get the button to display at all as well getting the background to display as red.
This is where I'm having trouble with the most:
import java.awt.Color;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
public class MyCustomFrame extends JFrame
{
public MyCustomFrame()
{
createComponents();
setSize(FRAME_WIDTH, FRAME_HEIGHT);
}
private void createComponents()
{
JPanel panel=new createPanel();
button=new JButton("Push Me");
label=new JLabel("This is a label");
add(button);
add(label);
}
private JButton button;
private JLabel label;
final int FRAME_WIDTH = 800;
final int FRAME_HEIGHT = 800;
public void createPanel()
{
JPanel panel=new JPanel();
panel.setBackground(Color.RED);
//button=new JButton("Push Me");
//label=new JLabel("This is a label");
}
public void createFrame()
{
JFrame frame=new JFrame();
add(frame);
frame.setVisible(true);
}
}
And this is the tester class:
import javax.swing.JFrame;
public class MyCustomFrameViewer
{
public static void main(String[] args)
{
MyCustomFrame frame = new MyCustomFrame();
frame.setTitle("My first frame");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
}
You create the JPanel, panel, but add nothing to it, and then never add the panel to your JFrame. You should add your components to the JPanel, panel, and then add the panel object to your JFrame.
Note that a JPanel uses FlowLayout by default, and so it will more easily accept multiple other components without special add methods. The JFrame's contentPane uses BorderLayout which is slightly more complicated to use.
Add the panel to the frame instead of the label and button, and add the label and button to the panel...
private void createComponents()
{
JPanel panel=new createPanel();
add(panel);
button=new JButton("Push Me");
label=new JLabel("This is a label");
panel.add(button);
panel.add(label);
}
You may also want to take a look at Initial Threads and make sure you are creating your UI from within the context of the Event Dispatching Thread
Some side notes...
This scares me...
public void createFrame()
{
JFrame frame=new JFrame();
add(frame);
frame.setVisible(true);
}
You're trying to add a frame to a frame, which is an illegal operation in Swing, but thankfully, you're not actually calling it from what I can see.
Instead of using setSize(FRAME_WIDTH, FRAME_HEIGHT);, you may wish to use pack instead.
See also the tips in comments in this example:
import java.awt.*;
import javax.swing.*;
import javax.swing.border.EmptyBorder;
// Don't extend frame, just use an instance
//public class MyCustomFrame extends JFrame {
public class MyCustomFrame {
private JFrame frame;
private JButton button;
private JLabel label;
private JPanel panel;
// better to override the preferred size of the component of interest
final int GAME_WIDTH = 300;
final int GAME_HEIGHT = 100;
public MyCustomFrame() {
createComponents();
// better to override the preferred size of the component of interest
//setSize(GAME_WIDTH, GAME_HEIGHT);
}
private void createComponents() {
// compilation error! createPanel() does not return anything..
//JPanel panel = new createPanel();
createPanel();
// create the frame!
createFrame();
}
private void createPanel() {
// creates a local instance
//JPanel panel = new JPanel();
panel = new JPanel() {
/* override the preferred size of the component of interest */
#Override
public Dimension getPreferredSize() {
return new Dimension(GAME_WIDTH, GAME_HEIGHT);
}
};
panel.setBackground(Color.RED);
button = new JButton("Push Me");
label = new JLabel("This is a label");
panel.add(button);
panel.add(label);
}
private void createFrame() {
// create a local instance of a JFrame that goes out of scope at end
// of method..
//JFrame frame = new JFrame();
frame = new JFrame("My first frame");
// add the panel to the frame!
frame.add(panel);
// better to use DISPOSE_ON_CLOSE
//frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
// a good way to position a GUI
frame.setLocationByPlatform(true);
// this was trying to add a JFrame to another JFrame
//add(frame);
frame.pack();
}
public final JFrame getFrame() {
return frame;
}
public static void main(String[] args) {
Runnable r = new Runnable() {
#Override
public void run() {
MyCustomFrame myFrame = new MyCustomFrame();
JFrame frame = myFrame.getFrame();
frame.setVisible(true);
}
};
// Swing GUIs should be created and updated on the EDT
// http://docs.oracle.com/javase/tutorial/uiswing/concurrency
SwingUtilities.invokeLater(r);
}
}
I'm trying to do the following layout, where all JPanels are visible with the exception of panel2 when the program starts. When the user clicks btn1 then JCalendar and panel3 are set to invisible and panel2 becomes visible. The problem I'm having is that panel2 is not showing up with btn1 is clicked. However if i change the borderlayout of panel2 to one that is not being used (in this case WEST) it will show when the button is clicked, but its aligned on the left side and i want it to be centered across the form.
Code:
public class GUI extends JFrame implements ActionListener, PropertyChangeListener
{
private JPanel panel1, panel2, panel3;
private com.toedter.calendar.JCalendar calendar;
private Button btn1, btn2;
private JLabel label1, label2;
public GUI()
{
init();
}
private void init()
{
//panel1 components
panel1 = new JPanel();
btn1 = new JButton("Click me");
panel1.add(btn1);
//panel2 components
panel2 = new JPanel();
label1 = new JLabel("Time:");
label2 = new JLabel("Date:");
panel2.add(label1); panel2.add(label2);
//JCalendar
calendar = new com.toedter.calendar.JCalendar();
//panel3
panel3 = new JPanel();
//Add panels to JFrame
add(panel1, BorderLayout.NORTH);
add(calendar, BorderLayout.CENTER);
add(panel2, BorderLayout.CENTER); //if i set this to WEST it show!!
add(panel3, BorderLayout.EAST);
//event handling
btn1.addActionListener(this);
//hide panel2
panel2.setVisible(false);
pack();
}
public void actionPerformed(ActionEvent ae)
{
if(ae.getSource().equals(btn1)
{
calendar.setVisible(false);
panel3.setVisible(false);
panel2.setVisible(true); //make panel2 visible
panel2.updateUI();
revalidate();
repaint();
}
}
public static void main(String args[])
{
javax.swing.SwingUtilities.invokeLater(new Runnable()
{
public void run()
{
new GUI().setVisible(true);
}
});
}
When i click btn1, JCalendar and panel3 is invisible but panel2 does not show
There are a number of issues that I can find...
BorderLayout will only ever allow a single component to occupy any given position. That is, two components can not share the CENTER position at the same time, regardless if one is invisible or not.
You should never call updateUI, this is used to tell UI components that the look and feel has changed that they should update in response to it.
Use revalidate to tell the container that some change has occurred to the layout that it should perform a new layout process...
Before click...
After Click...
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.border.LineBorder;
public class GUI extends JFrame implements ActionListener, PropertyChangeListener {
private JPanel panel1, panel2, panel3;
// private com.toedter.calendar.JCalendar calendar;
private JPanel calendar;
private JButton btn1, btn2;
private JLabel label1, label2;
public GUI() {
setDefaultCloseOperation(EXIT_ON_CLOSE);
init();
}
private void init() {
//panel1 components
panel1 = new JPanel();
btn1 = new JButton("Click me");
panel1.add(btn1);
//panel2 components
panel2 = new JPanel();
label1 = new JLabel("Time:");
label2 = new JLabel("Date:");
panel2.add(label1);
panel2.add(label2);
//JCalendar
calendar = new JPanel();//new com.toedter.calendar.JCalendar();
calendar.setBorder(new LineBorder(Color.RED));
calendar.add(new JLabel("Calendar"));
//panel3
panel3 = new JPanel();
panel3.setBorder(new LineBorder(Color.BLUE));
panel3.add(new JLabel("Panel3"));
panel2.setBorder(new LineBorder(Color.GREEN));
//Add panels to JFrame
add(panel1, BorderLayout.NORTH);
add(calendar, BorderLayout.WEST);
add(panel2, BorderLayout.CENTER);
add(panel3, BorderLayout.EAST);
//event handling
btn1.addActionListener(this);
//hide panel2
panel2.setVisible(false);
pack();
}
public void actionPerformed(ActionEvent ae) {
if (ae.getSource().equals(btn1)) {
calendar.setVisible(false);
panel3.setVisible(false);
panel2.setVisible(true); //make panel2 visible
// panel2.updateUI();
revalidate();
repaint();
}
}
public static void main(String args[]) {
javax.swing.SwingUtilities.invokeLater(new Runnable() {
public void run() {
new GUI().setVisible(true);
}
});
}
#Override
public void propertyChange(PropertyChangeEvent evt) {
}
}
Now, I'm pretty sure this won't meet you over all requirements (as I see them)...You have at least two options...
Remove the Calendar component and add panel2 to the CENTER position when the button is clicked
Preferably, use a CardLayout
BorderLayout doesn't do overlapping, IIRC none of the layout managers are about "overlapping layout".
You'll need to do it a different way -- try using JLayeredPane with your existing JPanel & BorderLayout as the bottom layer, and then your (optional) panel as the top layer.
See: http://docs.oracle.com/javase/tutorial/uiswing/components/layeredpane.html
I am new to Java and mostly CardLayout. I want to simply switch "windows" represented by JPanels. I read somewhere that job for CardLayout. But my problem is, when add chatPanel to mainPanel (this is the CardLayout one), it shifts the content of connectPanel several pixels to the top, away from its centered position. Is I skip in my code createChatPanel(), its where it should be.
I have this code:
package App;
import java.awt.CardLayout;
import java.awt.Dimension;
import java.awt.GridBagLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
import Validators.*;
public class GUI {
private JFrame mainFrame = null;
private JPanel mainPanel = null;
private CardLayout cl = new CardLayout();
public GUI(){
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
}
catch (UnsupportedLookAndFeelException e) {
}
catch (ClassNotFoundException e) {
}
catch (InstantiationException e) {
}
catch (IllegalAccessException e) {
}
mainFrame = new JFrame("MainChat");
mainFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
mainFrame.setSize(640,480);
mainFrame.setLocationRelativeTo(null);
mainFrame.setResizable(false);
mainFrame.setLayout(new GridBagLayout());
JMenuBar menuBar = new JMenuBar();
JMenu menuFile = new JMenu("Soubor");
JMenu menuHelp = new JMenu("Nápověda");
menuBar.add(menuFile);
menuBar.add(menuHelp);
menuFile.add(new JMenuItem("Nové Připojení"));
menuFile.add(new JSeparator());
menuFile.add(new JMenuItem("Konec"));
menuHelp.add(new JMenuItem("O programu"));
mainFrame.setJMenuBar(menuBar);
createMainPanel();
createConnectPanel();
createChatPanel();
mainFrame.setVisible(true);
}
public void createMainPanel() {
mainPanel = new JPanel(cl);
mainFrame.add(mainPanel);
}
public void createConnectPanel() {
JPanel connectPanel = new JPanel();
mainPanel.add(connectPanel,"connectPanel");
JTextField ip = new JTextField();
ip.setDocument(new JTextFieldLimit(15));
ip.setColumns(11);
JLabel iplabel = new JLabel("IP:");
connectPanel.add(iplabel);
connectPanel.add(ip);
JButton connect = new JButton("Connect");
connect.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
cl.show(mainPanel,"chatPanel");
}
});
connectPanel.add(connect);
}
public void createChatPanel(){
JPanel chatPanel = new JPanel();
mainPanel.add(chatPanel,"chatPanel");
JTextArea chatbox = new JTextArea();
chatbox.setPreferredSize(new Dimension(200,200));
chatPanel.add(chatbox);
}
}
Please, what I messed up? Thanks.
Since you are adding two JPanels to your main JPanel,
these two panels both need to fit within the main panel.
If one of the inner panels is much larger than the other one,
the main panel will adjust to fit the larger one.
E.g. commenting this line:
chatbox.setPreferredSize(new Dimension(200,200));
would cause your text field to stay put. This is because the
chatbox would not cause the container to resize.
Also note that the main panel is not initially the same size as your
main frame, since you have not set the size of the main panel.
If you would set the size of the connectPanel to the same size
as your main frame, the connectPanel would not
be automatically resized when adding the chatPanel (as a
consequence of the mainPanel being resized)
So what you could do is add the middle line in:
JPanel connectPanel = new JPanel();
connectPanel.setSize(640, 480);
mainPanel.add(connectPanel, "connectPanel");
, which probably would solve your problem.
Although this would work, I definitely recommend using
MIG Layout for
all your GUI designing. It will save you plenty of time if
you take an hour to learn it. It will also save you from
having to set sizes manually (and thereby saving you from
having to rewrite half your GUI code with every design change).
If you want a JPanel centered in another, place your connectPanel in another JPanel that acts as a dumb container, and have this container use GridBagLayout. Then if you add the connectPanel to the container without any GridBagConstraints, it will be added to the default position for GridBagLayout which is centered. You can then add this container JPanel to your mainPanel using the same constant that you would have used for your connectPanel.
I would tend to let the layouts determine the size of components and avoid using setSize and even setPreferredSize, and would definitely call pack() on my JFrame prior to setting it visible. You definitely don't want to set the size or preferredSize of your JTextField, but rather set its columns and rows and place it in a JScrollPane, and then add that JScrollPane to the view.
Edit:
Here's an example that shows placement of something like your connect panel at the top, middle and bottom of a small gui. Just press the "Next" button to see what I mean:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
#SuppressWarnings("serial")
public class GUI2 extends JPanel {
public static final String CONNECT_NORTH = "connect north";
public static final String CONNECT_CENTER = "connect center";
private static final String CONNECT_SOUTH = "connect south";
private static final String CHAT_PANEL = "chat panel";
private CardLayout cardlayout = new CardLayout();
public GUI2() {
setLayout(cardlayout);
add(createConnectPanel(BorderLayout.NORTH), CONNECT_NORTH);
add(createConnectPanel(BorderLayout.CENTER), CONNECT_CENTER);
add(createConnectPanel(BorderLayout.SOUTH), CONNECT_SOUTH);
add(createChatPanel(), CHAT_PANEL);
}
public void nextPanel() {
cardlayout.next(this);
}
private JPanel createConnectPanel(String borderlayoutLocation) {
JPanel innerPanel = new JPanel();
innerPanel.add(new JLabel("IP:"));
innerPanel.add(Box.createHorizontalStrut(5));
innerPanel.add(new JTextField(11));
innerPanel.add(Box.createHorizontalStrut(5));
innerPanel.add(new JButton(new AbstractAction("Next") {
#Override
public void actionPerformed(ActionEvent arg0) {
GUI2.this.nextPanel();
}
}));
JPanel innerPanel2 = new JPanel(new GridBagLayout());
innerPanel2.add(innerPanel);
JPanel connectPanel = new JPanel(new BorderLayout());
connectPanel.add(innerPanel2, borderlayoutLocation);
return connectPanel;
}
private JPanel createChatPanel() {
JPanel chatPanel = new JPanel(new BorderLayout(5, 5));
chatPanel.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));
chatPanel.add(new JScrollPane(new JTextArea(15, 30)), BorderLayout.CENTER);
chatPanel.add(new JTextField(), BorderLayout.SOUTH);
return chatPanel;
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
createGui();
}
});
}
private static void createGui() {
JFrame frame = new JFrame("App");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().add(new GUI2());
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
}