How to replace current JPanel with another JPanel? - java

I am trying to transit from a UserAdminPanel to AdminLogin within the same JPanel when I press the Admin button.
UserAdmin Panel
transit to AdminLogin Panel
The problem I have now is that I am opening up a new panel instead of changing the current panel to the new panel.
This is my code for the UserAdminPanel
public class SelectAdminUserPanel extends JPanel
{
public SelectAdminUserPanel()
{
setLayout(new GridLayout(3,1));
JButton b1 = new JButton("User Login");
JButton b2 = new JButton("Admin Login");
JButton b3 = new JButton("Exit");
b1.addActionListener(new SelectUserButtonListener() );
b2.addActionListener(new SelectAdminButtonListener());
b3.addActionListener(new SelectExitButtonListener() );
add(b1);
add(b2);
add(b3);
}
private class SelectAdminButtonListener implements ActionListener
{
public void actionPerformed(ActionEvent event)
{
AdminModule am = new AdminModule();
am.run();
}
}
private class SelectUserButtonListener implements ActionListener
{
public void actionPerformed(ActionEvent event)
{
GameModule gm = new GameModule();
gm.run();
}
}
private class SelectExitButtonListener implements ActionListener
{
public void actionPerformed (ActionEvent event)
{
}
}
}
This is the code for the AdminLogin Panel
public class AdminLoginPanel extends JPanel
{
AdminLoginPanel()
{
JLabel pwlabel = new JLabel("Password");
JPasswordField pwfield = new JPasswordField(20);
JButton loginbutton = new JButton("Login");
add(pwlabel);
add(pwfield);
add(loginbutton);
}
}
I have looked at the following example and this example but it's not very applicable because it talks about CardLayout instead of like rewriting the current JPanel.

I think that you should have a reference to your main frame and just remove the components from it based on the button pressed and add only the required components. From what you say, UserAdminPanel is your main panel. I think it's added to a frame for which you can obtain a reference. When you click a button, you want to remove all the content shown on it and display only what the button clicked should show. I think it should look something like this:
private class SelectAdminButtonListener implements ActionListener {
public void actionPerformed(ActionEvent event) {
frame.getContentPane().removeAll();
AdminModule am = new AdminModule();
frame.add(am.getNewPanel());
frame.pack();
// am.run(); //it's not clear what does for you
}
}
Where the method getNewPanel() would return the underlying JPanel. I'm assuming that AdminModule has a reference to the AdminLoginPanel.

Related

How do I get my button actionListener to count the number of times someone clicks?

public class Button {
public static void main(String[] args) {
int numClicks = 0;
JButton button1 = new JButton ();
button1.setText("1 click = 1 dollar for animals you love");
JFrame god = new JFrame ();
JPanel panel = new JPanel();
god.getContentPane().add(button1);
god.add(button1);
god.setSize(new Dimension(400, 400));
god.setVisible(true);
}
public void actionPerformed(ActionEvent event) {
}
}
Can anyone help me figure out how to make the actionlistener count the number of times the button is clicks? Im a noobie coder (3 months) and Im really stuck on this
You need to add ActionListener to you button.
button1.addActionListener(new MyActionList());
//MyActionList is an object which implements ActionListener
In your case is the same object.
//---implements ActionListener---
public class Button implements ActionListener{
private int clickCount;
public Button() {
clickCount = 0;
JFrame god = new JFrame ();
god.setSize(new Dimension(400, 400));
//if you don't do this, your program still running, even if you click on X.
god.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel panel = new JPanel();
god.add(panel);
JButton button1 = new JButton("1 click = 1 dollar for animals you love");
//here we add ActionListener
button1.addActionListener(this);
//'this' refer to the current object. In our case 'new Button()' from 'main'...
//...which has implemented ActionListener.
panel.add(button1);
god.setVisible(true);
}
public static void main(String[] args){
new Button();
}
//Override from Interface ActionListener and...
//...here describe what happend when button was pressed.
#Override
public void actionPerformed(ActionEvent e) {
clickCount++;
System.out.println(clickCount);
}
}

How to be back on main class panel from panels of different class?

I have two classes one is main class and other is pro class. The main class has introductory panel which further shows the panel of pro class. The second panel of pro class has "Home" button which needs to show the main class panel. please tell me how do I make "Home" button work?
class proMain extends JPanel {
JPanel pan2 = new JPanel();
JButton b1, b2, b3;
CardLayout lay = new CardLayout();
public void pshow() {
// First Panel
pan.setBackground(Color.red);
b1 = new JButton("Next");
b1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent ae) {
if (ae.getActionCommand().equals("Next")) {
pan2.setVisible(true);
pan.setVisible(false);
}
}
});
pan.add(b1);
// Second Panel
b2 = new JButton("Previous");
b2.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent ae) {
if (ae.getActionCommand().equals("Previous")) {
pan.setVisible(true);
pan2.setVisible(false);
}
}
});
b3 = new JButton("Home");
b3.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent ae) {
if (ae.getActionCommand() == "Home") {
//
}
}
});
pan2.setBackground(Color.cyan);
pan2.add(b2);
pan2.add(b3);
pan.setVisible(true);
pan2.setVisible(false);
add(pan);
add(pan2);
}
}
Above class is proMain class and the main class is:
public class proDis {
public static void main(String[] args) {
JFrame fr = new JFrame("CrdLay");
JPanel pan3 = new JPanel();
JButton b = new JButton("Next");
CardLayout cl = new CardLayout();
fr.setLayout(cl);
b.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent ae) {
if (ae.getActionCommand().equals("Next")) {
pan3.setVisible(false);
proMain pm = new proMain();
pm.pshow();
pm.setLayout(cl);
fr.add(pm);
pm.setVisible(true);
}
}
});
pan3.setBackground(Color.gray);
pan3.add(b);
fr.setSize(100, 300);
fr.setVisible(true);
fr.add(pan3);
}
}
You are attempting to use a CardLayout, which is the correct approach. However, your implementation is incorrect.
The problem with your code is that you are not using the methods of the CardLayout to control which panel is being displayed. The CardLayout supports methods. like next(...) and previous(...) to sequentially move through all the panels and the method show(...) to display a specific panel.
So the first thing to do is give your panels a name when you add each panel to the layout. Then change your code to use the above methods to control which panel is being displayed. There is no need to play with the visibility of any panel, this is the job of the CardLayout.
Read the section from the Swing tutorial on How to Use CardLayout for more information and working examples.

Hide radio button on show jframe

I'm using netbeans 8. I have 2 radio button that I want to hide when the frame is shown. How can I do that? I successfully do that when I click other button such as this:
private void jRadioButton1ActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
jRadioButton3.setVisible(false);
jRadioButton4.setVisible(false);
}
but that's not what I want. I want to set it to invisible and only show it when I click other radio button. For some reason netbean prevent me from editing certain area in my source code so I can't test or explore it. Please help and thanks in advance.
Set the JRadioButton setVisible method as false by default and then change it when an action is performed.
For example, here under, the JRadioButtons will be visible once the first JRadioButton is selected. If it is deselected, they disappear.
I did it with a JRadioButton but it can be done with other components of course.
Solution
public class Test extends JFrame{
private JRadioButton but1, but2, but3;
public Test(){
setSize(new Dimension(200,200));
initComp();
setVisible(true);
}
private void initComp() {
but1 = new JRadioButton();
but1.addActionListener(new ActionListener(){
#Override
public void actionPerformed(ActionEvent e) {
but2.setVisible(but1.isSelected());
but3.setVisible(but1.isSelected());
}
});
but2 = new JRadioButton();
but2.setVisible(false);
but3 = new JRadioButton();
but3.setVisible(false);
setLayout(new FlowLayout());
JPanel pan = new JPanel();
pan.add(but1);
pan.add(but2);
pan.add(but3);
getContentPane().add(pan);
}
public static void main(String[] args) {
new Test();
}
}
You can set the radio button to invisible when you add it to the frame and make it visible on some event:
public class InvisibleRadioButton {
public static void main(String[] args) {
JFrame frame = new JFrame();
final JRadioButton jRadioButton1 = new JRadioButton("1");
JRadioButton jRadioButton2 = new JRadioButton("2");
frame.setLayout(new FlowLayout());
frame.add(jRadioButton1);
frame.add(jRadioButton2);
frame.setVisible(true);
jRadioButton1.setVisible(false); // initialize as invisible
jRadioButton2.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
jRadioButton1.setVisible(true); // set it to be visible
}
});
frame.pack();
}
}

Java - Button in a button

I'm making a program with 2 JButton, however I have a MyFrame class and the buttons are in a different class named KnoppenPanel. The problem is, when I do this I will get a JButton in a JButton. So I have my 2 buttons and another Button surrounding these. How do I solve this?
MyFrame:
public class MyFrame extends JFrame {
Uithangbord u = new Uithangbord();
KnoppenPanel kp = new KnoppenPanel();
public MyFrame() {
setLayout(new FlowLayout());
add(u);
add(kp);
setSize(280, 180);
setVisible(true);
setDefaultCloseOperation(EXIT_ON_CLOSE);
}
}
public class KnoppenPanel extends JButton implements ActionListener {
private JButton b, b2;
private JPanel p1;
Uithangbord bord = new Uithangbord();
public KnoppenPanel() {
p1 = new JPanel();
add(p1);
b = new JButton("Open");
p1.add(b);
b.addActionListener(this);
b2 = new JButton("Gesloten");
p1.add(b2);
b2.addActionListener(this);
}
public void actionPerformed(ActionEvent event) {
JButton knop = (JButton)(event.getSource());
if (knop == b) {
b.setEnabled(false);
b2.setEnabled(true);
bord.maakOpen();
}
if (knop == b2) {
b2.setEnabled(false);
b.setEnabled(true);
bord.maakGesloten();
}
}
}
You class is extending JButton. Then when you add your JPanel (with 2 JButton) you are adding this in a JButton.
I think what do you want is the KnoppenPanel have only the 2 JButtons then you just need to change:
public class KnoppenPanel extends JButton implements ActionListener {
By
public class KnoppenPanel extends JPanel implements ActionListener {
If you do this change, you can also directly add the JButton in the KnoppenPanel like that:
public KnoppenPanel() {
b = new JButton("Open");
add(b);
b.addActionListener(this);
b2 = new JButton("Gesloten");
add(b2);
b2.addActionListener(this);
}

close window on button click

Hello,
I am using Java Swing and I want to close a window on a button click. I don't know using an actionlistener as the best way to do this but currently I am having compilation errors with it so its must be incorrect.
Here my code:
public class assignment2
{
public static void main(String[] args){
MyFrame f = new MyFrame(); //open the inital gui interface
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setVisible(true); //set it visibile
}
}
//this is the initial gui screen, presenting user with options for which action they would like to take
//al actions for the gui are commenced here
class MyFrame extends JFrame{
public MyFrame(){
buttonPanel1 p = new buttonPanel1(); // add the buttons for this frame
add(p);
setSize(800,600);
setTitle("Travel Console");
setLocationRelativeTo(null);
}
}
class buttonPanel1 extends JPanel{
public buttonPanel1(){
//create buttons
JButton addItem = new JButton("Add an Item");
JButton deleteItem = new JButton("Delete an item");
JButton listItem = new JButton("List items");
JButton editItem = new JButton("Edit an item");
JButton bookFlight = new JButton("Book a flight");
JButton save = new JButton("Save data");
JButton load = new JButton("Load data");
JButton exit = new JButton("Exit");
//set layout manager for button panel
setLayout(new GridLayout(8,1,1,5));
//create buttons
add(addItem);
add(deleteItem);
add(listItem);
add(editItem);
add(bookFlight);
add(load);
add(save);
add(exit);
addItemListener addList = new addItemListener();
addItem.addActionListener(addList);
exitListener exitList = new exitListener();
exit.addActionListener(exitList);
}
}
//listener classes for the inital gui page. each button has its own actionlistener which launches the selected option
class addItemListener implements ActionListener{
public void actionPerformed(ActionEvent event){ //launch add item
addItemFrame addItem = new addItemFrame();
addItem.setDefaultCloseOperation(addItemFrame.DISPOSE_ON_CLOSE);
addItem.setVisible(true);
}
}
class addItemFrame extends JFrame{
public addItemFrame(){
addItemButtonPanel b = new addItemButtonPanel(); // add the buttons for this frame
add(b);
setSize(800,500);
setTitle("Add an Item");
setLocationRelativeTo(null);
}
}
//part of addItemFrame class
class addItemButtonPanel extends JPanel{
public addItemButtonPanel(){
JLabel selectItem = new JLabel("Select which item you would like to add:");
JButton newCustomer = new JButton("Customer");
JButton newflight = new JButton("Flight");
JButton newMovie = new JButton("Movie");
JButton goBack = new JButton("Return to main menu");
setLayout(new GridLayout(5,1,1,5));
add(selectItem);
add(newCustomer);
add(newflight);
add(newMovie);
add(goBack);
goBackListener gbList = new goBackListener();
goBack.addActionListener(gbList);
}
}
//listener classes for the addItemFrame
class goBackListener implements ActionListener{
public void actionPerformed(ActionEvent event){
addItemFrame.dispose();
}
}
The problem I am having is with the last class listed goBackListener, which effectively just closes the current window so the main menu screen is displayed again. I need a static reference to addItemFrame created in the addItemListener class. But changing it to static is an invalid modifier?
How can I fix it?
try this
//listener classes for the addItemFrame
class goBackListener implements ActionListener{
private addItemFrame frame;
public goBackListener(addItemFrame frame){
this.frame= frame;
}
public void actionPerformed(ActionEvent event){
frame.dispose();
}
}
and send an instance of addItemFrame to it's constructor

Categories