Moving method after JButton selection - java

When I select the first option and hit select the JFrame stays as is and I want it to close and move into the next method being called and open a different JFrame. Can anyone see the problem? I can't figure out where I am going wrong
public class GUI extends JFrame
{
public static void main(String args [])
{
final JFrame frame = new JFrame("Choose an option");
frame.setSize(350, 180);
frame.setDefaultCloseOperation(
JFrame.EXIT_ON_CLOSE);
frame.setLayout(new GridLayout(0, 1, 0, 0));
final JRadioButton sb = new JRadioButton("Student");
final JRadioButton lb = new JRadioButton("Lecturer");
final JRadioButton cdb = new JRadioButton("Course Director");
final JRadioButton ab = new JRadioButton("Admin");
ButtonGroup buttonGroup = new ButtonGroup();
buttonGroup.add(sb);
buttonGroup.add(lb);
buttonGroup.add(cdb);
buttonGroup.add(ab);
JPanel panel = new JPanel();
panel.add(sb);
panel.add(lb);
panel.add(cdb);
panel.add(ab);
frame.getContentPane().add(panel);
panel.setLayout(new GridLayout(0, 1, 0, 0));
JButton select = new JButton("Select");
JButton cancel = new JButton("Cancel");
select.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e)
{
if(sb.isSelected())
{
frame.dispose();
StudentGUI();
}
else if(lb.isSelected())
System.out.println("Lecturer");
else if(cdb.isSelected())
System.out.println("Course Director");
else if(ab.isSelected())
System.out.println("Admin");
}
});
cancel.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e)
{
System.exit(0);
}
});
JPanel panel2 = new JPanel();
panel2.add(select);
panel2.add(cancel);
panel2.setLayout(new FlowLayout(FlowLayout.CENTER, 20, 0));
frame.getContentPane().add(panel2);
frame.setVisible(true);
}
public static void StudentGUI()
{
JFrame frame1 = new JFrame("Input Username");
frame1.setSize(350, 180);
frame1.setDefaultCloseOperation(
JFrame.EXIT_ON_CLOSE);
JTextField tf = new JTextField("Input username here");
JButton submit = new JButton("Submit");
JPanel panel1 = new JPanel();
panel1.add(tf);
panel1.add(submit);
frame1.getContentPane().add(panel1);
}

You forgot to set frame1.setVisible(true); in your StudentGUI method, and you never close the window in the first method (use yourframe.dispose()).
So try:
public void actionPerformed(ActionEvent e)
{
if(sb.isSelected())
StudentGUI();
else if(lb.isSelected())
System.out.println("Lecturer");
else if(cdb.isSelected())
System.out.println("Course Director");
else if(ab.isSelected())
System.out.println("Admin");
yourframe.dispose();//don't know your frame variable
}
public static void StudentGUI()
{
JFrame frame1 = new JFrame("Input Username");
frame1.setSize(350, 180);
frame1.setDefaultCloseOperation(
JFrame.EXIT_ON_CLOSE);
frame1.setVisible(true);
//code omitted
}

Related

JPanel on CardLayout Swing Java

I am new to Java and I am making a navigation through CardLayout in Swing. Basically I have two buttons on a JFrame and when I click on one button it should go to card 1 where I have kept two buttons on JPanel, card1 and when I click on another button it should go to card 2 where I have kept a JTextField on panel card2. But it is not happening.
Can anyone fix it?
My code is as below.
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class CardLayoutTest extends JFrame implements ActionListener {
JFrame f;
JButton b;
JButton c;
JPanel panel;
JPanel cards;
JPanel card1;
JPanel card2;
CardLayout card;
Container pane;
final String card1Text = "One";
final String card2Text = "Two";
CardLayoutTest() {
}
public void passBtn() {
f=new JFrame("Card Layout Test");
card1 = new JPanel();
card1.add(new JButton("Button 1 - Card 1"));
card1.add(new JButton("Button 2 - Card 1"));
card1.setBackground(new Color(255,0,0));
card2 = new JPanel();
card2.add(new JTextField("TextField on Card 2", 20));
card2.setBackground(new Color(0,255,0));
//Create the panel that contains the "cards".
cards = new JPanel(new CardLayout());
cards.add(card1, card1Text);
cards.add(card2, card2Text);
b = new JButton("Page 1");
b.setBounds(50,50,70,30);
b.setBackground(Color.red);
c = new JButton("Page 2");
c.setBounds(50,80,70,30);
c.setBackground(Color.blue);
pane = f.getContentPane();
pane.add(cards, BorderLayout.CENTER);
b.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e) {
card.show(card1, card1Text);
}
});
c.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e) {
card.show(card2, card2Text);
}
});
f.add(b); f.add(c); f.add(panel); f.add(cards);
}
public static void main(String[] args) {
new CardLayoutTest();
}
}
Try:
card = new CardLayout();
cards = new JPanel(card);
instead of cards = new JPanel(new CardLayout());
UPDATE
public class CardLayoutTest extends JFrame {
JButton b;
JButton c;
JPanel cards;
JPanel card1;
JPanel card2;
CardLayout card;
final String card1Text = "One";
final String card2Text = "Two";
CardLayoutTest() {
super();
setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
passBtn();
}
public void passBtn() {
card1 = new JPanel();
card1.setBackground(new Color(255,0,0));
card2 = new JPanel();
card2.add(new JTextField("TextField on Card 2", 20));
card2.setBackground(new Color(0, 255, 0));
//Create the panel that contains the "cards".
card = new CardLayout();
cards = new JPanel(card);
cards.add(card1, card1Text);
cards.add(card2, card2Text);
b = new JButton("Page 1");
b.setBounds(50, 50, 70, 30);
b.setBackground(Color.red);
card1.add(b);
c = new JButton("Page 2");
c.setBounds(50, 80, 70, 30);
c.setBackground(Color.blue);
card1.add(c);
add(cards);
card.show(cards, card1Text);
b.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
try {
card.show(cards, card1Text);
} catch (Exception e1) {
e1.printStackTrace();
}
}
});
c.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
try {
card.show(cards, card2Text);
} catch (Exception e1) {
e1.printStackTrace();
}
}
});
}
public static void main(String[] args) {
CardLayoutTest cardLayoutTest = new CardLayoutTest();
cardLayoutTest.setVisible(true);
}
}

JPanel How to make button work?

As you read this code, you will realize I got one action event to work, it opens up a new JPanel that displays the button that will run the ballBounce, but for now im stuck trying to get a working button within that frame because that frame is already within a actionEvent, any help?
public class MainJPanelOperation
{
public static void main(String[] a)
{
JPanel panel1 = new JPanel(new GridLayout(5, 10, 1, 1));
JButton t1 = new JButton();
JButton t2 = new JButton();
JButton letsStart = new JButton("Start The Program!");
JButton t3 = new JButton();
JButton t4 = new JButton();
//letsStart.setBounds(200,250,12,12);
panel1.add(t1);
panel1.add(t2);
panel1.add(letsStart);
panel1.add(t3);
panel1.add(t4);
final JFrame frame1 = new JFrame("Game");
frame1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame1.add(panel1);
frame1.setSize(1000,1000);
frame1.setVisible(true);
t1.setVisible(false);
t2.setVisible(false);
t3.setVisible(false);
t4.setVisible(false);
letsStart.setBackground(Color.yellow);
panel1.setBackground(Color.black);
letsStart.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
System.out.println("panel 2 main menu intro online");
JPanel panelMM = new JPanel(new GridLayout(5, 10, 1, 0));
JButton MM1 = new JButton("BallBounce");
panelMM.add(MM1);
JFrame frameMM = new JFrame("Game/Main Menu");
frameMM.add(panelMM);
frameMM.setSize(1000,1000);
frameMM.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frameMM.setVisible(true);
frame1.setVisible(false);
}
});//end of start sequence
}
}
JPanel panelMM = new JPanel(new GridLayout(5, 10, 1, 0));
JButton MM1 = new JButton("BallBounce");
panelMM.add(MM1);
final JFrame frameMM = new JFrame("Game/Main Menu");
frameMM.add(panelMM);
frameMM.setSize(1000,1000);
frameMM.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
letsStart.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
System.out.println("panel 2 main menu intro
frameMM.setVisible(true);
frame1.setVisible(false);
}
});
You can make frameMM final and there is no need to have all of your code inside the ActionListener.
Try This :it is working inside a Action Listener.
JButton MM1 = new JButton("BallBoe");
MM1.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
System.out.println("panel 2");
}
});
or class MainJPanelOperation implements ActionListener
You can use class MainJPanelOperation
{
static JButton MM1;
//your code
}
MM1=new JButton("Button");
MM1.addActionListener(this);
write a Method outside Main()
public void ActionPerformed(ActionEvent e)
{
if(e.getSource()==MM1)
{
System.out.print("");
}
if(e.getSource()==Buttonobject)
{
//your code for button Pressing Event
}
}

How to position JButton in a JFrame window?

My program is about a supermarket. I want to position the JButton 'b1' just below JLabel 'l1' and also below JTextField 'jt1'. I want the JButton 'b1' to also be in the centre but below 'l1' and 'jt1'. Below is the delivery() method of my program:
public static void delivery()
{
final JFrame f1 = new JFrame("Name");
f1.setVisible(true);
f1.setSize(600,200);
f1.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
f1.setLocation(700,450);
JPanel p1 = new JPanel();
final JLabel l1 = new JLabel("Enter your name: ");
final JTextField jt1 = new JTextField(20);
JButton b1 = new JButton("Ok");
b1.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
input1 = jt1.getText();
f1.setVisible(false);
}
});
p1.add(b1);
p1.add(l1);
p1.add(jt1);
f1.add(p1);
final JFrame f2 = new JFrame("Address");
f2.setVisible(true);
f2.setSize(600,200);
f2.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
f2.setLocation(700,450);
JPanel p2 = new JPanel();
final JLabel l2 = new JLabel("Enter your address: ");
final JTextField jt2 = new JTextField(20);
JButton b2 = new JButton("Ok");
b2.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
input2 = jt2.getText();
f2.setVisible(false);
}
});
p2.add(b2);
p2.add(l2);
p2.add(jt2);
f2.add(p2);
}
}
You can use multiple JPanels to get close to what you want:
import java.io.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
class MyGui {
public static void delivery()
{
JFrame f1 = new JFrame("Name");
f1.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
f1.setBounds(200, 100, 500, 300);
Container cpane = f1.getContentPane();
JPanel p1 = new JPanel();
p1.setLayout(new BoxLayout(p1, BoxLayout.LINE_AXIS)); //Horizontal
JLabel l1 = new JLabel("Enter your name: ");
JTextField jt1 = new JTextField(20);
jt1.setMaximumSize( jt1.getPreferredSize() );
p1.add(l1);
p1.add(jt1);
JPanel p2 = new JPanel();
p2.setLayout(new BoxLayout(p2, BoxLayout.PAGE_AXIS)); //Vertical
p2.add(p1);
JButton b1 = new JButton("Ok");
p2.add(b1);
cpane.add(p2);
f1.setVisible(true);
}
}
public class SwingProg {
private static void createAndShowGUI() {
MyGui.delivery();
}
public static void main(String[] args) {
javax.swing.SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGUI();
}
});
}
}
Use the GridBagLayout Class. It's for custom designing using Constraints.

Action Listener does not work on swing

I have a form , That when i click to save button, "Yes" String should display on my console!
(I use "Yes" String for test!)
But does not work when clicked.
My code:
public final class NewUserFrame1 extends JFrame implements ActionListener {
UserInformation userinfo;
JLabel fnamelbl;
JLabel lnamelbl;
JTextField fntf;
JTextField lntf;
JLabel gndlnl;
JRadioButton malerb;
JRadioButton femalerb;
ButtonGroup bgroup;
JLabel registnm;
JButton savebt;
JButton cancelbt;
JLabel showreglbl;
public NewUserFrame1() {
add(rowComponent(), BorderLayout.CENTER);
setLocation(200, 40);
setVisible(true);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
pack();
}
public JPanel rowComponent() {
JPanel panel = new JPanel();
fnamelbl = new JLabel("First name");
lnamelbl = new JLabel("Last Name");
JLabel fntemp = new JLabel();
JLabel lntemp = new JLabel();
fntf = new JTextField(10);
lntf = new JTextField(10);
gndlnl = new JLabel("Gender");
malerb = new JRadioButton("Male");
femalerb = new JRadioButton("Female");
bgroup = new ButtonGroup();
bgroup.add(malerb);
bgroup.add(femalerb);
registnm = new JLabel("Registration ID is:");
showreglbl = new JLabel("");
JLabel regtemp = new JLabel();
savebt = new JButton("Save");
cancelbt = new JButton("Cancell");
JLabel buttontemp = new JLabel();
panel.add(fnamelbl);
panel.add(fntf);
panel.add(fntemp);
panel.add(lnamelbl);
panel.add(lntf);
panel.add(lntemp);
panel.add(gndlnl);
JPanel radiopanel = new JPanel();
radiopanel.setLayout(new FlowLayout(FlowLayout.LEFT));
radiopanel.add(malerb);
radiopanel.add(femalerb);
panel.add(radiopanel);
panel.add(new JLabel());
panel.add(registnm);
panel.add(showreglbl);
panel.add(regtemp);
panel.add(savebt);
panel.add(cancelbt);
panel.add(buttontemp);
panel.setLayout(new SpringLayout());
SpringUtilities.makeCompactGrid(panel, 5, 3, 50, 10, 80, 60);
return panel;
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
#Override
public void run() {
NewUserFrame1 newUserFrame1 = new NewUserFrame1();
}
});
}
#Override
public void actionPerformed(ActionEvent e) {
if (e.getSource() == savebt) {
System.out.print("Yes");
}
}
}
You need to add an ActionListener to your button like so:
savebt.addActionListener(this);
or with an anonymous class, like so:
savebt.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
// your code.
}
});
Using anonymous classes (or inner classes) is better because you can't have more than one actionPerformed() method in a given class.
You need to tell the button to invoke the ActionListener:
savebt = new JButton("Save");
savebt.addActionListener(this);
Note if you intend to use the same method for the save and cancel buttons, you'll need to differentiate, perhaps by comparing the source of the ActionEvent against the two buttons.

CardLayout issue

I have a card layout, first card is a menu.
I Select the second card, and carry out some action. We'll say add a JTextField by clicking a button. If I return to the menu card, and then go back to the second card, that JTextField I added the first time will still be there.
I want the second card to be as I originally constructed it each time I access it, with the buttons, but without the Textfield.
Make sure the panel you're trying to reset has code that takes it back to its "as it was originally constructed" state. Then, when you process the whatever event that causes you to change cards, call that code to restore the original state before showing the card.
Here is the final sorted out version, to remove the card, after doing changes to it, have a look, use the revalidate() and repaint() thingy as usual :-)
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class ApplicationBase extends JFrame
{
private JPanel centerPanel;
private int topPanelCount = 0;
private String[] cardNames = {
"Login Window",
"TextField Creation"
};
private TextFieldCreation tfc;
private LoginWindow lw;
private JButton nextButton;
private JButton removeButton;
private ActionListener actionListener = new ActionListener()
{
public void actionPerformed(ActionEvent ae)
{
if (ae.getSource() == nextButton)
{
CardLayout cardLayout = (CardLayout) centerPanel.getLayout();
cardLayout.next(centerPanel);
}
else if (ae.getSource() == removeButton)
{
centerPanel.remove(tfc);
centerPanel.revalidate();
centerPanel.repaint();
tfc = new TextFieldCreation();
tfc.createAndDisplayGUI();
centerPanel.add(tfc, cardNames[1]);
}
}
};
private void createAndDisplayGUI()
{
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLocationByPlatform(true);
centerPanel = new JPanel();
centerPanel.setLayout(new CardLayout());
lw = new LoginWindow();
lw.createAndDisplayGUI();
centerPanel.add(lw, cardNames[0]);
tfc = new TextFieldCreation();
tfc.createAndDisplayGUI();
centerPanel.add(tfc, cardNames[1]);
JPanel bottomPanel = new JPanel();
removeButton = new JButton("REMOVE");
nextButton = new JButton("NEXT");
removeButton.addActionListener(actionListener);
nextButton.addActionListener(actionListener);
bottomPanel.add(removeButton);
bottomPanel.add(nextButton);
add(centerPanel, BorderLayout.CENTER);
add(bottomPanel, BorderLayout.PAGE_END);
pack();
setVisible(true);
}
public static void main(String... args)
{
SwingUtilities.invokeLater(new Runnable()
{
public void run()
{
new ApplicationBase().createAndDisplayGUI();
}
});
}
}
class TextFieldCreation extends JPanel
{
private JButton createButton;
private int count = 0;
public void createAndDisplayGUI()
{
final JPanel topPanel = new JPanel();
topPanel.setLayout(new GridLayout(0, 2));
createButton = new JButton("CREATE TEXTFIELD");
createButton.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent ae)
{
JTextField tfield = new JTextField();
tfield.setActionCommand("JTextField" + count);
topPanel.add(tfield);
topPanel.revalidate();
topPanel.repaint();
}
});
setLayout(new BorderLayout(5, 5));
add(topPanel, BorderLayout.CENTER);
add(createButton, BorderLayout.PAGE_END);
}
}
class LoginWindow extends JPanel
{
private JPanel topPanel;
private JPanel middlePanel;
private JPanel bottomPanel;
public void createAndDisplayGUI()
{
topPanel = new JPanel();
JLabel userLabel = new JLabel("USERNAME : ", JLabel.CENTER);
JTextField userField = new JTextField(20);
topPanel.add(userLabel);
topPanel.add(userField);
middlePanel = new JPanel();
JLabel passLabel = new JLabel("PASSWORD : ", JLabel.CENTER);
JTextField passField = new JTextField(20);
middlePanel.add(passLabel);
middlePanel.add(passField);
bottomPanel = new JPanel();
JButton loginButton = new JButton("LGOIN");
bottomPanel.add(loginButton);
setLayout(new BoxLayout(this, BoxLayout.Y_AXIS));
add(topPanel);
add(middlePanel);
add(bottomPanel);
}
}
If you just wanted to remove the Latest Edit made to the card, try this code :
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class ApplicationBase extends JFrame
{
private JPanel centerPanel;
private int topPanelCount = 0;
private String[] cardNames = {
"Login Window",
"TextField Creation"
};
private TextFieldCreation tfc;
private LoginWindow lw;
private JButton nextButton;
private JButton removeButton;
private ActionListener actionListener = new ActionListener()
{
public void actionPerformed(ActionEvent ae)
{
if (ae.getSource() == nextButton)
{
CardLayout cardLayout = (CardLayout) centerPanel.getLayout();
cardLayout.next(centerPanel);
}
else if (ae.getSource() == removeButton)
{
TextFieldCreation.topPanel.remove(TextFieldCreation.tfield);
TextFieldCreation.topPanel.revalidate();
TextFieldCreation.topPanel.repaint();
}
}
};
private void createAndDisplayGUI()
{
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLocationByPlatform(true);
centerPanel = new JPanel();
centerPanel.setLayout(new CardLayout());
lw = new LoginWindow();
lw.createAndDisplayGUI();
centerPanel.add(lw, cardNames[0]);
tfc = new TextFieldCreation();
tfc.createAndDisplayGUI();
centerPanel.add(tfc, cardNames[1]);
JPanel bottomPanel = new JPanel();
removeButton = new JButton("REMOVE");
nextButton = new JButton("NEXT");
removeButton.addActionListener(actionListener);
nextButton.addActionListener(actionListener);
bottomPanel.add(removeButton);
bottomPanel.add(nextButton);
add(centerPanel, BorderLayout.CENTER);
add(bottomPanel, BorderLayout.PAGE_END);
pack();
setVisible(true);
}
public static void main(String... args)
{
SwingUtilities.invokeLater(new Runnable()
{
public void run()
{
new ApplicationBase().createAndDisplayGUI();
}
});
}
}
class TextFieldCreation extends JPanel
{
private JButton createButton;
private int count = 0;
public static JTextField tfield;
public static JPanel topPanel;
public void createAndDisplayGUI()
{
topPanel = new JPanel();
topPanel.setLayout(new GridLayout(0, 2));
createButton = new JButton("CREATE TEXTFIELD");
createButton.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent ae)
{
tfield = new JTextField();
tfield.setActionCommand("JTextField" + count);
topPanel.add(tfield);
topPanel.revalidate();
topPanel.repaint();
}
});
setLayout(new BorderLayout(5, 5));
add(topPanel, BorderLayout.CENTER);
add(createButton, BorderLayout.PAGE_END);
}
}
class LoginWindow extends JPanel
{
private JPanel topPanel;
private JPanel middlePanel;
private JPanel bottomPanel;
public void createAndDisplayGUI()
{
topPanel = new JPanel();
JLabel userLabel = new JLabel("USERNAME : ", JLabel.CENTER);
JTextField userField = new JTextField(20);
topPanel.add(userLabel);
topPanel.add(userField);
middlePanel = new JPanel();
JLabel passLabel = new JLabel("PASSWORD : ", JLabel.CENTER);
JTextField passField = new JTextField(20);
middlePanel.add(passLabel);
middlePanel.add(passField);
bottomPanel = new JPanel();
JButton loginButton = new JButton("LGOIN");
bottomPanel.add(loginButton);
setLayout(new BoxLayout(this, BoxLayout.Y_AXIS));
add(topPanel);
add(middlePanel);
add(bottomPanel);
}
}

Categories

Resources