I have to get a String input in one JFrame and display in another.
My second task is to flash the given string in a larger font in the second frame, at an interval of 1sec.
How to proceed?
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
class Input{
String hinput;
private JFrame mainFrame;
private JLabel headerLabel;
private JLabel statusLabel;
private JPanel controlPanel;
private void prepareGUI(){
mainFrame = new JFrame("STRING");
mainFrame.setSize(500,100);
headerLabel = new JLabel("", JLabel.CENTER);
controlPanel = new JPanel();
controlPanel.setLayout(new FlowLayout());
mainFrame.add(headerLabel);
mainFrame.add(controlPanel);
mainFrame.setVisible(true);
}
private void showTextField(){
JLabel stringlabel= new JLabel("String ", JLabel.RIGHT);
final JTextField userText = new JTextField(20);
JButton submitButton = new JButton("Submit");
submitButton.addActionListener(new mylistener());
submitButton.setActionCommand("open");
controlPanel.add(stringlabel);
controlPanel.add(userText);
controlPanel.add(submitButton);
mainFrame.setVisible(true);
}
private class mylistener implements ActionListener{
public void actionPerformed(ActionEvent e){
String cmd = e.getActionCommand();
if(cmd.equals("open")){
mainFrame.dispose();
NewJFrame nj= new NewJFrame(hinput);
}
}
}
public static void main(String args[]){
Input Inp = new Input();
Inp.prepareGUI();
Inp.showTextField();
}
}
class NewJFrame{
JFrame mainFrame;
String text;
JLabel l1;
JTextField tb1;
public NewJFrame(String t){
text=t;
mainFrame=new JFrame("STRING");
mainFrame.setSize(800,800);
mainFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
l1=new JLabel("Entered string");
tb1.setText(text);
mainFrame.add(l1);
mainFrame.add(tb1);
mainFrame.setVisible(true);
}
}
I am getting traceback after i click 'submit' button.
Please point out the errors.
You can get rid of the error by instatiating tb1 in your NewJFrame class like so:
class NewJFrame{
JFrame mainFrame;
String text;
JLabel l1;
JTextField tb1;
public NewJFrame(String t){
text=t;
mainFrame=new JFrame("STRING");
mainFrame.setSize(800,800);
// *** must init tb1!!! ***///
JTextField tb1 = new JTextField();
mainFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
l1=new JLabel("Entered string");
tb1.setText(text);
mainFrame.add(l1);
mainFrame.add(tb1);
mainFrame.setVisible(true);
}
}
As for getting text typed in one JFrame to open in another, I have a slightly modified solution. Maybe have text entered in a JTextField on one JPanel display in another JPanel. To do that, you could use the following code:
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JButton;
import javax.swing.JTextField;
import javax.swing.JLabel;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.FlowLayout;
public class SimpleGUI extends JFrame {
private final JPanel firstPanel;
private final JPanel secondPanel;
private final JButton submitButton;
private final JTextField textField;
private final JLabel secondPanelLabel;
public SimpleGUI() {
// sets the title of the JFrame
super("SimpleGUI");
setLayout(new FlowLayout());
// inits both JPanels
firstPanel = new JPanel();
secondPanel = new JPanel();
// inits empty second JLabel and adds to the secondPanel
secondPanelLabel = new JLabel();
secondPanel.add(secondPanelLabel);
// makes the secondPanel invisible for the time being
secondPanel.setVisible(false);
// inits the submit button
submitButton = new JButton("Submit");
// event-handler for submit button, will set the text in the
// secondPanelLabel to the text in the JTextField the user types
// into. It then makes the firstPanel (with the text field and button),
// invisible, and then makes the second panel visible.
submitButton.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
secondPanelLabel.setText(textField.getText());
firstPanel.setVisible(false);
secondPanel.setVisible(true);
}
});
// inits the textField
textField = new JTextField(10);
// adds the button and the text field to the firstPanel
firstPanel.add(submitButton);
firstPanel.add(textField);
// adds both panels to this JFrame
this.add(firstPanel);
this.add(secondPanel);
}
}
And here is a class with a main method that constructs the SimpleGUI so you can test it out for yourself:
import javax.swing.JFrame;
public class SimpleGUITest {
public static void main(String[] args) {
SimpleGUI frame = new SimpleGUI();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(500, 500);
frame.setVisible(true);
}
}
Related
I am creating a basic GUI frame. The frame has 10 radio buttons and a Submit button. The user selects one option(JRadioButtons) and clicks on the Submit(JButton) button. On clicking the Submit button, the option selected by the user appears on a different frame.
I want the Submit button to recognize the JRadioButton selected by the user.
I have put my bit of code here for reference.
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
public class Frame2 extends JFrame{
private JFrame frame2;
private JLabel label2;
private JButton button2;
private JRadioButton r1;
private JRadioButton r2;
private JRadioButton r3;
private JRadioButton r4;
private JRadioButton r5;
private JRadioButton r6;
private JRadioButton r7;
private JRadioButton r8;
private JRadioButton r9;
private JRadioButton r10;
public ButtonGroup group;
Frame2(){
setLayout(new BorderLayout());
setSize(new Dimension(1304,690));
getContentPane().setBackground(Color.DARK_GRAY);
label2= new JLabel(" Choose a topic: ");
label2.setFont(new Font("Seriff",Font.BOLD, 14));
label2.setForeground(Color.WHITE);
button2=new JButton("Submit");
add(label2, BorderLayout.NORTH);
JPanel centerPanel = new JPanel(new GridLayout(2, 5));
centerPanel.add(r1=new JRadioButton("Introduction"));
centerPanel.add(r2=new JRadioButton("Class and Objects"));
centerPanel.add(r3=new JRadioButton("Object Oriented Programming Concepts"));
centerPanel.add(r4=new JRadioButton("JAVA literals, constants, variables"));
centerPanel.add(r5=new JRadioButton("Loops"));
centerPanel.add(r6=new JRadioButton("Functions/Methods"));
centerPanel.add(r7=new JRadioButton("Strings"));
centerPanel.add(r8=new JRadioButton("Arrays"));
centerPanel.add(r9=new JRadioButton("Time Complexity"));
centerPanel.add(r10=new JRadioButton("Data Structures"));
add(centerPanel, BorderLayout.CENTER);
group= new ButtonGroup();
group.add(r1);
group.add(r2);
group.add(r3);
group.add(r4);
group.add(r5);
group.add(r6);
group.add(r7);
group.add(r8);
group.add(r9);
group.add(r10);
add(button2, BorderLayout.SOUTH);
setVisible(true);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
button2.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
if(e.getSource()==button2) {
Layouts l=new Layouts();
l.main(null);
dispose();
}
}
});
}
public static void main(String[] args) {
Frame2 fr2=new Frame2();
}
}`
Thanks in advance.
It's a lot easier if you put the JRadioButtons in an array.
Here are the changes I made to your code to make it easier to modify and understand.
I added a call to the SwingUtilities invokeLater method to ensure the creation and execution of the Swing components happens on the Event Dispatch Thread.
I created the individual JPanels in methods. By separating the JPanel code, I could more easily focus on one part of the GUI at a time.
The methods to construct a JFrame must be called in the proper order. You have to create all the Swing components before you make the JFrame visible.
Here's one way to connect a JButton with a group of JRadioButtons.
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Font;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.ButtonGroup;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.SwingUtilities;
public class RadioButtonTest {
private JButton button2;
private JRadioButton[] rb;
private ButtonGroup group;
public RadioButtonTest() {
JFrame frame = new JFrame("Java Tutorials");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setBackground(Color.DARK_GRAY);
frame.add(createMainPanel());
frame.pack();
frame.setLocationByPlatform(true);
frame.setVisible(true);
}
private JPanel createMainPanel() {
JPanel panel = new JPanel(new BorderLayout());
JLabel label2 = new JLabel(" Choose a topic: ");
label2.setFont(new Font("Seriff", Font.BOLD, 14));
label2.setForeground(Color.WHITE);
panel.add(label2, BorderLayout.NORTH);
panel.add(createButtonPanel(), BorderLayout.CENTER);
button2 = new JButton("Submit");
button2.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
if (e.getSource() == button2) {
for (int i = 0; i < rb.length; i++) {
if (rb[i].isSelected()) {
String text = rb[i].getText();
System.out.println(text);
// Do your second JFrame
}
}
}
}
});
panel.add(button2, BorderLayout.SOUTH);
return panel;
}
private JPanel createButtonPanel() {
JPanel centerPanel = new JPanel(new GridLayout(0, 2));
String[] titles = { "Introduction", "Class and Objects",
"Object Oriented Programming Concepts",
"JAVA literals, constants, variables", "Loops",
"Functions/Methods", "Strings", "Arrays",
"Time Complexity", "Data Structures" };
rb = new JRadioButton[titles.length];
group = new ButtonGroup();
for (int i = 0; i < titles.length; i++) {
rb[i] = new JRadioButton(titles[i]);
group.add(rb[i]);
centerPanel.add(rb[i]);
}
return centerPanel;
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
#Override
public void run() {
new RadioButtonTest();
}
});
}
}
I have been experimenting with making GUIs in java as opposed to just using "static" all the time and come across the "SwingUtilities.invokeLater()" method. I manage to get everything setup but when it comes to run the application, nothing appears on the JPanel until I resize the window. Is there a fix for this or am I doing it wrong?
Heres my code:
public class main extends JPanel implements ActionListener{
public JLabel userLabel;
public JLabel passLabel;
public JTextField userField;
public JTextField passField;
public JButton login;
public JButton closeLogin;
public JButton help;
public main(){
userLabel = new JLabel("Username: ");
passLabel = new JLabel("Password: ");
userField = new JTextField(16);
passField = new JTextField(16);
login = new JButton("Login");
login.setActionCommand("login");
login.setMnemonic(KeyEvent.VK_L);
closeLogin = new JButton("Close");
closeLogin.setActionCommand("closeLogin");
closeLogin.setMnemonic(KeyEvent.VK_E);
help = new JButton("Help");
help.setActionCommand("helpLogin");
help.setMnemonic(KeyEvent.VK_H);
login.addActionListener(this);
closeLogin.addActionListener(this);
help.addActionListener(this);
add(userLabel);
add(userField);
add(passLabel);
add(passField);
add(login);
add(help);
add(closeLogin);
}
public void actionPerformed(ActionEvent e){
}
public static void initComponents(){
JFrame loginFrame = new JFrame("Encrypted Chat - Login");
loginFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
main loginPanel = new main();
loginPanel.setLayout(new FlowLayout());
loginFrame.setSize(300, 125);
loginFrame.setResizable(false);
loginFrame.setVisible(true);
}
public static void main(String args[]){
SwingUtilities.invokeLater(new Runnable(){
public void run(){
initComponents();
}
});
}
}
EDIT: I know the password JTextField is meant to be a JPasswordField.. so ignore it :P
Two basic advices:
1.) When you use swing, and stuff doesnt show up/update, you should call JPanel.revalidate() and JPanel.repaint() These two functions will update your panel.
If you are using a JFrame and you didn't add any extra panels to it, then you can get the content panel by JFrame.getContentPane()
2.) When you finished adding Components to a panel/frame you should also call pack() on the frame, this will ensure, that all your Components have the prefered size.
You never add your content to the JFrame. The minimal set of changes you need:
public static void main(String args[]){
final main main = new main();
SwingUtilities.invokeLater(new Runnable(){
public void run(){
initComponents(main);
}
});
}
And then modify initComponents to take a main object:
public static void initComponents(main main){
JFrame loginFrame = new JFrame("Encrypted Chat - Login");
loginFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
main loginPanel = new main();
loginPanel.setLayout(new FlowLayout());
loginFrame.setSize(300, 125);
loginFrame.setResizable(false);
loginFrame.setVisible(true);
loginFrame.add(main); // <----- this line is added
}
for built_in FlowLayout (for JPanel) I don't suggest to use pack() for JFrame, sure correct way could be to use proper and better LayoutManager for this job, GridBagLayout or SpringLayout
output by using JFrame#setSize() and without pack()
for example
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.SwingUtilities;
public class MainLogin implements ActionListener {
private JFrame loginFrame = new JFrame("Encrypted Chat - Login");
private JPanel pnl = new JPanel();
private JLabel userLabel;
private JLabel passLabel;
private JTextField userField;
private JTextField passField;
private JButton login;
private JButton closeLogin;
private JButton help;
public MainLogin() {
userLabel = new JLabel("Username: ");
passLabel = new JLabel("Password: ");
userField = new JTextField(16);
passField = new JTextField(16);
login = new JButton("Login");
login.setActionCommand("login");
login.setMnemonic(KeyEvent.VK_L);
closeLogin = new JButton("Close");
closeLogin.setActionCommand("closeLogin");
closeLogin.setMnemonic(KeyEvent.VK_E);
help = new JButton("Help");
help.setActionCommand("helpLogin");
help.setMnemonic(KeyEvent.VK_H);
login.addActionListener(this);
closeLogin.addActionListener(this);
help.addActionListener(this);
pnl.add(userLabel);
pnl.add(userField);
pnl.add(passLabel);
pnl.add(passField);
pnl.add(login);
pnl.add(help);
pnl.add(closeLogin);
loginFrame.add(pnl);
loginFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
loginFrame.setSize(300, 125);
loginFrame.setResizable(false);
loginFrame.setVisible(true);
}
public void actionPerformed(ActionEvent e) {
}
public static void main(String args[]) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
MainLogin mainLogin = new MainLogin();
}
});
}
}
Put
JFrame.setVisible(true); on the last line after adding all components to it.
I'm writing a simple program using CardLayout. The main screen should display a button which when pressed would go to the next screen which contains another button for another screen. My problem is that when I run my program the screen is black. I tried following tutorials online to write my own program but I don't seem to find the problem with my code. I don't get any errors when run. Here is my code
//using CardLayout to change screen when action is performed
import javax.swing.JFrame;
import javax.swing.JTextField;
import javax.swing.JLabel;
import javax.swing.JButton;
import javax.swing.JPanel;
import javax.swing.Popup;
import javax.swing.JOptionPane;
import java.awt.BorderLayout;
import java.awt.CardLayout;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.FlowLayout;
public class CL extends JFrame {
JPanel cardPanel;
JPanel cardPanelA;
JPanel cardPanelB;//to set different screens
CardLayout cl;
private JButton button1;
private JButton button2;
private JButton change;
private JLabel label;
private JTextField textField1;
private JTextField textField2;
JButton button;
public CL() {
super("This is a sample");
cardPanel = new JPanel();
cardPanelA = new JPanel();
cardPanelB = new JPanel();
cl = new CardLayout();
cardPanel.setLayout(cl);
button1 = new JButton("button1");
button2 = new JButton("button2");
change = new JButton("change screen");
label = new JLabel("this is a label");
textField1 = new JTextField(10);
textField2 = new JTextField("enter text", 6);
cardPanelA.add(change);
cardPanelA.add(label);
cardPanelA.add(textField1);
cardPanelA.add(textField2);
cardPanelB.add(button1);
cardPanelB.add(button2);
cardPanel.add(cardPanelA);
cardPanel.add(cardPanelB);
JPanel panel1 = new JPanel();
button = new JButton("initial button");
panel1.add(button);
theHandler handler = new theHandler();//make action listener
change.addActionListener(handler);
button1.addActionListener(handler);
button2.addActionListener(handler);
button.addActionListener(handler);
/*
getContentPane().add(panel1, BorderLayout.NORTH);
getContentPane().add(cardPanelA, BorderLayout.NORTH);
getContentPane().add(cardPanelB, BorderLayout.NORTH);
*/
}
private class theHandler implements ActionListener {
public void actionPerformed(ActionEvent event) {
if (event.getSource() == button) {
cl.show(cardPanel, "Panel A");
}
if (event.getSource() == change) {
cl.show(cardPanelB, "panelB");
}
if (event.getSource() == button2) {
cl.show(cardPanel, "PanelA");
}
if (event.getSource() == button1) {
JOptionPane.showMessageDialog(null, "this is the second screen");
}
}
}
}
/*way to use CardLayout: create a CardLayout manager and create a bunch of different JPanels which
* would each be a different screen. Make a panel that stores the CardLayout as the layout.
* Add the different elements to each Panel(buttons, textfields) and then add the panels to the JPanel that stores
* the CardLayout
*/
import javax.swing.JFrame;
public class CardTest {
public static void main(String[] args) {
CL object = new CL();
object.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
object.setSize(400, 400);
object.setVisible(true);
}
}
It might be something simple but I'm not sure of what it is. Some advice would be appreciated.
Make sure you add your panels to the frame
add(cardPanel);
Without that no components will be shown
I have been experimenting with making GUIs in java as opposed to just using "static" all the time and come across the "SwingUtilities.invokeLater()" method. I manage to get everything setup but when it comes to run the application, nothing appears on the JPanel until I resize the window. Is there a fix for this or am I doing it wrong?
Heres my code:
public class main extends JPanel implements ActionListener{
public JLabel userLabel;
public JLabel passLabel;
public JTextField userField;
public JTextField passField;
public JButton login;
public JButton closeLogin;
public JButton help;
public main(){
userLabel = new JLabel("Username: ");
passLabel = new JLabel("Password: ");
userField = new JTextField(16);
passField = new JTextField(16);
login = new JButton("Login");
login.setActionCommand("login");
login.setMnemonic(KeyEvent.VK_L);
closeLogin = new JButton("Close");
closeLogin.setActionCommand("closeLogin");
closeLogin.setMnemonic(KeyEvent.VK_E);
help = new JButton("Help");
help.setActionCommand("helpLogin");
help.setMnemonic(KeyEvent.VK_H);
login.addActionListener(this);
closeLogin.addActionListener(this);
help.addActionListener(this);
add(userLabel);
add(userField);
add(passLabel);
add(passField);
add(login);
add(help);
add(closeLogin);
}
public void actionPerformed(ActionEvent e){
}
public static void initComponents(){
JFrame loginFrame = new JFrame("Encrypted Chat - Login");
loginFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
main loginPanel = new main();
loginPanel.setLayout(new FlowLayout());
loginFrame.setSize(300, 125);
loginFrame.setResizable(false);
loginFrame.setVisible(true);
}
public static void main(String args[]){
SwingUtilities.invokeLater(new Runnable(){
public void run(){
initComponents();
}
});
}
}
EDIT: I know the password JTextField is meant to be a JPasswordField.. so ignore it :P
Two basic advices:
1.) When you use swing, and stuff doesnt show up/update, you should call JPanel.revalidate() and JPanel.repaint() These two functions will update your panel.
If you are using a JFrame and you didn't add any extra panels to it, then you can get the content panel by JFrame.getContentPane()
2.) When you finished adding Components to a panel/frame you should also call pack() on the frame, this will ensure, that all your Components have the prefered size.
You never add your content to the JFrame. The minimal set of changes you need:
public static void main(String args[]){
final main main = new main();
SwingUtilities.invokeLater(new Runnable(){
public void run(){
initComponents(main);
}
});
}
And then modify initComponents to take a main object:
public static void initComponents(main main){
JFrame loginFrame = new JFrame("Encrypted Chat - Login");
loginFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
main loginPanel = new main();
loginPanel.setLayout(new FlowLayout());
loginFrame.setSize(300, 125);
loginFrame.setResizable(false);
loginFrame.setVisible(true);
loginFrame.add(main); // <----- this line is added
}
for built_in FlowLayout (for JPanel) I don't suggest to use pack() for JFrame, sure correct way could be to use proper and better LayoutManager for this job, GridBagLayout or SpringLayout
output by using JFrame#setSize() and without pack()
for example
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.SwingUtilities;
public class MainLogin implements ActionListener {
private JFrame loginFrame = new JFrame("Encrypted Chat - Login");
private JPanel pnl = new JPanel();
private JLabel userLabel;
private JLabel passLabel;
private JTextField userField;
private JTextField passField;
private JButton login;
private JButton closeLogin;
private JButton help;
public MainLogin() {
userLabel = new JLabel("Username: ");
passLabel = new JLabel("Password: ");
userField = new JTextField(16);
passField = new JTextField(16);
login = new JButton("Login");
login.setActionCommand("login");
login.setMnemonic(KeyEvent.VK_L);
closeLogin = new JButton("Close");
closeLogin.setActionCommand("closeLogin");
closeLogin.setMnemonic(KeyEvent.VK_E);
help = new JButton("Help");
help.setActionCommand("helpLogin");
help.setMnemonic(KeyEvent.VK_H);
login.addActionListener(this);
closeLogin.addActionListener(this);
help.addActionListener(this);
pnl.add(userLabel);
pnl.add(userField);
pnl.add(passLabel);
pnl.add(passField);
pnl.add(login);
pnl.add(help);
pnl.add(closeLogin);
loginFrame.add(pnl);
loginFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
loginFrame.setSize(300, 125);
loginFrame.setResizable(false);
loginFrame.setVisible(true);
}
public void actionPerformed(ActionEvent e) {
}
public static void main(String args[]) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
MainLogin mainLogin = new MainLogin();
}
});
}
}
Put
JFrame.setVisible(true); on the last line after adding all components to it.
Haven't been able to find a definite answer. I know that one button can have multiple listeners, but what about the inverse? Currently having an issue where the second button I have setup on a ButtonListener isn't responding, and I'm wondering if that's why. If what I was trying to do isn't possible, how do you setup another ButtonListener?
As always, any assistance is appreciated.
Relevant Source:
import javax.swing.*;
import java.util.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.event.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.image.BufferedImage;
import java.io.*;
import javax.imageio.*;
import java.text.NumberFormat;
public class ClientApp extends JFrame
{
public static void main(String[] args)
{
new ClientApp();
}
//Declarations so they have scope outside of ClientApp()
private JButton switchCard;
private JPanel infoPanel;
private JPanel mainPanel;
private JPanel cartPanel;
private JPanel orderingPanel;
private JList candyList;
private CardLayout cl = new CardLayout();
private CardLayout cl2 = new CardLayout();
private JPanel checkoutPanel;
private JButton checkoutButton;
private JTextField acidPopsTF;
private JTextField bertieBottsTF;
private JTextField bloodPopsTF;
private JTextField cauldronCakesTF;
private JTextField charmChocTF;
private JTextField chocoballsTF;
private JTextField chocCauldronsTF;
private JTextField chocFrogsTF;
private JTextField chocWandsTF;
private JTextField roachClustersTF;
private JTextField crystalPineappleTF;
private JTextField droobleGumTF;
private JTextField explodeBonbonsTF;
private JTextField fizzWhizTF;
private JTextField iceMiceTF;
private JTextField jellySlugsTF;
private JTextField liquorWandsTF;
private JTextField pepImpsTF;
private JTextField pinkCocoIceTF;
private JTextField spindleSpidersTF;
private JTextField sugarQuillsTF;
private JTextField wizochocTF;
private JTextField shockChocTF;
private ArrayList cart;
private Object[] cartArray;
public ClientApp()
{
this.setSize(750,400);
this.setTitle("Honeydukes Muggle Ordering System");
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
ButtonListener bl = new ButtonListener();
//Creating the panels
mainPanel = new JPanel(cl2);
cartPanel = new JPanel();
orderingPanel = new JPanel(new BorderLayout());
infoPanel = new JPanel(cl);
JPanel invntryPanel = new JPanel(new BorderLayout());
checkoutPanel = new JPanel(new BorderLayout());
//-----Code Code Code----//
//Creating the interface element for advancing to the checkout screen
checkoutButton = new JButton("Checkout");
checkoutButton.addActionListener(bl);
checkoutPanel.add(checkoutButton, BorderLayout.LINE_END);
//Adding everything to the frame
orderingPanel.add(checkoutPanel, BorderLayout.PAGE_END);
orderingPanel.add(invntryPanel, BorderLayout.LINE_START);
orderingPanel.add(infoPanel, BorderLayout.CENTER);
mainPanel.add(orderingPanel, "Ordering");
mainPanel.add(cartPanel, "Cart");
if (currentPage == 1)
{
cl2.show(mainPanel, "Cart");
}
if (currentPage == 0)
{
cl2.show(mainPanel, "Ordering");
}
this.add(mainPanel);
this.setVisible(true);
Definitely you may use use single listener for one or more Buttons. For more inforation read tutorial pages : Writing Event Listeners
If you want to set One Listener per One Button..then try Anonymous class
Eg:
button_1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
JOptionPane.showMessageDialog(null, "You clicked the 1st button!");
}
});
button_2.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
JOptionPane.showMessageDialog(null, "You clicked the 2nd button!");
}
});