Trying to add ActionListener to JButtons - java

I cannot figure out how to add Actionlisteners to the JButtons, any help would be much appreciated.
public class Translator extends JPanel implements MouseListener, ActionListener {
private JButton french = new JButton();
private JButton german = new JButton();
private JButton irish = new JButton();
public Translator(){
french = new JButton("French");
german = new JButton("German");
irish = new JButton("Irish");
setLayout(new GridLayout(2,1));
buttonPanel.setLayout(new GridLayout(1,3));
buttonPanel.add(french);
buttonPanel.add(german);
buttonPanel.add(irish);
}
#Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
}
#Override
public void mouseClicked(MouseEvent e) {
// TODO Auto-generated method stub
}
#Override
public void mouseEntered(MouseEvent e) {
// TODO Auto-generated method stub
}
}

There are plenty of ways to add an ActionListener, to a given JComponent (that supports it's use). I have added some comments in the code snippets, to help explain them a bit better, and some links in the comments for future reference.
1.) If the class implements the ActionListener interface, i.e. the class itself contains the actionPerformed(...) method, then one can do it, in this manner:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class Skeleton implements ActionListener {
private JFrame frame;
private JPanel contentPane;
private JButton button;
private void displayGUI() {
frame = new JFrame("Skeleton");
/*
* EXIT_ON_CLOSE is same as putting System.exit(0),
* which in some sense, doesnot allows one's
* application to terminate graciously.
*/
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
contentPane = new JPanel();
button = new JButton("This is a button.");
/*
* This is one way of attaching an ActionListener
* to the JButton, but the main disadvantage of
* this approach is, it breaks encapsulation,
* as you can see the public method, actionPerformed(),
* is lying free to be accessed by any code outside
* the scope of the class
*/
button.addActionListener(this);
contentPane.add(button);
frame.setContentPane(contentPane);
frame.pack();
frame.setLocationByPlatform(true);
frame.setVisible(true);
}
public static void main(String[] args) {
Runnable runnable = new Runnable() {
#Override
public void run() {
new Skeleton().displayGUI();
}
};
EventQueue.invokeLater(runnable);
}
#Override
public void actionPerformed(ActionEvent e) {
JOptionPane.showMessageDialog(frame, "BINGO!",
"Information: ", JOptionPane.INFORMATION_MESSAGE);
}
}
2.) If one doesn't wants to create unnecessary class files. Then this approach, which uses, EventHandler can be used:
import java.awt.*;
import java.awt.event.*;
import java.beans.EventHandler;
import javax.swing.*;
public class Example1 {
private JFrame frame;
private JPanel contentPane;
private JButton button;
private void displayGUI() {
frame = new JFrame("Skeleton");
/*
* EXIT_ON_CLOSE is same as putting System.exit(0),
* which in some sense, doesnot allows one's
* application to terminate graciously.
*/
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
contentPane = new JPanel();
button = new JButton("This is a button.");
/*
* This is another way of attaching
* an ActionListener to the JButton,
* the main advantage of this approach
* is, that one does not have to create
* a new class to handle events
* More info regarding the use of this
* approach, can be found on this link :
* http://docs.oracle.com/javase/tutorial/uiswing/events/generalrules.html
*/
button.addActionListener((ActionListener)
EventHandler.create(ActionListener.class
, Example1.this, "buttonAction", ""));
contentPane.add(button);
frame.setContentPane(contentPane);
frame.pack();
frame.setLocationByPlatform(true);
frame.setVisible(true);
}
public static void main(String[] args) {
Runnable runnable = new Runnable() {
#Override
public void run() {
new Example1().displayGUI();
}
};
EventQueue.invokeLater(runnable);
}
public void buttonAction(ActionEvent e) {
JOptionPane.showMessageDialog(frame, "BINGO!",
"Information: ", JOptionPane.INFORMATION_MESSAGE);
}
}
3.) If one is more concern about the concept of Encapsulation, then this approach is beneficial:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class Example2 {
private JFrame frame;
private JPanel contentPane;
private JButton button;
private ActionListener buttonActions =
new ActionListener() {
#Override
public void actionPerformed(ActionEvent ae) {
JOptionPane.showMessageDialog(frame, "BINGO!",
"Information: ", JOptionPane.INFORMATION_MESSAGE);
}
};
private void displayGUI() {
frame = new JFrame("Skeleton");
/*
* EXIT_ON_CLOSE is same as putting System.exit(0),
* which in some sense, doesnot allows one's
* application to terminate graciously.
*/
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
contentPane = new JPanel();
button = new JButton("This is a button.");
/*
* This is another way of attaching
* an ActionListener to the JButton,
* the main advantage of this approach
* is, it adheres to encapsulation.
*/
button.addActionListener(buttonActions);
contentPane.add(button);
frame.setContentPane(contentPane);
frame.pack();
frame.setLocationByPlatform(true);
frame.setVisible(true);
}
public static void main(String[] args) {
Runnable runnable = new Runnable() {
#Override
public void run() {
new Example2().displayGUI();
}
};
EventQueue.invokeLater(runnable);
}
}
4.) If one is more inclined towards creation of Anonymous Classes, then this approach can be used:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class Example3 {
private JFrame frame;
private JPanel contentPane;
private JButton button;
private void displayGUI() {
frame = new JFrame("Skeleton");
/*
* EXIT_ON_CLOSE is same as putting System.exit(0),
* which in some sense, doesnot allows one's
* application to terminate graciously.
*/
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
contentPane = new JPanel();
button = new JButton("This is a button.");
/*
* This is the fourth way of attaching
* an ActionListener to the JButton,
* the main advantage of this approach
* is, it adheres to encapsulation, the
* public method remains hidden
* inside the Anonymous Class
* More info can be found on this link :
* http://docs.oracle.com/javase/tutorial/java/javaOO/anonymousclasses.html
* The main disadvantage of this approach is
* that it doesnot gives you the privilege
* of separation of concerns, which can
* be done using the fifth approach,
* which is MVC - Pattern (Model-View-Controller)
* and moreover, it creates a hell lot of classes, in
* your project, which can lead to extra overhead
*/
button.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent ae) {
JOptionPane.showMessageDialog(frame, "BINGO!",
"Information: ", JOptionPane.INFORMATION_MESSAGE);
}
});
contentPane.add(button);
frame.setContentPane(contentPane);
frame.pack();
frame.setLocationByPlatform(true);
frame.setVisible(true);
}
public static void main(String[] args) {
Runnable runnable = new Runnable() {
#Override
public void run() {
new Example3().displayGUI();
}
};
EventQueue.invokeLater(runnable);
}
}
EDIT:
5.) This approach, includes using Action instead of ActionListener. This is to be used for sharing same functionality among various JComponents, which leads to code reusability
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class Example4 {
private JFrame frame;
private JPanel contentPane;
private JMenuItem showMenuItem;
private JButton button;
private Action myActions;
/*
* This approach is basically used, when
* one wants to share the same functionality
* of different JComponents among each other,
* without writing redundant codes for each
* one of those components. Here JMenuItem
* and JButton are both using the same
* functionality, to perform the same task.
* More info can be found on this link:
* http://docs.oracle.com/javase/tutorial/uiswing/misc/action.html
*/
private class MyActions extends AbstractAction {
public MyActions(String title, String desc) {
super(title);
putValue(SHORT_DESCRIPTION, desc);
}
#Override
public void actionPerformed(ActionEvent ae) {
JOptionPane.showMessageDialog(frame, "BINGO!",
"Information: ", JOptionPane.INFORMATION_MESSAGE);
}
}
private void displayGUI() {
frame = new JFrame("Skeleton");
/*
* EXIT_ON_CLOSE is same as putting System.exit(0),
* which in some sense, doesnot allows one's
* application to terminate graciously.
*/
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
contentPane = new JPanel();
button = new JButton("This is a button.");
myActions = new MyActions("Show", "A small description");
button.setAction(myActions);
contentPane.add(button);
frame.setJMenuBar(getJMenuBar());
frame.setContentPane(contentPane);
frame.pack();
frame.setLocationByPlatform(true);
frame.setVisible(true);
}
private JMenuBar getJMenuBar() {
JMenuBar menuBar = new JMenuBar();
JMenu fileMenu = new JMenu("File");
showMenuItem = new JMenuItem(myActions);
fileMenu.add(showMenuItem);
menuBar.add(fileMenu);
return menuBar;
}
public static void main(String[] args) {
Runnable runnable = new Runnable() {
#Override
public void run() {
new Example4().displayGUI();
}
};
EventQueue.invokeLater(runnable);
}
}

french.addActionListener(an_instance_of_the_class_where_actionPerformed_is);
which, as I can see after the edit, would be this
Also, see this Example and some tutorial text in this corner of the web
I think that the reference to the tutorial and the many examples is highly relevant.

button.addActionListener(<your_ActionListener_here>);
In your case, it'll be:
french.addActionListener(this);
If you want to use the same ActionListener for all the three buttons, you can use the getSource() function of ActionEvent e to detect which button was actually pressed.

If you are using Java8, you can try this.
JButton french = new JButton("French");
french.addActionListener(new ActionListener(){
#Override
public void actionPerformed(ActionEvent ae){
System.out.println("Button french clicked!");
}
});
french.addActionListener(button -> System.out.println("Button Click listener..."));
JFrame frame = new JFrame("Button Listener Test");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(french, BorderLayout.CENTER);
frame.setSize(250, 250);
frame.setVisible(true);

Obviously the answer is place this into your addActionListener method.
The addActionListener method takes as an argument an object that implements ActionListener interface, this interface force you to implements/place-in-your-code theactionPerformed method which is the one that is called, when an action is triggered to the component that is assigned.
So, placing this in your method, it will search inside the object you passed, in our case, the Translator object for an actionPerformed method and call it.
this.french.addActionListener(this);
Of course there is a lot of code missing in order to work.
I really liked #Sandeep answer take use of lambda expression.
You can see a full example below.
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class Translator extends JPanel implements ActionListener {
private JButton french = new JButton();
private JButton german = new JButton();
private JButton irish = new JButton();
#SuppressWarnings("empty-statement")
public Translator(){
french = new JButton("French");
german = new JButton("German");
irish = new JButton("Irish");
// setLayout(new GridLayout(2,1));
this.setLayout(new GridLayout(1,3));
this.add(french);
this.add(german);
this.add(irish);
ActionListener ac = (ActionEvent ae) -> { System.out.println(((JButton) ae.getSource()).getText()); };
this.french.addActionListener(ac);
this.german.addActionListener(ac);
this.irish.addActionListener(ac);
this.irish.addActionListener(Translator.this);
}
#Override
public void actionPerformed(ActionEvent e) {
System.out.println(((JButton) e.getSource()).getText());
}
public static void main(String[] args) {
JFrame jframe = new JFrame("StackOverflow");
jframe.add(new Translator());
jframe.pack();
jframe.setLocationRelativeTo(null);
jframe.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
jframe.setVisible(true);
}
}

JButton button = new JButton("Button");
button.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
//To-Do
//Button clicked
});
Hope this helps, it's relatively simple! You just need to add an ActionListener to your desired JButton
To give you a broader idea of how to implement it in a case-scenario where I would like to run a new GUI frame after pressing a button:
startNewFrame.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
System.out.println("Starting new frame");
SwingUtilities.invokeLater(new Runnable() {
#Override
public void run() {
NewFrame newFrame = new NewFrame();
newFrame.setVisible(true);
dispose();//Disposes of current frame
}
});
}
});

Related

Dispose JDialog from another class

I want to dispose a JDialog from another class, because i am trying to keep classes and methods clean, and not create buttons and handle the listeners in the same class. So here is the problem.
I tried creating a get method from the first class to get the dialog and then dispose it on the third but didnt work.
public class AddServiceListener extends JFrame implements ActionListener {
/**
* Creates listener for the File/New/Service button.
*/
public AddServiceListener() {
}
/**
* Performs action.
*/
public void actionPerformed(ActionEvent e) {
AddServiceWindow dialog = new AddServiceWindow();
dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
dialog.setVisible(true);
}
}
public class AddServiceWindow extends JDialog {
private JPanel contentPanel;
private JFrame frame;
private JPanel buttonPanel;
private JLabel nameLabel;
private JTextField nameField;
private JLabel destinationLabel;
private JTextField destinationField;
private JButton destinationButton;
private JButton okButton;
private JButton cancelButton;
/**
* Creates the dialog window.
*/
public AddServiceWindow() {
ManageMinder mainFrame = new ManageMinder();
frame = mainFrame.getFrame();
contentPanel = new JPanel();
contentPanel.setLayout(null);
setTitle("New Service File");
setSize(340, 220);
setLocationRelativeTo(frame);
getContentPane().setLayout(new BorderLayout());
contentPanel.setBorder(new EmptyBorder(5, 5, 5, 5));
getContentPane().add(contentPanel, BorderLayout.CENTER);
buttonPanel = new JPanel();
buttonPanel.setLayout(new FlowLayout(FlowLayout.RIGHT));
getContentPane().add(buttonPanel, BorderLayout.SOUTH);
createLabels();
createTextFields();
createButtons();
addListeners();
}
/**
* Creates the labels.
*/
private void createLabels() {
nameLabel = new JLabel("Name:");
nameLabel.setHorizontalAlignment(SwingConstants.RIGHT);
nameLabel.setBounds(25, 25, 52, 16);
contentPanel.add(nameLabel);
destinationLabel = new JLabel("Path:");
destinationLabel.setHorizontalAlignment(SwingConstants.RIGHT);
destinationLabel.setBounds(7, 70, 70, 16);
contentPanel.add(destinationLabel);
}
/**
* Creates the text fields.
*/
private void createTextFields() {
nameField = new JTextField();
nameField.setBounds(87, 22, 220, 22);
contentPanel.add(nameField);
nameField.setColumns(10);
destinationField = new JTextField();
destinationField.setBounds(87, 68, 220, 20);
contentPanel.add(destinationField);
destinationField.setColumns(10);
}
/**
* Creates the buttons of the window.
*/
private void createButtons() {
destinationButton = new JButton("Select...");
destinationButton.setBounds(87, 99, 82, 23);
destinationButton.setFocusPainted(false);
contentPanel.add(destinationButton);
okButton = new JButton("OK");
okButton.setFocusPainted(false);
buttonPanel.add(okButton);
cancelButton = new JButton("Cancel");
cancelButton.setFocusPainted(false);
buttonPanel.add(cancelButton);
}
/**
* Adds listeners to buttons.
*/
private void addListeners() {
ActionListener destinationAction = new AddDestinationListener(destinationField);
destinationButton.addActionListener(destinationAction);
ActionListener okAction = new SaveNewServiceFileListener(nameField, destinationField);
okButton.addActionListener(okAction);
}
}
public class SaveNewServiceFileListener extends JFrame implements ActionListener {
private JTextField nameField;
private JTextField destinationField;
private String path;
private File newService;
/**
* Creates listener for the File/New/Add Service/OK button.
*/
public SaveNewServiceFileListener(JTextField nameField, JTextField destinationField) {
this.nameField = nameField;
this.destinationField = destinationField;
}
/**
* Performs action.
*/
public void actionPerformed(ActionEvent e) {
path = destinationField.getText() + "\\" + nameField.getText() + ".csv";
try {
newService = new File(path);
if(newService.createNewFile()) {
System.out.println("Done!");
// DISPOSE HERE
}
else {
System.out.println("Exists!");
}
} catch (IOException io) {
throw new RuntimeException(io);
}
}
}
What should i do, so the dialog disposes on the third one, when OK is clicked and file is created?
The way to change the state of another object is to 1) have a reference to that object, and 2) call a public method on it.
Here the other object is the JDialog, and the state that you wish to change is its visibility by calling either .close() or .dispose() on it. The problem that you're having is that the reference to the JDialog is not readily available since it is buried within the actionPerformed(...) method of your AddServiceListener class.
So don't do this -- don't bury the reference but rather put it into a field of the class that needs it.
If you absolutely need to have stand-alone ActionListener classes, then I think that the simplest thing to do is to take the dialog out of the listener class and into the view class, your main GUI, and then have the listener call methods on the view. For example
Assume that the dialog class is called SomeDialog and the main GUI is called MainGui. Then put the reference to the dialog in the MainGui class:
public class MainGui extends JFrame {
private SomeDialog someDialog = new SomeDialog(this);
And rather than have your listeners create the dialog, have them call a method in the main class that does this:
public class ShowDialogListener implements ActionListener {
private MainGui mainGui;
public ShowDialogListener(MainGui mainGui) {
// pass the main GUI reference into the listener
this.mainGui = mainGui;
}
#Override
public void actionPerformed(ActionEvent e) {
// tell the main GUI to display the dialog
mainGui.displaySomeDialog();
}
}
Then MainGUI can have:
public void displaySomeDialog() {
someDialog.setVisible(true);
}
An example MRE program could look like so:
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Window;
import java.awt.event.*;
import javax.swing.*;
public class FooGui002 {
public static void main(String[] args) {
SwingUtilities.invokeLater(() -> {
MainGui mainGui = new MainGui();
mainGui.setVisible(true);
});
}
}
#SuppressWarnings("serial")
class MainGui extends JFrame {
private SomeDialog someDialog;
private JButton showSomeDialogButton = new JButton("Show Some Dialog");
private JButton closeSomeDialogButton = new JButton("Close Some Dialog");
public MainGui() {
super("Main GUI");
setDefaultCloseOperation(EXIT_ON_CLOSE);
setPreferredSize(new Dimension(500, 200));
someDialog = new SomeDialog(this);
showSomeDialogButton.addActionListener(new ShowDialogListener(this));
showSomeDialogButton.setMnemonic(KeyEvent.VK_S);
closeSomeDialogButton.addActionListener(new CloseSomeDialogListener(this));
closeSomeDialogButton.setMnemonic(KeyEvent.VK_C);
setLayout(new FlowLayout());
add(showSomeDialogButton);
add(closeSomeDialogButton);
pack();
setLocationByPlatform(true);
}
public void displaySomeDialog() {
someDialog.setVisible(true);
}
public void closeSomeDialog() {
someDialog.setVisible(false);
}
}
#SuppressWarnings("serial")
class SomeDialog extends JDialog {
public SomeDialog(Window window) {
super(window, "Some Dialog", ModalityType.MODELESS);
setPreferredSize(new Dimension(300, 200));
add(new JLabel("Some Dialog", SwingConstants.CENTER));
pack();
setLocationByPlatform(true);
}
}
class ShowDialogListener implements ActionListener {
private MainGui mainGui;
public ShowDialogListener(MainGui mainGui) {
this.mainGui = mainGui;
}
#Override
public void actionPerformed(ActionEvent e) {
mainGui.displaySomeDialog();
}
}
class CloseSomeDialogListener implements ActionListener {
private MainGui mainGui;
public CloseSomeDialogListener(MainGui mainGui) {
this.mainGui = mainGui;
}
#Override
public void actionPerformed(ActionEvent e) {
mainGui.closeSomeDialog();
}
}

Java Thread Start-Stop-Start on same button click

I am creating a simple java program with a GUI built with the help of window builder. The GUI consists of just a button.
On button click,start a thread that will print to the random number infinitely until it is stopped by clicking the same button again.
Here is my code
LoopTest.java
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class LoopTest extends JFrame implements ActionListener {//******
private JButton startB, stopB;
private JTextArea oa;
Start sta;
public LoopTest(){
super("Final Exam: Question ");
Container c = getContentPane();
c.setLayout(new FlowLayout());
startB = new JButton("START"); c.add(startB);
stopB = new JButton("STOP"); c.add(stopB);
oa = new JTextArea(5,20); c.add(oa);
c.add(new JScrollPane(oa));
registerEvents();
sta = new Start("Loop", oa);
}
public void registerEvents(){
startB.addActionListener(
new ActionListener(){
public void actionPerformed(ActionEvent ae){
if(startB.isEnabled() == true )
sta.setLoopFlag(true);
if(!sta.isAlive())
sta.start();
startB.setEnabled(false);
}
}
);
stopB.addActionListener(
new ActionListener(){
public void actionPerformed(ActionEvent ae){
if(stopB.isEnabled()==true){
sta.setLoopFlag(false);
}
}
}
);
}
#Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
}
public static void main(String[] args){
LoopTest app = new LoopTest();
app.setSize(300,300);
app.setLocationRelativeTo(null);
app.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
app.setVisible(true);
}
}
Start.java
public class Start extends Thread {
private JTextArea ta;
private boolean loopFlag;
public Start(String name, JTextArea ta){
super(name);
this.ta = ta;
ta.setText("");
loopFlag = true;
}
public void run(){
int num=0;
while(true)
while(loopFlag){
num = 1+ (int)(Math.random()*100);
ta.append(num + "\n");
}
}
public void setLoopFlag(boolean value){
loopFlag = value;
}
}
Stop.java
public class Stop extends Thread {
public Stop( String name ){
super(name);
}
public void run(){
}
}
Thanks in advance.
Your code breaks Swing threading rules as you're making mutational changes to Swing components off of the Swing event thread.
Suggestions:
Never extend Thread. It's almost always better to implement Runnable and use the Runnable in a Thread.
Avoid making Swing calls, other than repaint() off of the Swing event thread.
Your while (true) is a "tight" loop -- it has no Thread.sleep within it, and that means that it risks typing up the CPU in its tight loop, something that can hamper your program and your computer.
Best to avoid using direct background threading altogether here as your code issue can be solved much more easily and cleanly by using a Swing Timer. Please check the Swing Timer Tutorial
You can easily start and stop this Timer by calling its start() and stop() methods.
I would also use a JList preferentially over a JTextArea since it can more easily handle large amounts of data.
I also like using AbstractActions rather than ActionListeners for my JButton, and this problem lends itself nicely to their use. You can create an Action for start and one for stop and simply swap out the button's actions.
For example:
import java.awt.BorderLayout;
import java.awt.event.*;
import javax.swing.*;
#SuppressWarnings("serial")
public class StartStop extends JPanel {
private static final int TIMER_DELAY = 300;
private StartAction startAction = new StartAction();
private StopAction stopAction = new StopAction();
private JButton button = new JButton(startAction);
private DefaultListModel<Integer> model = new DefaultListModel<>();
private JList<Integer> jList = new JList<>(model);
private Timer timer = new Timer(TIMER_DELAY, new TimerListener());
public StartStop() {
JPanel btnPanel = new JPanel();
btnPanel.add(button);
jList.setFocusable(false);
jList.setVisibleRowCount(10);
jList.setPrototypeCellValue(100000);
JScrollPane scrollPane = new JScrollPane(jList);
setLayout(new BorderLayout());
add(scrollPane, BorderLayout.CENTER);
add(btnPanel, BorderLayout.PAGE_END);
}
private class TimerListener implements ActionListener {
#Override
public void actionPerformed(ActionEvent e) {
int num = 1 + (int) (Math.random() * 100);
model.addElement(num);
}
}
private class StartAction extends AbstractAction {
public StartAction() {
super("Start");
putValue(MNEMONIC_KEY, KeyEvent.VK_S);
}
#Override
public void actionPerformed(ActionEvent e) {
timer.start();
button.setAction(stopAction);
}
}
private class StopAction extends AbstractAction {
public StopAction() {
super("Stop");
putValue(MNEMONIC_KEY, KeyEvent.VK_S);
}
#Override
public void actionPerformed(ActionEvent e) {
timer.stop();
button.setAction(startAction);
}
}
private static void createAndShowGui() {
JFrame frame = new JFrame("Start Stop");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().add(new StartStop());
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(() -> createAndShowGui());
}
}

JButton Wont Display JPanel

I want to call My JPanel with button. My Jpanel is actually a sub JPanel from main Panel with card layout.
to do that, i am using card layout api method HERE to show the JPanel after a button was clicked.
JButton btnCallPanel1 = new JButton("Call PanelOne");
btnCallPanel1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
CardLayout card = (CardLayout)mainPanel.getLayout();
card.show(mainPanel, "PanelOne"); //call Panel One
}
output :
nothing appear, panel not called and no error pop out.
My Code is HERE
package wan.dev.sample.cardlayout;
import java.awt.CardLayout;
import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import javax.swing.JLabel;
public class HowToUseCardLayout {
private JFrame frame;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
HowToUseCardLayout window = new HowToUseCardLayout();
window.frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the application.
*/
public HowToUseCardLayout() {
initialize();
}
/**
* Initialize the contents of the frame.
*/
private void initialize() {
frame = new JFrame();
frame.setBounds(100, 100, 688, 358);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().setLayout(null);
JPanel mainPanel = new JPanel();
mainPanel.setBounds(0, 0, 672, 260);
frame.getContentPane().add(mainPanel);
mainPanel.setLayout(new CardLayout(0, 0));
JPanel PrePanel = new JPanel();
mainPanel.add(PrePanel, "name_246268073832057");
PrePanel.setLayout(null);
JLabel lblPanel_1 = new JLabel("Pre Panel");
lblPanel_1.setBounds(280, 115, 57, 20);
PrePanel.add(lblPanel_1);
JPanel panelOne = new JPanel();
mainPanel.add(panelOne, "name_246268067657434");
panelOne.setLayout(null);
JLabel lblPanel = new JLabel("panel 1");
lblPanel.setBounds(279, 118, 46, 14);
panelOne.add(lblPanel);
JButton btnPan1 = new JButton("Call PanelOne");
btnPan1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
CardLayout card = (CardLayout) mainPanel.getLayout();
card.show(mainPanel, "PanelOne");
}
});
btnPan1.setBounds(262, 286, 144, 23);
frame.getContentPane().add(btnPan1);
}
}
ANSWER
The reason i cant call my panel because i did not call it by using identifier.
i have to give identifier name to my desire jpanel and use the identifier on my cardLayout.show(..)
Public Static final String PANEL_ONE = "panel one"; //Name of JPanel Identifier
//add panel to main panel and declare panelOne identifier
mainPanel.add(panelOne, PANEL_ONE); //PANEL_ONE function like
//an identifier
JButton btnCallPanel1 = new JButton("Call PanelOne");
btnCallPanel1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
CardLayout card =
(CardLayout)mainPanel.getLayout();
card.show(mainPanel, PANEL_ONE); //call panelOne using PANEL_ONE
//instead of JPanel name
}
As I suspected — You're calling the CardLayout.show(...) method with the String parameter "PanelOne", but yet you've not added any component to the CardLayout-using container using this same String, so it makes sense that it won't work. Solution: don't do this. Use the Same String that you add the component to the CardLayout using container as the one that you use to display it.
i.e., If you want to display container foo and use the String "bar" to add it to the CardLayout-using container, then you must pass "bar" into the CardLayout's show(...) method. Again, use String constants for this so that you reduce the chances of messing up.
Other issues: You're using null layout and setBounds — Don't. Doing this makes for very inflexible GUI's that while they might look good on one platform look terrible on most other platforms or screen resolutions and that are very difficult to update and maintain.
e.g.,
import java.awt.CardLayout;
import java.awt.event.ActionEvent;
import javax.swing.*;
public class CardLayoutFoo extends JPanel {
public static final String BAR = "bar";
public static final String BUTTON_PANEL = "button panel";
private CardLayout cardlayout = new CardLayout();
public CardLayoutFoo() {
setLayout(cardlayout);
JLabel fooLabel = new JLabel("Foo", SwingConstants.CENTER);
add(fooLabel, BAR); // added using String constant, BAR
JButton showFooBtn = new JButton(new AbstractAction("Show Foo") {
#Override
public void actionPerformed(ActionEvent evt) {
// use same String, BAR, to get the fooLabl shown
cardlayout.show(CardLayoutFoo.this, BAR);
}
});
JPanel btnPanel = new JPanel();
btnPanel.add(showFooBtn);
add(btnPanel, BUTTON_PANEL);
cardlayout.show(this, BUTTON_PANEL);
}
private static void createAndShowGui() {
CardLayoutFoo mainPanel = new CardLayoutFoo();
JFrame frame = new JFrame("CardLayoutFoo");
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
frame.getContentPane().add(mainPanel);
frame.pack();
frame.setLocationByPlatform(true);
frame.setVisible(true);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGui();
}
});
}
}

How do I get user input from a JTextField?

I am attempting a very simple form designed to take user input into a JTextField and show that same input via a pop up dialog.
I can hardcode the JTextField to have a preset number using setText(). If I do this, my program works flawlessly.
However, when I leave the field blank and try getText() to show the text in the pop up dialog, I either get an empty pop up frame, or I get an 'empty string' exception (I am attempting to parse String to Double.)
package buttontest;
import java.awt.*;
import java.awt.event.ActionListener;
import java.awt.event.ComponentEvent;
import javax.swing.*;
import java.awt.event.ActionEvent;
import javax.swing.event.DocumentEvent;
import javax.swing.event.DocumentListener;
public class ButtonTest
{
public static void main(String[] args)
{
ButtonFrame frame = new ButtonFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
}
class ButtonFrame extends JFrame
{
#SuppressWarnings("LeakingThisInConstructor")
public ButtonFrame()
{
setTitle("SunStream Loan Calculator v2.0");
setSize(900,900);
ButtonPanel panel = new ButtonPanel();
panel.add(new JLabel("Enter your loan amount:"));
loanAmt = new JTextField(40);
panel.add(loanAmt);
add(panel,BorderLayout.CENTER);
}
public JTextField loanAmt;
class ButtonPanel extends JPanel implements ActionListener
{
private Component frame;
public ButtonPanel()
{
final JButton b2 = new JButton("Calculate");
add(b2, BorderLayout.SOUTH);
b2.setActionCommand("calculate");
b2.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e)
{
ButtonFrame bf = new ButtonFrame();
if("calculate".equals(e.getActionCommand()))
{
JOptionPane.showMessageDialog(frame, bf.loanAmt.getText());
}
}
});
}
#Override
public void actionPerformed(ActionEvent ae) {
throw new UnsupportedOperationException("Not supported yet.");
}
}
}
Any help would be greatly appreciated. I am researching using a KeyListener or KeyEvent but I don't quite understand it well enough.
You're creating a "shadow" ButtonFrame variable inside of the b2's ActionListener. Yes the bf variable refers to a ButtonFrame object which is of the same class as the displayed ButtonFrame object, but it refers to a completely distinct and non-visualized object. The key to a solution is to get the text from the ButtonFrame object that is actually displayed, and this can be obtained from within an inner class via the ButtonFrame.this construct:
b2.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
//!! ButtonFrame bf = new ButtonFrame();
if ("calculate".equals(e.getActionCommand())) {
//!! note use of ButtonFrame.this:
JOptionPane.showMessageDialog(frame, ButtonFrame.this.loanAmt.getText());
}
}
});
Next consider using public getters rather than accessing fields such as the JTextField directly. This reduces the chances of the code causing side effects, such as changing the properties of the JTextField object inadvertently.
For instance (changes denoted by //!! comment):
import java.awt.*;
import java.awt.event.ActionListener;
import javax.swing.*;
import java.awt.event.ActionEvent;
public class ButtonTest {
public static void main(String[] args) {
ButtonFrame frame = new ButtonFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
}
class ButtonFrame extends JFrame {
private JTextField loanAmt; // !! Make field private
#SuppressWarnings("LeakingThisInConstructor")
public ButtonFrame() {
setTitle("SunStream Loan Calculator v2.0");
setSize(900, 900);
ButtonPanel panel = new ButtonPanel();
panel.add(new JLabel("Enter your loan amount:"));
loanAmt = new JTextField(40);
panel.add(loanAmt);
add(panel, BorderLayout.CENTER);
}
// !! create a public method to get JTextField's text
// !! without exposing the JTextField itself.
public String getLoanAmtText() {
return loanAmt.getText();
}
class ButtonPanel extends JPanel implements ActionListener {
private Component frame;
public ButtonPanel() {
final JButton b2 = new JButton("Calculate");
add(b2, BorderLayout.SOUTH);
b2.setActionCommand("calculate");
b2.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
// !! ButtonFrame bf = new ButtonFrame();
if ("calculate".equals(e.getActionCommand())) {
//!! call public method on ButtonFrame object
JOptionPane.showMessageDialog(frame,
ButtonFrame.this.getLoanAmtText());
}
}
});
}
#Override
public void actionPerformed(ActionEvent ae) {
throw new UnsupportedOperationException("Not supported yet.");
}
}
}
The only way you can access your loanAmt is through ButtonPanel itself. Because you add loanAmt to this button right ?
So, if you want access loanAmt. You must get all component on this button panel. This is my psudeo code howto accessing your loanAmt from ButtonPanel class.
b2.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
ButtonFrame bf = new ButtonFrame();
if("calculate".equals(e.getActionCommand())) {
// Get all component
java.awt.Component[] componentList = this.getComponents();
JTextField txtField;
String value;
for (int i = 0; i < componentList.length; i++) {
if (componentList[i].getClass().getName().equals("javax.swing.JTextField")) {
txtField = (JTextField) componentList[i];
value = textField.getText();
}
}
if (value != null) JOptionPane.showMessageDialog(frame, value);
}
}
});

Adding jinternalframe class to jdesktoppane using other jinternalframe class

I'm creating a very simple program.
I have created this classes :
MainJframeClass, JDesktopPaneClass, JinternalFrameClass1 and JinternalFrameClass2.
what ive done is that i instantiated my jdesktoppaneclass and named it desktoppane1 and i added it to the MainJframeclass. i have also instantiated the 2 jinternalframes and named it internal1 and internal2. Now, i have button in mainjframeclass that when i press, i add the internal1 to desktoppane1. what my problem now is how to add the internal2 to desktoppane1 using a button placed somewhere in internal1. i know that why could i just add another button to desktoppane1 and add the internal2. but i have done it already, i just want to solve this problem. if you can help me please. sorry for my english by the way.
It's simply a matter of references. The code that adds something to the JDesktopPane must have a reference to it, and so you will need to pass that reference into the class that needs it say via either a constructor parameter or a method parameter.
Edit 1
For example:
import java.awt.*;
import java.awt.event.*;
import java.util.Random;
import javax.swing.*;
public class ReferenceExample extends JPanel {
private JDesktopPane desktop = new JDesktopPane();
private Random random = new Random();
public ReferenceExample() {
JButton addInternalFrameBtn = new JButton("Add Internal Frame");
addInternalFrameBtn.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
addInternalFrame();
}
});
JPanel btnPanel = new JPanel();
btnPanel.add(addInternalFrameBtn);
setPreferredSize(new Dimension(600, 450));
setLayout(new BorderLayout());
add(new JScrollPane(desktop), BorderLayout.CENTER);
add(btnPanel, BorderLayout.SOUTH);
}
public void addInternalFrame() {
MyInternalFrame intFrame = new MyInternalFrame(ReferenceExample.this);
int x = random.nextInt(getWidth() - intFrame.getPreferredSize().width);
int y = random.nextInt(getHeight() - intFrame.getPreferredSize().height);
intFrame.setLocation(x, y);
desktop.add(intFrame);
intFrame.setVisible(true);
}
private static void createAndShowUI() {
JFrame frame = new JFrame("Reference Eg");
frame.getContentPane().add(new ReferenceExample());
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
public static void main(String[] args) {
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
createAndShowUI();
}
});
}
}
class MyInternalFrame extends JInternalFrame {
// pass in the reference in the constructor
public MyInternalFrame(final ReferenceExample refEg) {
setPreferredSize(new Dimension(200, 200));
setClosable(true);
JButton addInternalFrameBtn = new JButton("Add Internal Frame");
addInternalFrameBtn.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
// use the reference here
refEg.addInternalFrame();
}
});
JPanel panel = new JPanel();
panel.add(addInternalFrameBtn);
getContentPane().add(panel);
pack();
}
}
how to add the internal2 to desktoppane1 using a button placed somewhere in internal1.
In the ActionListener added to your button you can use code like the following to get a reference to the desktop pane:
Container container = SwingUtilities.getAncestorOfClass(JDesktopPane.class, (Component)event.getSource());
if (container != null)
{
JDesktopPane desktop = (JDesktopPane)container;
JInternalFrame frame = new JInternalFrame(...);
desktop.add( frame );
}

Categories