Java swing; How to toggle panel's visibility? - java

i made this code to navigate trough panel1 and panel2
with buttons.
(button1 and button2) but when i run my code the frame stays empty.
Can somebody explain me what i'm doing wrong and how i can accomplish
toggling between panel1 and panel2 in this way? Starting with panel1 first
Code:
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JPanel;
import javax.swing.JFrame;
public class togglepanel {
public static void main(String[] args) {
final JFrame frame = new JFrame();
final JPanel panel1 = new JPanel();
final JPanel panel2 = new JPanel();
JButton button1 = new JButton("previous frame!");
JButton button2 = new JButton("next frame");
frame.setLocationRelativeTo(null);
frame.setResizable(true);
frame.setVisible(true);
frame.setSize(600, 400);
frame.add(panel1);
frame.add(panel2);
panel1.add(button2);
panel1.setVisible(true);
panel2.add(button1);
panel2.setVisible(false);
button1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
panel1.setVisible(true);
panel2.setVisible(false);
}
});
button2.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
panel1.setVisible(false);
panel2.setVisible(true);
}
});
}
}
thanks in advance

Use a layout manager.
frame.setLayout(new FlowLayout());

Another useful way to do this, and I think better is to use a CardLayout and to add both JPanels to a container that uses this CardLayout. You can then easily swap views by calling the CardLayout methods.
e.g.,
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class TogglePanel {
public static void main(String[] args) {
final CardLayout cardlayout = new CardLayout();
final JFrame frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
final Container contentPane = frame.getContentPane();
contentPane.setLayout(cardlayout);
final JPanel panel1 = new JPanel();
final JPanel panel2 = new JPanel();
JButton button1 = new JButton("previous frame!");
JButton button2 = new JButton("next frame");
contentPane.setPreferredSize(new Dimension(600, 400));
contentPane.add(panel1, "Panel 1");
contentPane.add(panel2, "Panel 2");
frame.pack();
frame.setLocationRelativeTo(null);
frame.setResizable(true);
frame.setVisible(true);
panel1.add(button2);
panel2.add(button1);
ActionListener btnListener = new ActionListener() {
public void actionPerformed(ActionEvent e) {
cardlayout.next(contentPane);
}
};
button1.addActionListener(btnListener);
button2.addActionListener(btnListener);
}
}

Related

Adding JButton to JPanel by pressing JButton problem

I'm creating a program for table reservations for practice. I came across the next problem: When I click on button "Create new table", a Button should be added on centerPanel, but it does not appear there.
Here's the code
import javax.swing.*;
import java.awt.event.*;
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.GridLayout;
public class CreateNewFloorV2 extends JFrame implements ActionListener{
JFrame frame=new JFrame("Create new table");
BorderLayout borderLayout=new BorderLayout();
JPanel centerPanel=new JPanel();
SpringLayout centerPanelLayout=new SpringLayout();
JPanel bottomPanel=new JPanel();
GridLayout bottomPanelLayout=new GridLayout(1,2);
JButton btn1=new JButton("Create new table");
JButton btn2=new JButton("Delete table");
//Constructor
public CreateNewFloorV2() {
frame.setSize(600, 400);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setResizable(false);
//Create layout
frame.setLayout(borderLayout);
frame.getContentPane().add(centerPanel, BorderLayout.CENTER);
centerPanel.setLayout(centerPanelLayout);
frame.getContentPane().add(bottomPanel, BorderLayout.SOUTH);
bottomPanel.setLayout(bottomPanelLayout);
bottomPanel.add(btn1);
bottomPanel.add(btn2);
btn1.addActionListener(this);
btn2.addActionListener(this);
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
//ActionListener
public void actionPerformed(ActionEvent e) {
if(e.getSource()==btn1) {
JButton newTable=new JButton("Table X");
centerPanel.add(newTable);
}
}
public static void main(String[] args) {
CreateNewFloorV2 newFloor=new CreateNewFloorV2();
}
}
I've tried putting
JButton newTable=new JButton("Table X");
centerPanel.add(newTable);
into constructor CreateNewFloorV2() and then it does appear. But I don't know why it doesn't appear when I click on button btn1 and how should I fix it?
Simply adding the component won't suffice - You have to tell the windowing toolkit to redraw the frame. Try using something like this:
if(e.getSource()==btn1) {
JButton newTable=new JButton("Table X");
centerPanel.add(newTable);
centerPanel.invalidate();
centerPanel.repaint();
}
Also see https://www.oracle.com/technetwork/java/painting-140037.html

Having a nightmare making a JButton Visible

I'm having a problem were a can't make any of the created buttons visible in my frame.
I am trying to create a Frame with 2 layouts the top part is a blank layout and the bottom layout will have three buttons.
import java.awt.BorderLayout;
import java.awt.Color;
import javax.swing.*;
public class Game_Initiliasation extends JFrame {
int Time;
int Difficulty;
JPanel TopPanel;
JPanel BottomPanel;
Game_Initiliasation(){
this.setDefaultCloseOperation(EXIT_ON_CLOSE);
this.setSize(300, 300);
this.setVisible(true);
TopPanel = new JPanel();
BottomPanel = new JPanel();
TopPanel.setLayout(new BorderLayout());
add(TopPanel, BorderLayout.CENTER);
add(BottomPanel, BorderLayout.SOUTH);
JButton Easy = new JButton("Easy");
JButton Medium = new JButton("Medium");
JButton Difficult = new JButton("Difficult");
/*Easy.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
actionPerformed(evt);
}
});*/
Easy.setVisible(true);
BottomPanel.add(Easy);
BottomPanel.add(Medium);
BottomPanel.add(Difficult);
}
}
this.add(BottomPanel); // Add this line, you will be able to see the buttons
import java.awt.BorderLayout;
import java.awt.Color;
import javax.swing.*;
public class Game_Initiliasation extends JFrame {
int Time;
int Difficulty;
JPanel TopPanel;
JPanel BottomPanel;
Game_Initiliasation(){
this.setDefaultCloseOperation(EXIT_ON_CLOSE);
this.setSize(300, 300);
this.setVisible(true);
TopPanel = new JPanel();
BottomPanel = new JPanel();
TopPanel.setLayout(new BorderLayout());
add(TopPanel, BorderLayout.CENTER);
add(BottomPanel, BorderLayout.SOUTH);
JButton Easy = new JButton("Easy");
JButton Medium = new JButton("Medium");
JButton Difficult = new JButton("Difficult");
/*Easy.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
actionPerformed(evt);
}
});*/
Easy.setVisible(true);
BottomPanel.add(Easy);
BottomPanel.add(Medium);
BottomPanel.add(Difficult);
this.add(BottomPanel); // Add this line, you will be able to see the buttons
}
}

Java JButton Can't open new form

I am a new to Java, I met a problem. Once I click the Button, it never shows me another form, just disappear. You can see, I set the button ActionListener, but it only run the second line (close the current form).
import java.awt.*;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import javax.swing.*;
public class login implements ActionListener {
public login(){
JFrame frame = new JFrame("Login");
frame.setLayout(new GridLayout(5,1));
JLabel label1 = new JLabel("User Name:");
JPanel panel1 = new JPanel();
frame.add(new JPanel());
frame.add(panel1);
panel1.add(label1);
JLabel label2 = new JLabel("Password:");
JPanel panel2 = new JPanel();
frame.add(panel2);
panel2.add(label2);
JPanel panel3 = new JPanel();
JButton button1 = new JButton("Register");
//button1.addActionListener(this);
JButton button2 = new JButton("Login");
//button2.addActionListener(this);
JButton button3 = new JButton("Cancel");
//button3.addActionListener(this);
panel3.add(button1);
panel3.add(button2);
panel3.add(button3);
frame.add(panel3);
JTextField JTF = new JTextField(12);
JPasswordField JPF = new JPasswordField(12);
panel1.add(JTF);
panel2.add(JPF);
frame.setResizable(false);
frame.setVisible(true);
frame.setSize(300, 200);
frame.setLocation(300,200);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
button1.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
new register();
frame.dispose();
}
});
}
public static void main(String[] args){
new login();
}
public void actionPerformed(ActionEvent e ){
}
}

PictureCount does not change

the code is used to create an application which shows picture and if your anwser is correct your supposed to see the next picture but the pictureCount does not go up. all of the variables ar declared after the main class and i created an Actionlistener to check if the awnser is correct.
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class Main {
public static void main(String[] args) {new Main().test();}
public int pictureCount = 1;
JFrame frame = new JFrame();
JButton button1 = new JButton("Submit");
JTextField text = new JTextField();
JPanel panel1 = new JPanel();
JPanel panel2 = new JPanel();
JPanel panel3 = new JPanel();
JLabel label = new JLabel(new ImageIcon("C:\\Users\\Admin\\Desktop\\practicum 3\\" + pictureCount + ".jpg"));
void test(){
button1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e)
{
if(text.getText().equals("5")){
pictureCount++;
new Main().test();
}
}
});
panel1.add(button1);
panel2.add(text);
panel3.add(label);
text.setPreferredSize(new Dimension(100,50));
panel1.setPreferredSize(new Dimension(1000, 200));
panel2.setPreferredSize(new Dimension(1000, 100));
panel3.setPreferredSize(new Dimension(1000, 450));
frame.getContentPane().add(BorderLayout.SOUTH, panel1);
frame.getContentPane().add(BorderLayout.CENTER, panel2);
frame.getContentPane().add(BorderLayout.NORTH, panel3);
frame.setSize(1000,750);
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setTitle("Operation Screen");
frame.setLocationRelativeTo(null);
}
}
You need to read in all the pictures as ImageIcons into an array or ArrayList, say called imageIconArray and then display imageIconArray[0] in your JLabel when you start.
When the button is pressed, increment pictureCount, and then reset the JLabel's icon via its setIcon(...) method:
// in the ActionListener code:
pictureCount++;
label.setIcon(imageIconArray[pictureCount];
Whatever you do, don't create a new Main object, despite what others might say. Why create a new GUI when all you need to do is swap displayed images?

Java Swing Removing Jpanel while clicking on Jbutton which is present in that jpanel which I want to remove

I am creating a GUI using Netbeans and my question is "how to remove the jpanel"...the hierarchical order as it is present Jframe->Jscrollpane->Jpanel->Jbutton. By clicking on the Jbutton I want to remove all components of the jpanel including the Jbutton(on which I am clicking) and the panel itself. Please do help.Urgent.THanks in advance
-Sayantan
private void b4ActionPerformed(java.awt.event.ActionEvent evt) {
final JPanel jp;
JTextField tf,of1,of2,xf,yf;
int i=1;
int m=(int)sp3.getValue();
JButton jb;
jp=new JPanel();
//jp.setLayout(new GridBagLayout());
DesignGridLayout layout = new DesignGridLayout(jp);
jsp2.getViewport().add(jp);
while(i<=m){
tf=new JTextField(5);
of1=new JTextField(5);
of2=new JTextField(5);
layout.row().grid(new JLabel("Type:")).indent(9).add(tf).grid(new JLabel("Length:")).indent().add(of1).grid(new JLabel("Breadth:")).indent().add(of2).empty();
fields1.add(tf);
fields1.add(of1);
fields1.add(of2);
xf=new JTextField(5);
yf=new JTextField(5);
layout.row().grid(new JLabel("X-axis:")).indent(9).add(xf).grid(new JLabel("Y-axis:")).indent().add(yf).empty(2);
fields1.add(xf);
fields1.add(yf);
i++;
}
jb=new JButton("Submit");
jb.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e)
{
for(JTextField field: fields1){
String s=field.getText();
lbr.setText(lbr.getText()+s);
}
lb3.setVisible(false);
sp3.setVisible(false);
b4.setVisible(false);
//jp.setVisible(false);
//jp.removeAll();
// jp.revalidate();
//jp.repaint();
// jsp2.remove(jp);
//jsp2.revalidate();
//jsp2.repaint();
}
});
layout.emptyRow();
layout.row().right().add(jb);
jp.revalidate();
jp.repaint();
// jsp2.revalidate();
//jsp2.repaint();
}
Note: I am using DesignGridLayout package.
This works:
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class ScratchSpace {
public static void main(String[] args) {
final JPanel panel = new JPanel();
panel.setOpaque(true);
panel.setBackground(Color.YELLOW);
panel.add(new JButton(new AbstractAction("Kill me") {
#Override
public void actionPerformed(ActionEvent e) {
panel.removeAll();
panel.revalidate();
panel.repaint();
}
}));
final JFrame frame = new JFrame();
frame.setContentPane(panel);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
frame.setVisible(true);
}
}
I want to remove all components of the jpanel including the Jbutton(on which I am clicking) and the panel itself.
Then you need code something like:
JButton button = (JButtton)event.getSource();
JPanel grandparent = button.getParent().getParent();
grandparent.removeAll();
grandparent.revalidate();
grandparent.repaint();
Although I question any code where I see a removeAll(). You might look into using a Card Layout.

Categories