How to swap 2 jPanel places? - java

So I created a new jFrame and in it I have 2 jPanels - jPanel1 and jPanel2.
I need these 2 jPanels to swap places, the first one goes to the place of the second, and the second to the place of the first (with everything that will be inside jPanels).
What is the best way to do this?

import javax.swing.*;
import java.awt.GridLayout;
public class Class {
static JFrame frame;
static JPanel panel1 = new JPanel();
static JPanel panel2 = new JPanel();
public static void main(String[] args) {
panel1.add(new JLabel("Panel 1"));
panel2.add(new JLabel("Panel 2"));
frame = new JFrame();
frame.getContentPane().setLayout(new GridLayout(1,2));
frame.setVisible(true);
frame.setSize(500,500);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().add(panel1);
frame.getContentPane().add(panel2);
new Timer(2000,e -> {
frame.getContentPane().removeAll();
frame.getContentPane().add(panel2);
frame.getContentPane().add(panel1);
frame.getContentPane().revalidate();
}).start();
}
}

It depends on the layout you use in your parent panel, but basically, you must remove both components (content1 and content2) from the common parent panel and add them again at the other position.
I made a simple example with a (horizontal) flow layout and two panels on it with just one label on it - but it should work for more complex panels, too.
import java.awt.FlowLayout;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.WindowConstants;
public class TestFrame extends JFrame {
public static void main(String[] args) throws InterruptedException {
TestFrame f = new TestFrame();
f.getContentPane().setLayout(new FlowLayout());
JPanel pnl1 = new JPanel();
pnl1.add(new JLabel("My first label"));
JPanel pnl2 = new JPanel();
pnl2.add(new JLabel("My second label"));
f.getContentPane().add(pnl1);
f.getContentPane().add(pnl2);
f.setSize(400,400);
f.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
f.setVisible(true);
// wait 2 seconds and switch panels
System.out.println("before sleep");
Thread.sleep(2000);
System.out.println("after sleep");
// remove the old content - could be more "precise" in real world - not just remove all components
f.getContentPane().removeAll();
// add the components in different order (in flow layout)
f.getContentPane().add(pnl2);
f.getContentPane().add(pnl1);
// render again
f.revalidate();
}
}

Related

How do I adjust the button sizes and move them to be in a different place?

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

Click a button to open a new JFrame / GUI,

I seem to not be able to find a way to get my code to work.
I am making a program and until now everything was working, i have some buttons and they do what they should.
But now i added a button that when a user click it, it should close the current GUI and open a new one.
I also want to point out that i created a new class for this new GUI.
The other GUI class that i want to call is the GuiCrafting, in that class the GUI is also all coded, and works if i call it on the Main.
My question is what do i type here (I tried a lot of things like dispose() etc but i just get error messages) :
public void actionPerformed(ActionEvent event) {
if( str.equals("Crafting")){
//insert code to call the GuiCrafting class and open his GUI
}
Thanks in advance and if you need something more please let me know.
Multiple JFrames are frowned upon as you can read about here and here
Perhaps what you want to use is a CardLayout which manages two or more components (usually JPanel instances) that share the same display space.
After clicking the button "Goto Card 2"
TestApp.java:
import java.awt.BorderLayout;
import java.awt.CardLayout;
import java.awt.event.ActionEvent;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
public class TestApp {
final static String CARD1 = "Card1";
final static String CARD2 = "Card2";
public TestApp() {
initComponents();
}
public static void main(String[] args) {
SwingUtilities.invokeLater(TestApp::new);
}
private void initComponents() {
JFrame frame = new JFrame("TestApp");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// create the panel that contains the "cards".
JPanel cards = new JPanel(new CardLayout());
// card 1 components
JButton buttonGotoCard2 = new JButton("Goto Card 2");
buttonGotoCard2.addActionListener((ActionEvent e) -> {
CardLayout cl = (CardLayout) (cards.getLayout());
cl.show(cards, CARD2);
});
// create card 1
JPanel card1 = new JPanel();
card1.add(new JLabel("Card 1"));
card1.add(buttonGotoCard2);
// card 2 components
JButton buttonGotoCard1 = new JButton("Goto Card 1");
buttonGotoCard1.addActionListener((ActionEvent e) -> {
CardLayout cl = (CardLayout) (cards.getLayout());
cl.show(cards, CARD1);
});
// create card 2
JPanel card2 = new JPanel();
card2.add(new JLabel("Card 2"));
card2.add(buttonGotoCard1);
// add cards to cards panel
cards.add(card1, CARD1);
cards.add(card2, CARD2);
frame.getContentPane().add(cards, BorderLayout.CENTER);
frame.pack();
frame.setVisible(true);
}
}
There is also a JDialog which could be what you want.
HOWEVER
You can easily do something like that (Open a JFrame from another If you must):
TestApp.java:
import java.awt.event.ActionEvent;
import java.awt.event.WindowAdapter;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
import javax.swing.border.EmptyBorder;
public class TestApp {
public TestApp() {
initComponents();
}
public static void main(String[] args) {
SwingUtilities.invokeLater(TestApp::new);
}
private void initComponents() {
JFrame mainFrame = new JFrame();
mainFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel panel = new JPanel();
panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS));
panel.setBorder(new EmptyBorder(10, 10, 10, 10));
JLabel label = new JLabel("JFrame 1");
JButton button = new JButton("Open JFrame 2");
button.addActionListener((ActionEvent e) -> {
this.showNewJFrame(new WindowAdapter() {
#Override
public void windowClosing(java.awt.event.WindowEvent e) {
// here we listen for the second JFrame being closed so we can bring back the main JFrame
mainFrame.setVisible(true);
}
});
// hide the main JFrame
mainFrame.setVisible(false);
});
panel.add(label);
panel.add(button);
mainFrame.add(panel);
mainFrame.pack();
mainFrame.setVisible(true);
}
private void showNewJFrame(WindowAdapter windowAdapter) {
JFrame frame2 = new JFrame();
frame2.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE); // we dont wnat to exit when this JFrame is closed
JPanel panel2 = new JPanel();
panel2.setLayout(new BoxLayout(panel2, BoxLayout.Y_AXIS));
panel2.setBorder(new EmptyBorder(10, 10, 10, 10));
JLabel label2 = new JLabel("JFrame 2");
panel2.add(label2);
frame2.add(panel2);
frame2.addWindowListener(windowAdapter);
frame2.pack();
frame2.setVisible(true);
}
}
This produces:
and when the "Open JFrame 2" is clicked:
and when JFrame 2 is closed it brings back the main JFrame via the WindowAdapter#windowClosing.

How do I put two Jpanels/Jbuttons in the borderlayout north section?

How do I display two JPanels in the 'North' in borderlayout?
Here's and example code that outputs a GUI with three distinct rows, Top, Middle, Bottom. There's one button covering the first row, 3 buttons covering the second row, and one covering the bottom row.
package borderLayoutDemo;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import java.awt.BorderLayout;
public class BorderLayoutDemo {
public static void main(String[] args) {
JFrame.setDefaultLookAndFeelDecorated(true);
JFrame fj = new JFrame("Demonstration of Border Layout");
fj.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JButton jbtn1 = new JButton("UP");
JButton jbtn2 = new JButton("DOWN");
JButton jbtn3 = new JButton("LEFT");
JButton jbtn4 = new JButton("RIGHT");
JButton jbtn5 = new JButton("MIDDLE");
JPanel pnl = new JPanel();
pnl.setLayout(new BorderLayout());
pnl.add(jbtn1, BorderLayout.NORTH);
pnl.add(jbtn2, BorderLayout.SOUTH);
pnl.add(jbtn3, BorderLayout.WEST);
pnl.add(jbtn4, BorderLayout.EAST);
pnl.add(jbtn5, BorderLayout.CENTER);
fj.add(pnl);
fj.pack();
fj.setVisible(true);
}
}
Output of above code:
output of above code
However, I'd like there to be two jpanels in the North section so it'd make 4 "rows" like this:
|---------------button--------------| //north
|---------------button2-------------| //north
----------------center--------------- //center
|---------------button3-------------| //south
I've tried simply just adding it as follows:
pnl.add(jbtn1, BorderLayout.NORTH);
pnl.add(jbtn2, BorderLayout.NORTH);
But what happens here is the second button just replaces the first one:
|---------------button2-------------| //north
----------------center--------------- //center
|---------------button3-------------| //south
How would I get two rows in the north layout area?
Creating a more complex GUI is straightforward when you think of a GUI as a JFrame with as many JPanels as necessary to define the GUI.
Here's the GUI you were looking for.
I created a JPanel for each section of the JFrame (NORTH, CENTER, and SOUTH). Each of those JPanels used a BorderLayout so that when you expand the GUI, the NORTH and SOUTH buttons stay the same height.
Here's the complete runnable example code.
import java.awt.BorderLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
public class BorderLayoutDemo implements Runnable {
public static void main(String[] args) {
SwingUtilities.invokeLater(new BorderLayoutDemo());
}
#Override
public void run() {
JFrame fj = new JFrame("Demonstration of Border Layout");
fj.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
fj.add(createNorthPanel(), BorderLayout.NORTH);
fj.add(createCenterPanel(), BorderLayout.CENTER);
fj.add(createSouthPanel(), BorderLayout.SOUTH);
fj.pack();
fj.setLocationByPlatform(true);
fj.setVisible(true);
}
private JPanel createNorthPanel() {
JPanel panel = new JPanel(new BorderLayout());
JButton button1 = new JButton("North Button");
panel.add(button1, BorderLayout.NORTH);
JButton button2 = new JButton("North Button 2");
panel.add(button2, BorderLayout.SOUTH);
return panel;
}
private JPanel createCenterPanel() {
JPanel panel = new JPanel(new BorderLayout());
JButton button = new JButton("Center Button");
panel.add(button, BorderLayout.CENTER);
return panel;
}
private JPanel createSouthPanel() {
JPanel panel = new JPanel(new BorderLayout());
JButton button = new JButton("South Button");
panel.add(button, BorderLayout.SOUTH);
return panel;
}
}

Java buttons not showing up on JFrame

I am fairly new to java, and I am starting a sort of glossary program.
At the start, I am trying to use a JFrame with buttons on.
But only 1 button shows up when I run it.
Also, I don't think I am positioning the buttons right.
package glossary;
import java.awt.BorderLayout;
import javax.swing.JFrame;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import javax.swing.*;
public class Glossary {
public static void main(String[] args) {
JFrame frame = new JFrame("Glossary");
frame.setVisible(true);
frame.setSize(400,200);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel panel1 = new JPanel();
frame.add(panel1);
JButton LookUpWord = new JButton("Look up word");
panel1.add(LookUpWord, BorderLayout.NORTH);
JPanel panel2 = new JPanel();
frame.add(panel2);
JButton SubmitNewWord = new JButton("Submit word");
panel2.add(SubmitNewWord, BorderLayout.SOUTH);
}
}
Please tell me what I am doing horribly wrong!
I think you are just getting mixed up about where to add your components. You probably aren't intending to add the buttons to NORTH and SOUTH, but rather adding the panels to the frame at NORTH and SOUTH. Also, wait until you've added all your components before calling frame.setVisible(true).
Try this:
public static void main(String[] args) {
JFrame frame = new JFrame("Glossary");
frame.setSize(400, 200);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JButton LookUpWord = new JButton("Look up word"); // create the button
JPanel panel1 = new JPanel(); // create the panel
panel1.add(LookUpWord); // add the button to the panel
frame.add(panel1, BorderLayout.NORTH); // add the panel to the frame
JButton SubmitNewWord = new JButton("Submit word");
JPanel panel2 = new JPanel();
panel2.add(SubmitNewWord);
frame.add(panel2, BorderLayout.SOUTH);
frame.setVisible(true);
}
Add frame.setVisible(true);
Hoping this tutorial help you
How to Make Frames

JButton disappears when adding JTextrea

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

Categories

Resources