I have multiple panels on a JFrame window. I am going to populate each panel differently every time. For example:
i start the GUI: (image center panel, right panel, bottom panel). Center panel is populated with 20 buttons, right panel with 10 buttons and bottom panel with 3.
second start of the GUI (same gui). Center panel has 50 buttons, right panel has 12 buttons, bottom has 3.
So everytime there is a random number of buttons, impossible to be all uniquely named.
Given the fact that I don't have a unique name for each button (just a list) I would like to know which buttons were clicked according to the panel they belong to. is that possible?
Somehow the buttons are being created; Let's assume you are somehow numbering them in a way you can retrieve later.
import javax.swing.JFrame;
import javax.swing.JPanel;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.util.List;
import java.util.ArrayList;
import javax.swing.JButton;
public class ButtonTest extends JFrame implements ActionListener {
public ButtonTest() {
super();
initGUI();
setDefaultCloseOperation(EXIT_ON_CLOSE);
setVisible(true);
}
private final List<JButton> buttons = new ArrayList<JButton>();
private static final int NUM_BUTTONS = 20;
public void initGUI() {
JPanel panel = new JPanel();
for (int i = 0; i < NUM_BUTTONS; i++) {
String label = "Button " + i;
JButton button = new JButton(label);
button.setActionCommand(label);
button.addActionListener(this);
buttons.add(button);
panel.add(button);
}
getContentPane().add(panel);
}
public static void main(String[] args) {
new ButtonTest();
}
public void actionPerformed(ActionEvent e) {
String actionCommand = ((JButton) e.getSource()).getActionCommand();
System.out.println("Action command for pressed button: " + actionCommand);
// Use the action command to determine which button was pressed
}
}
The ActionEvent has a getSource() method which will be the reference to the button that was clicked. You can then check the action command of the button if you need to.
If you want to know which panel contains the button, try calling getParent() on the JButton itself. To find out which button was clicked, as camickr suggests, use getSource() on the ActionEvent.
Related
I am trying to create a file according to two events:
the first is a JRadioButton: according to which radio button is selected, the file will be saved in the respective folder, the second event is the "add" button. I am struggling with creating the file in the right folder, so far I managed to create the file in the right folder when the radio button is selected, but that is not what I want.
How can I create the file at the right moment (when "add" button is pressed) according to the previously selected radio button?
Things like:
if(e.getSource() == button && e.getActionCommand() == jradio1)
Doesn't work and neither deos nested if.
I think I'm missing the big picture, anyone can help?
You could create a object that stores the location (possibly a String containing the filepath) of where you want the file, and which will update when you click on a given jradiobutton.
When you click the add button, you can then reference the same object to see where you should put the file.
Selected radio and the event that is fired when you select the radio button are two different thing.
From what you have have stated is that you want to create the file on click of the add button.
Do this
You can just ignore the event fired on the radio selection.
When the event is fired on the press of the Add button, read the state of the
radio and save the file
You can keep the instance of the radio class wide so that you can read it in the event handler for the button listener.
Just use the name of the Radio button as the action command. Same for the Add button. When the radio button is selected, it saves its name as the target location. Here is an example of how it can work.
import java.awt.Dimension;
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.JPanel;
import javax.swing.JRadioButton;
import javax.swing.SwingUtilities;
public class SavingFiles extends JPanel {
String selectedLocation = null;
public static void main(String[] args) {
SwingUtilities.invokeLater(() -> new SavingFiles().start());
}
public void start() {
String[] fileLocations =
{ "f:/location1", "c:/location2", "G:/location3"
}; // etc
int nRadioButtons = fileLocations.length;
int defaultLoc = 0;
YourActionListener listener = new YourActionListener();
ButtonGroup group = new ButtonGroup();
for (int i = 0; i < nRadioButtons; i++) {
if (i == defaultLoc) {
selectedLocation = fileLocations[i];
}
JRadioButton button =
new JRadioButton(fileLocations[i], i == defaultLoc);
group.add(button);
button.addActionListener(listener);
// do something with button
// store it or add it to panel.
add(button);
}
JButton addButton = new JButton("Add"); // also acts as actionCommand
addButton.addActionListener(listener);
add(addButton);
// Boiler plate
JFrame frame = new JFrame("SavingFiles");
frame.add(this);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setPreferredSize(new Dimension(300, 80));
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
// Note that if actionCommand is not set, then the button name
// serves the same function.
private class YourActionListener implements ActionListener {
public void actionPerformed(ActionEvent ev) {
String actionCommand = ev.getActionCommand();
if (actionCommand.equals("Add")) {
// add file to 'selectedLocation'
System.out.println("adding file to " + selectedLocation);
}
else {
selectedLocation = actionCommand;
System.out.println("Location set to " + selectedLocation);
}
}
}
}
I was tasked with creating a program that displays a numeric keypad (like one on a phone) and has a screen that displays the numbers that are picked. Also included is a clear button that clears the screen.
In creating my program, I created three classes. The Phone class simply creates a JFrame that adds a PhonePanel to the screen. The PhonePanel class adds a JLabel which acts as a screen, a JButton which acts as a clear button, and a KeypadPanel which is a GridLayout of JButtons which acts as the numeric keys.
The clear button and numeric buttons both use separate action listeners. Is this the most efficient way of going about this? Is there a way I can use one action listener instead of two?
// ******************************************************************************************
// Phone.java
// David Read
// This class creates a JFrame that contains a PhonePanel. The PhonePanel provides a
// user interface that allows one to input numeric symbols on a screen and allows clearing
// of the screen.
// ******************************************************************************************
package lab5;
import javax.swing.JFrame;
public class Phone {
public static void main(String[] args)
{
// Create a JFrame object.
JFrame frame = new JFrame ("Phone");
// Set the default close operation for the JFrame.
frame.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);
// Add a Phone panel to the screen.
frame.getContentPane().add(new PhonePanel());
frame.pack();
frame.setVisible(true);
}
}
// ******************************************************************************************
// PhonePanel.java
// David Read
// This class creates a JPanel that includes an output label which displays inputed numeric
// symbols, a clear button that clears what is displayed on the output label, and a KeypadPanel
// which displays a GridLayout of buttons that when pressed, display their corresponding symbols
// on the output label.
// ******************************************************************************************
package lab5;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
public class PhonePanel extends JPanel
{
private static JLabel labelOutput;
private JButton buttonClear;
//-----------------------------------------------------------------------
// Creates a JPanel that arranges three objects in a Border layout. The
// north component contains a JLabel, the east component contains a JButton
// and the center component contains a KeypadPanel.
//-----------------------------------------------------------------------
public PhonePanel()
{
// Set the layout manager, size and background color of the Phone Panel.
setLayout(new BorderLayout());
setPreferredSize (new Dimension(400, 200));
setBackground (Color.yellow);
// Output label created.
labelOutput = new JLabel(" ");
// Clear button created, assigned a button title and assigned an action listener.
buttonClear = new JButton();
buttonClear.setText("Clear");
buttonClear.addActionListener(new ClearButtonListener());
// Add the JLabel, JButton and KeypadPanel to the PhonePanel.
add(labelOutput, BorderLayout.NORTH);
add(buttonClear, BorderLayout.EAST);
add(new KeypadPanel(), BorderLayout.CENTER);
}
//-----------------------------------------------------------------------
// Adds the specified symbol to the output label.
//-----------------------------------------------------------------------
public static void addToOutputLabel(String input)
{
// Create a String object to hold the current value of the output label.
String label = labelOutput.getText();
// Append the inputed String onto the String.
label += input;
// Update the output label with the appended String.
labelOutput.setText(label);
}
//-----------------------------------------------------------------------
// Listens for the clear button to be pressed. When pressed, the output
// label is reassigned as blank.
//-----------------------------------------------------------------------
private class ClearButtonListener implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
labelOutput.setText(" ");
}
}
}
// ******************************************************************************************
// KeypadPanel.java
// David Read
// This class creates a JPanel that contains several buttons which when pressed, adds their
// corresponding numeric symbol to the output label in the PhonePanel.
// ******************************************************************************************
package lab5;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JPanel;
public class KeypadPanel extends JPanel
{
private JButton button1, button2, button3, button4, button5, button6, button7, button8, button9, buttonStar, button0, buttonNumber;
//-----------------------------------------------------------------------
// Creates a JPanel that arranges several JButtons in a GridLayout. Each
// of the buttons are assigned button titles, action listeners and
// action commands.
//-----------------------------------------------------------------------
public KeypadPanel()
{
// Set layout to a GridLayout with 4 rows and 3 columns.
setLayout(new GridLayout(4,3));
// Create new JButtons.
button1 = new JButton();
button2 = new JButton();
button3 = new JButton();
button4 = new JButton();
button5 = new JButton();
button6 = new JButton();
button7 = new JButton();
button8 = new JButton();
button9 = new JButton();
buttonStar = new JButton();
button0 = new JButton();
buttonNumber = new JButton();
// Assign button titles to the JButtons.
button1.setText("1");
button2.setText("2");
button3.setText("3");
button4.setText("4");
button5.setText("5");
button6.setText("6");
button7.setText("7");
button8.setText("8");
button9.setText("9");
buttonStar.setText("*");
button0.setText("0");
buttonNumber.setText("#");
// Create a new KeypadButtonListener.
KeypadButtonListener listener = new KeypadButtonListener();
// Assign the listener as an action listener for all of the JButton objects.
button1.addActionListener(listener);
button2.addActionListener(listener);
button3.addActionListener(listener);
button4.addActionListener(listener);
button5.addActionListener(listener);
button6.addActionListener(listener);
button7.addActionListener(listener);
button8.addActionListener(listener);
button9.addActionListener(listener);
buttonStar.addActionListener(listener);
button0.addActionListener(listener);
buttonNumber.addActionListener(listener);
// Set the action commands for all of the JButtons.
button1.setActionCommand("1");
button2.setActionCommand("2");
button3.setActionCommand("3");
button4.setActionCommand("4");
button5.setActionCommand("5");
button6.setActionCommand("6");
button7.setActionCommand("7");
button8.setActionCommand("8");
button9.setActionCommand("9");
buttonStar.setActionCommand("*");
button0.setActionCommand("0");
buttonNumber.setActionCommand("#");
// Add the JButtons to the KeypadPanel.
add(button1);
add(button2);
add(button3);
add(button4);
add(button5);
add(button6);
add(button7);
add(button8);
add(button9);
add(buttonStar);
add(button0);
add(buttonNumber);
}
//-----------------------------------------------------------------------
// Listens for all of the buttons to be pressed. When a particular button
// is pressed, the addToOutputLabel method of the PhonePanel is called
// with the input being the action command of the button pressed.
//-----------------------------------------------------------------------
private class KeypadButtonListener implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
// Add the action command string to the output label.
PhonePanel.addToOutputLabel(e.getActionCommand());
}
}
}
In this case it is better to use two separate action listeners as functionality for clear button and for number buttons is different.
It is considered as best practice to use single responsibility principle (https://en.wikipedia.org/wiki/Single_responsibility_principle) when developing your classes. This will make your code more maintainable and easier to read and modify.
It is better to use multiple ActionListeners here, however, if you still desire to use one ActionListener instead you may make a separate class to handle all actions similar to this.
public class KeyListener implements ActionListener {
#Override
public void actionPerformed(ActionEvent e) {
//Get the name of the ActionEvent
String cmd = e.getActionCommand();
//Here the Actionevent is only checked to see if it is a "Clear" or not
//If you need to impliment more then a switch statment may be appropriate
if(cmd.equals("Clear")) {
//Clear Label with additional setter method
PhonePanel.clearLabel();
}
else {
PhonePanel.addToOutputLabel(e.getActionCommand());
}
}
So I have 1 class, with a radio button and 1 class, that will create an applet depending on the outcome of the Radio Button. I don't know how to make the graphics run depending on an if/else statement. All help will be greatly appreciated.
Radio Button Class:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class RadioButton extends JPanel {
static JFrame frame;
JLabel pic;
RadioListener myListener = null;
protected JRadioButton displacement;
protected JRadioButton accel;
protected JRadioButton time;
public RadioButton() {
// Create the radio buttons
displacement = new JRadioButton("Displacement");
displacement.setMnemonic(KeyEvent.VK_N);
displacement.setSelected(true);
//Displacement Button, set to automatically be clicked
accel = new JRadioButton("Acceleration");
accel.setMnemonic(KeyEvent.VK_A);
accel.setActionCommand("acceleration");
//Acceleration Button
time = new JRadioButton("Change in time");
time.setMnemonic(KeyEvent.VK_S);
time.setActionCommand("deltaT");
//The change in time button
// Creates the group of buttons
ButtonGroup group = new ButtonGroup();
group.add(displacement);
group.add(accel);
group.add(time);
myListener = new RadioListener();
displacement.addActionListener(myListener);
accel.addActionListener(myListener);
time.addActionListener(myListener);
// Set up the picture label
pic = new JLabel(new ImageIcon(""+"numbers" + ".jpg")); //Set the Default Image
pic.setPreferredSize(new Dimension(177, 122));
// Puts the radio buttons down
JPanel panel = new JPanel();
panel.setLayout(new GridLayout(0, 1));
panel.add(displacement);
panel.add(accel);
panel.add(time);
setLayout(new BorderLayout());
add(panel, BorderLayout.WEST);
add(pic, BorderLayout.CENTER);
setBorder(BorderFactory.createEmptyBorder(40,40,40,40));
}
//Listening to the buttons
class RadioListener implements ActionListener {
public void actionPerformed(ActionEvent e) {
pic.setIcon(new ImageIcon(""+e.getActionCommand()
+ ".jpg"));
}
}
public static void main(String s[]) {
frame = new JFrame("∆x = Vavg * time");
frame.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {System.exit(0);}
});
frame.getContentPane().add(new RadioButton(), BorderLayout.CENTER);
frame.pack();
frame.setVisible(true);
}
}
If/Else Statements class:
import java.lang.Object;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.*;
public class RadioButtonMain extends RadioButton {
public static void main(String [ ] args) {
new RadioButtonMain().doMain();
}
public void doMain() {
if ( displacement.isSelected()) {
//option 1 for applet
}
if ( accel.isSelected()) {
//Option 2 for applet
}
else {
//Option 3 for applet
}
}
}
How would I get the graphics to run based on whether or not the variables accel and displacement are pressed? Thanks.
Remember, a GUI is an event driven environment, things don't run within a linear manner. Instead of trying run a method yourself, you need to use a callback or listener of some kind which will tell you when the state of the program/buttons change...
When the JRadioButton actionPerformed event is raised, you need to call another method which provides information about what has occurred. You can then override these methods in your RadioButtonMain class and take action when they are called
This is very similar to an Observer Pattern
In the below code I am attempting to move the three buttons to the left when you click the left button. When I click it; nothing happens currently. Can anyone explain to me what I am doing wrong here? Also, for some reason it has stopped compiling correctly and I am unsure why but I BELIEVE it is because of a mistake in my code while attempting to get the buttons to move to the left when you click the button. I do NOT want the window to move. Just the buttons within the window. Does any one see what I am doing wrong and can you explain it?
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class Buttons extends JFrame {
//Control Definitions
JButton resetButton;
JButton leftButton;
JButton colorButton;
JPanel buttonPanel;
// Layout Definiton
eventHandle evt;
FlowLayout flt;
Point point; //to Hold Previous Window Position
Color color; //to Hold Previous Color
public Buttons() {
super("Buttons Window");
flt = new FlowLayout();//inialize the Flow Layout
buttonPanel = new JPanel(flt);
//inialize the buttonPanel With Flow Layout
//initialize buttons
resetButton = new JButton("Reset");
leftButton = new JButton("Left");
colorButton = new JButton("Blue");
evt = new eventHandle(); //initiate the eventhandle class
buttonPanel.add(leftButton); //add leftButton
buttonPanel.add(colorButton);//add colorButton
buttonPanel.add(resetButton);//add colorButton
getContentPane().add(buttonPanel);//buttonPanel
//add actionlistners
leftButton.addActionListener(evt);
colorButton.addActionListener(evt);
resetButton.addActionListener(evt);
setBounds(20, 120, 250, 70);
//following Initate the point with Center of Scren
point = new Point((Toolkit.getDefaultToolkit().
getScreenSize().width - getWidth()) / 2,
(Toolkit.getDefaultToolkit().getScreenSize().height
- getHeight()) / 2);
setLocation(point); //locates the window in center
color = buttonPanel.getBackground();//stores the initial color
setDefaultCloseOperation(EXIT_ON_CLOSE);
setVisible(true);
}
class eventHandle implements ActionListener { //Event Handler
public void actionPerformed(ActionEvent e) {
{
if (e.getSource() == leftButton) ///if its from leftButton
{
leftButton.setAlignmentX(Component.LEFT_ALIGNMENT);
colorButton.setAlignmentX(Component.LEFT_ALIGNMENT);
resetButton.setAlignmentX(Component.LEFT_ALIGNMENT);
//setLocation( (point.x -150), point.y);//shift the window 150 pixels left
} else if (e.getSource() == colorButton) {
buttonPanel.setBackground(color.BLUE);
//sets the backgorund to Blue
} else {
leftButton.setAlignmentX(Component.CENTER_ALIGNMENT);
//sets the location to previous location
colorButton.setAlignmentX(Component.CENTER_ALIGNMENT);
resetButton.setAlignmentX(Component.CENTER_ALIGNMENT);
}
}
}
}
public static void main(String[] args) {
Buttons buttonwindow = new Buttons();
}
}
It has stopped compiling, because you deleted one accolade, so put one accolade "}" just above the method:
public static void main(String[] args)
and the code should compile. pls feedback.
EDIT:
Also rewrite your main method like this:
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
#Override
public void run() {
Buttons buttonwindow = new Buttons();
}
}
);
}
Every usage of Swing components must be done thorugh the Event Dispatch Thread (abbreviated EDT) or you will probably get unwanted visual effects. See here for explanation.
EDIT^2:
To achieve the desired behavior, rewrite the the action listener like this:
if (e.getSource() == leftButton) {
((FlowLayout)buttonPanel.getLayout()).setAlignment(FlowLayout.LEFT); //1
buttonPanel.revalidate(); //2
}
else if (e.getSource() == colorButton) {
buttonPanel.setBackground(color.BLUE);
}
else {
((FlowLayout)buttonPanel.getLayout()).setAlignment(FlowLayout.CENTER);
buttonPanel.revalidate();
}
Any change to the visual appereance to the Swing component must be done through the assigned layout manager, in this case FlowLayout - in line 1.
To see the change you must notify the Swing components layout manager to rearrange the components - in line 2 the revalidate() method "notifies" the layout manager to recalculate the new positions and eventually "notifies" the EDT to draw it on the screen.
You should update the layout manager to align the components to the left or right. Try something like;
((FlowLayout)getLayout()).setAlignment(FlowLayout.LEFT);
Instead
You code won't compile as the static main method appears inside the inner class eventHandle. You can fix simply by moving it into the class body of the outer class Buttons.
As you have all the objects references at class level, you could do the button alignment using, for instance:
flt.setAlignment(FlowLayout.RIGHT);
buttonPanel.revalidate();
...
Here you are adjusting the layout alignment of your FlowLayout and revalidating to visually reflect the updated changes on your panel.
I'm learning Java and GUI. I have some questions, and the first is if there are any major difference between creating a subclass of JFrame and an instance of JFrame. It seems like like a subclass is more powerful? I also wonder if it's necessary to use this code when creating a GUI:
Container contentPane = getContentPane();
contentPane.setLayot(new Flowlayout());
I add my GUI class, it's a simple test so far, to a task that I have to hand in. When a user has entered some text in the textfield and press the button to continue to the next step, how do I do to clear the frame and show a new content or is there a special way to do this is in Java? I guess there must be better to use the same window instead of creating a new!? Help id preciated! Thanks
// Gui class
import java.awt.FlowLayout; // layout
import java.awt.event.ActionListener; // listener
import java.awt.event.ActionEvent; // event
import javax.swing.JFrame; // windows properties
import javax.swing.JLabel; // row of text
import javax.swing.JTextField; // enter text
import javax.swing.JOptionPane; // pop up dialog
import javax.swing.JButton; // buttons
// import.javax.swing.*;
public class Gui extends JFrame {
private JLabel text1;
private JTextField textInput1;
private JTextField textInput2;
private JButton nextButton;
// constructor creates the window and it's components
public Gui() {
super("Bank"); // title
setLayout(new FlowLayout()); // set default layout
text1 = new JLabel("New customer");
add(text1);
textInput1 = new JTextField(10);
add(textInput1);
nextButton = new JButton("Continue");
add(nextButton);
// create object to handle the components (action listener object)
frameHandler handler = new frameHandler();
textInput1.addActionListener(handler);
nextButton.addActionListener(handler);
}
// handle the events (class inside another class inherits contents from class outside)
private class frameHandler implements ActionListener {
public void actionPerformed(ActionEvent event){
String input1 = "";
// check if someone hits enter at first textfield
if(event.getSource() == textInput1){
input1 = String.format(event.getActionCommand());
JOptionPane.showMessageDialog(null, input1);
}
else if(event.getSource() == nextButton){
// ??
}
}
}
}
This small code might help you explain things :
import java.awt.event.*;
import javax.swing.*;
public class FrameDisplayTest implements ActionListener
{
/*
* Creating an object of JFrame instead of extending it
* has no side effects.
*/
private JFrame frame;
private JPanel panel, panel1;
private JTextField tfield;
private JButton nextButton, backButton;
public FrameDisplayTest()
{
frame = new JFrame("Frame Display Test");
// If you running your program from cmd, this line lets it comes
// out of cmd when you click the top-right RED Button.
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
panel = new JPanel();
panel1 = new JPanel();
tfield = new JTextField(10);
nextButton = new JButton("NEXT");
backButton = new JButton("BACK");
nextButton.addActionListener(this);
backButton.addActionListener(this);
panel.add(tfield);
panel.add(nextButton);
panel1.add(backButton);
frame.setContentPane(panel);
frame.setSize(220, 220);
frame.setVisible(true);
}
public void actionPerformed(ActionEvent ae)
{
JButton button = (JButton) ae.getSource();
if (tfield.getText().length() > 0)
{
if (button == nextButton)
{
/*
* this will remove the first panel
* and add the new panel to the frame.
*/
frame.remove(panel);
frame.setContentPane(panel1);
}
else if (button == backButton)
{
frame.remove(panel1);
frame.setContentPane(panel);
}
frame.validate();
frame.repaint(); // prefer to write this always.
}
}
public static void main(String[] args)
{
/*
* This is the most important part ofyour GUI app, never forget
* to schedule a job for your event dispatcher thread :
* by calling the function, method or constructor, responsible
* for creating and displaying your GUI.
*/
SwingUtilities.invokeLater(new Runnable()
{
#Override
public void run()
{
new FrameDisplayTest();
}
});
}
}
if you want to switch (add then remove) JComponents, then you have to
1) add/remove JComponents and then call
revalidate();
repaint()// sometimes required
2) better and easiest choice would be implements CardLayout
If your requirement is to make a wizard, a panel with next and prev buttons, and on clicking next/prev button showing some component. You could try using CardLayout.
The CardLayout manages two or more components (usually JPanel instances) that share the same display space. CardLayout let the user choose between the components.
How to Use CardLayout
If your class extends JFrame, you can do:
getContentPane().removeAll();