Static variable and actionePerformed method? - java

I have a class called Windows. The class extends JFrame and adds GUI components to the JFrame container. One of those components is a JTextfield. I am trying to set the text in the JTextfield through the actionPerformed() when generator JButton is clicked. The actionPerformed() is a class called EvenHandler. This is the eventHandler:
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Random;
public class EventHandler implements ActionListener {
int x = 0;
PassWordGenerator password;
Window Window; // It works only when static Window Window.
public void start() {
Window = new Window();
}
#Override
public void actionPerformed(ActionEvent e) {
password = new PassWordGenerator(3,3,3,3);
Window.setGeneratedPswd(password.getPswd());
x += 1;
System.out.println(x);
}
public static void main(String[] args) {
EventHandler x = new EventHandler();
x.start();
}
}
the window class if you want to know how the GUI looks like. The button is the one calling actionePerfome().
import javax.swing.*;
import java.awt.*;
public class Window extends JFrame {
Label passwordLength;
Label labelGnPswd;
JTextField psdLength;
JCheckBox upperCase_letters;
JCheckBox lowerCase_letters;
JCheckBox numbers;
JCheckBox symbols;
JTextField generatedPswd;
EventHandler event = new EventHandler();
JButton generetor;
public Window() {
setLayout(new FlowLayout());
setTitle("PasswordGenerator");
setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
setLocationRelativeTo(null);
setResizable(false);
//Password Length
passwordLength = new Label("Password Length");
add(passwordLength);
//Input text
psdLength = new JTextField("0", 5);
add(psdLength);
//Create checkBoxes
createcheckbxs();
//Label Generated psw
labelGnPswd = new Label("Generated pswd");
add(labelGnPswd);
//Generated Password;
generatedPswd = new JTextField("****", 5);
generatedPswd.setEditable(false);
add(generatedPswd);
//Button
generetor = new JButton("Generate pswd!");
generetor.addActionListener(event);
add(generetor);
setSize(200, 400);
setVisible(true);
}
public String getpsdLength() {
return psdLength.getText();
}
public void setGeneratedPswd(String pswd) {
generatedPswd.setText(pswd);
}
private void createcheckbxs() {
upperCase_letters = new JCheckBox("Include uppercase");
add(upperCase_letters);
lowerCase_letters = new JCheckBox("Include lowercase");
add(lowerCase_letters);
numbers = new JCheckBox("Include numbers ");
add(numbers);
symbols = new JCheckBox("Include symbols ");
add(symbols);
}
}
My question is that When I clicked on the generator JButton I get an error message along the line, "Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException..." I debugged the actionPerformed() and I found out that the Window Window is null when actionPefromed() is called after clicking genetor Jbutton. Why is Window null? Int x is working fine and it is not null. Isn't var x and window the same varaible scope. The only way I could keep the value of Window not null was to make Window a static variable.I hope my problem is a little more clear. Thanks in advance

Your problem seems in this line in Window.java:
generetor.addActionListener(event);
Variable event is declared in Window.java:
EventHandler event = new EventHandler();
The problem is that on event start() is not called so its instance variable Window Window remains null. The EventHandler you instantiate in main() is not used. What you might do is delete your start method and put its contents in the constructor.

Related

I have a small eror in my code here (swing-java-JFrame)

First I am a beginner in java. I'm making a window with small button and a label (with 0 in default position), when I click on the button the label will change to 1 and when I tap another click the button will be 2. But, I have an error in calling the method.
my code:
package prototype;
import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
public class Prototype {
public static int count;
public static JLabel l;
public void Proto()
{
JFrame f = new JFrame();
JButton b = new JButton("click");
JLabel lo = new JLabel("0");
JPanel p = new JPanel();
f.setBounds(120,120,500,500);
b.addActionListener(new MyAction());
p.add(lo);
p.add(b);
f.getContentPane().add(p,BorderLayout.CENTER);
f.show();}
public class MyAction implements ActionListener {
#Override
public void actionPerformed(ActionEvent e) {
count++;
l.setText(Integer.toString(count));}
public static void main(String[] args) {
//I want to call the proto method but it give me an eror
new proto();
}}}
public class Prototype extends JFrame{
private static int count;
private JLabel l;
public Prototype() {
super();
JButton b = new JButton("click");
l = new JLabel("0");
JPanel p = new JPanel();
b.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent arg0) {
count++;
l.setText(Integer.toString(count));
}
});
p.add(l);
p.add(b);
this.getContentPane().add(p, BorderLayout.CENTER);
this.pack();
this.setVisible(true);
}
public static void main(String...args){
Prototype p=new Prototype();
}
}
I changed the method to a constructor, to have the possibility of creating a object of type Prototype and directly create a frame with it. Also I extended the class with JFrame to not need to create an extra JFrame. Next step was to remove the ActionListener class and creating a new ActionListener while adding it to the button. In my eyes this is useful if you have several buttons with different functionalities, so you can see the function of the button directly just by looking at the code of the button. and the last step was to create a new Object of type Prototype in the main method
If I we're you use a SwingWorker instead of manually setting the text of JLabel. Because this is not a proper way updating your GUI. This should be done using SwingWorker. Please read about publish and processmethod.

Change JTextArea texts from JButton's actionPerformed which is in another class

I have a fully functional console-based database which I need to add GUIs to. I have created a tab page (currently only one tab) with a button "Display All Student" which when triggered will display a list of students inside a JTextArea which of course is in its own class and not inside the button's action listener class. Problem is, the JTextArea is not recognised inside button's action listener. If I add parameter into the action listener, more errors arise. Help?
I have searched Stack Overflow for similar problems but when I tried it in my code, doesn't really do the trick? Or maybe I just need a nudge in the head. Anyways.
Here is my code so far:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class StudDatabase extends JFrame
{
private JTabbedPane tabbedPane;
private JPanel studentPanel;
private static Scanner input = new Scanner(System.in);
static int studentCount = 0;
static Student studentArray[] = new Student[500];
public StudDatabase()
{
setTitle("Student Database");
setSize(650, 500);
setBackground(Color.gray);
JPanel topPanel = new JPanel();
topPanel.setLayout( new BorderLayout() );
getContentPane().add( topPanel );
// Create the tab pages
createStudentPage();
// more tabs later...
// Create a tab pane
tabbedPane = new JTabbedPane();
tabbedPane.addTab( "Student Admin", studentPanel );
topPanel.add( tabbedPane, BorderLayout.CENTER );
}
public void createStudentPage()
{
studentPanel = new JPanel();
studentPanel.setLayout(new FlowLayout(FlowLayout.CENTER));
JButton listButton = new JButton("List All Student(s)");
listButton.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent event)
{
if(studentCount > 0)
{
for(int i=0; i<studentCount; i++)
{
// print out the details into JTextArea
// ERROR! textDisplay not recognised!!!
textDisplay.append("Student " + i);
}
System.out.printf("\n");
}
else // no record? display warning to user
{
System.out.printf("No data to display!\n\n");
}
}
});
studentPanel.add(listButton);
JTextArea textDisplay = new JTextArea(10,48);
textDisplay.setEditable(true); // set textArea non-editable
JScrollPane scroll = new JScrollPane(textDisplay);
scroll.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
studentPanel.add(scroll);
}
public static void main(String[] args)
{
StudDatabase mainFrame = new StudDatabase();
mainFrame.setVisible(true);
}
Your code isn't working for the same reason this wouldn't work:
int j = i+5;
int i = 4;
You have to declare variables before using them in Java.
Secondly, in order to use a variable (local or instance) from inside an inner class - which is what your ActionListener is - you need to make it final.
So, the below code will compile and run:
final JTextArea textDisplay = new JTextArea(10,48);
...
listButton.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent event)
{
...
textDisplay.append("Student " + i);

about ItemEvent in java GUI

I'm doing a small program with Java GUI
here is mu code:
// DebugFourteen3
// User selects pizza topping and sees price
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
//use correct spelling of class name
public class DebugFourteen3 extends JFrame
{
FlowLayout flow = new FlowLayout();
JComboBox pizzaBox = new JComboBox();
JLabel toppingList = new JLabel("Topping List");
JLabel aLabel = new JLabel("Paulos's American Pie");
JTextField totPrice = new JTextField(10);
int[] pizzaPrice = {7,10,10,8,8,8,8};
int totalPrice = 0;
String output;
int pizzaNum;
public DebugFourteen3()
{
super("Pizza List");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLayout(flow);
add(toppingList);
pizzaBox.addItem("cheese");
pizzaBox.addItem("sausage");
pizzaBox.addItem("pepperoni");
pizzaBox.addItem("onion");
pizzaBox.addItem("green pepper");
pizzaBox.addItem("green olive");
pizzaBox.addItem("black olive");
add(pizzaBox);
add(aLabel);
add(totPrice);
itemStateChanged(this);
}
public static void main(String[] arguments)
{
JFrame frame = new DebugFourteen3();
frame.setSize(200, 150);
frame.setVisible(true);
}
public void itemStateChanged(ItemEvent[] list)
{
Object source = list.getSource();
if(source == pizzaBox)
{
int pizzaNum = pizzaBox.getSelectedIndex();
totalPrice = pizzaPrice[pizzaNum];
output = "Pizza Price $" + totalPrice;
totPrice.setText(output);
}
}
}
the compiler gets me error on line 35, it says itemStateChanged() should receive a argument which type is ItemEvent[] but I'm passing "this"(the class it self)
can anyone explain how the itemStateChanged works with the JComboBox?
thanks
You have to add the listener on the combo box instead of calling the itemStateChanged method directly. Adding the listener will enable the callbacks when the click event happens, and jvm will call the itemStateChanged method automatically as combo box is registered for the event. Replace this line
itemStateChanged(this);
with
pizzaBox.addItemListener(this);
At first, you need to implements ItemListener with JFrame. Than add following line with pizzaBox, this will notify overrided public void itemStateChanged(ItemEvent ev) method ItemListener.
pizzaBox.addItemListener(this);
For details, see this tutorial.
Problem :
1.you are not registering itemListener for JComboBox
2.itemStateChanged event takes ItemEvent as argument not ItemEvent[] array
1.Replace this :
itemStateChanged(this);
with this:
pizzaBox.addItemListener(this);
2.Replace This:
public void itemStateChanged(ItemEvent[] list)
With this:
public void itemStateChanged(ItemEvent list)
Complete Code:
// DebugFourteen3
// User selects pizza topping and sees price
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
//use correct spelling of class name
public class DebugFourteen3 extends JFrame
{
FlowLayout flow = new FlowLayout();
JComboBox pizzaBox = new JComboBox();
JLabel toppingList = new JLabel("Topping List");
JLabel aLabel = new JLabel("Paulos's American Pie");
JTextField totPrice = new JTextField(10);
int[] pizzaPrice = {7,10,10,8,8,8,8};
int totalPrice = 0;
String output;
int pizzaNum;
public DebugFourteen3()
{
super("Pizza List");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLayout(flow);
add(toppingList);
pizzaBox.addItem("cheese");
pizzaBox.addItem("sausage");
pizzaBox.addItem("pepperoni");
pizzaBox.addItem("onion");
pizzaBox.addItem("green pepper");
pizzaBox.addItem("green olive");
pizzaBox.addItem("black olive");
add(pizzaBox);
add(aLabel);
add(totPrice);
pizzaBox.addItemListener(this);
}
public static void main(String[] arguments)
{
JFrame frame = new DebugFourteen3();
frame.setSize(200, 150);
frame.setVisible(true);
}
public void itemStateChanged(ItemEvent list)
{
Object source = list.getSource();
if(source == pizzaBox)
{
int pizzaNum = pizzaBox.getSelectedIndex();
totalPrice = pizzaPrice[pizzaNum];
output = "Pizza Price $" + totalPrice;
totPrice.setText(output);
}
}
}
Here this i.e DebugFourteen3 is not a type ItemEvent. Thats why
itemStateChanged(ItemEvent[] list)
gives error.
If you want to handle itemStateChanged then you need to implement ItemLisntener and add actionListner to it as
pizzaBox.addItemListener(this);
also add Handler method of ItemLisntener as
public void itemStateChanged(....)
{
//Handling Code goes here
}
Or you can implement ActionListner also
For more information on how to use combobox you can visit here

Using AWT Buttons and detecting if clicked

I just joined, and am glad to be here~ So, this morning (at like 2am, but thats besides the point :P ) I was doing a little bit of Java tests with JFrame and other GUI stuff. This is my first time working with GUIs. I was trying to make a little java app that would act as a dream journaller. However, my progress was frozen when I encountered a problem i could not solve. My code is as follows.
import java.awt.*;
import javax.swing.*;
import java.applet.*;
public class Display extends Canvas
{
static final int WIDTH = 600;
static final int HEIGHT = 400;
public static String defaultEntry = "Dreams...";
public static final String TITLE = "Dream Journal Testing";
Button erase;
public static void main(String[] args)
{
Display d = new Display();
d.create();
}
public void create()
{
JFrame frame = new JFrame();
System.out.println("Running");
Panel cardOne = new Panel();
Panel p1 = new Panel();
Panel p2 = new Panel();
Panel p3 = new Panel();
Panel grid = new Panel();
cardOne.setLayout(new BorderLayout());
p1.setLayout(new GridLayout(2,1,3,6));
TextArea textArea1 = new TextArea(defaultEntry);
/*Font f1 = new Font("Courier", Font.PLAIN, 16);
setFont(f1);*/
Label l1 = new Label("Welcome to the Dream Journal! :)");
Label l2 = new Label("Type your dream below:");
p1.add(l1);
p1.add(l2);
p2.add(textArea1);
p3.setLayout(new FlowLayout(FlowLayout.CENTER));
Button ok = new Button("Save");
erase = new Button("Erase");
p3.add(erase);
p3.add(ok);
cardOne.add("North",p1);
cardOne.add("Center",p2);
cardOne.add("South",p3);
frame.add(cardOne);
//frame.add(cardOne);
//frame.setLocationRelativeTo(null);
frame.pack();
frame.setTitle(TITLE);
frame.setSize(WIDTH, HEIGHT);
frame.setResizable(false);
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
System.out.println(textArea1.getText());
}
/*public boolean handleEvent(Event evt)
{
if(evt.target == erase)
{
System.out.println("it works");
return true;
}
else return super.handleEvent(evt);
}
*/
public boolean action(Event evt, Object arg)
{
if("Erase".equals(arg))
{
System.out.println("hello");
//textArea1.setText("");
}
return true;
}
}
The problem i have is I am not able to figure out how to make it so if the "Erase" AWT button is pushed, the system will print a line (as a test). I have tried
public boolean action(Event evt, Object arg)
And
public boolean handleEvent, but neither worked. Anyone have any suggestions for the Java noob that is me? Thanks!! :)
One way is to add an action listener to the button (e.g. for Save). Another way is to create an Action (e.g. for Erase).
Don't mix Swing with AWT components unless it is necessary. It is not worth even learning how to use AWT components at this point in time, use Swing only for best results and best help.
Here is a version of the app. using all Swing components.
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class Display
{
static final int WIDTH = 600;
static final int HEIGHT = 400;
public static String defaultEntry = "Dreams...";
public static final String TITLE = "Dream Journal Testing";
JButton erase;
public static void main(String[] args)
{
Display d = new Display();
d.create();
}
public void create()
{
JFrame frame = new JFrame();
System.out.println("Running");
JPanel cardOne = new JPanel();
JPanel p1 = new JPanel();
JPanel p2 = new JPanel();
JPanel p3 = new JPanel();
cardOne.setLayout(new BorderLayout());
p1.setLayout(new GridLayout(2,1,3,6));
JTextArea textArea1 = new JTextArea(defaultEntry);
JLabel l1 = new JLabel("Welcome to the Dream Journal! :)");
JLabel l2 = new JLabel("Type your dream below:");
p1.add(l1);
p1.add(l2);
p2.add(textArea1);
p3.setLayout(new FlowLayout(FlowLayout.CENTER));
JButton ok = new JButton("Save");
ok.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent ae) {
System.out.println("Do " + ae.getActionCommand());
}
});
erase = new JButton(new EraseAction());
p3.add(erase);
p3.add(ok);
// Use the constants
cardOne.add(BorderLayout.PAGE_START,p1);
cardOne.add(BorderLayout.CENTER,p2);
cardOne.add(BorderLayout.PAGE_END,p3);
frame.add(cardOne);
frame.pack();
frame.setTitle(TITLE);
frame.setSize(WIDTH, HEIGHT);
frame.setResizable(false);
frame.setLocationByPlatform(true);
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
System.out.println(textArea1.getText());
}
}
class EraseAction extends AbstractAction {
EraseAction() {
super("Erase");
}
#Override
public void actionPerformed(ActionEvent arg0) {
System.out.println("Do " + arg0.getActionCommand());
}
}
First let me explain you the Funda of Event Handler....
- First of all there are Event Source, when any action take place on the Event Source, an Event Object is thrown to the call back method.
- Call Back method is the method inside the Listener (Interface) which is needed to be implemented by the Class that implements this Listener.
- The statements inside this call back method will dictate whats needed to be done, when the action is done on the Event Source.
Eg:
Assume
Event Source - Button
When Clicked - Event object is thrown at the call back method
Call back method - actionPerformed(ActionEvent e) inside ActionListener.
Now your case :
Now this can be done in 2 ways.....
1. Let you Display class implements the ActionListener, then Register the button with
the ActionListener, and finally implement the abstract method actionPerformed() of ActionListener.
Eg:
public class Display extends Canvas implements ActionListener{
public Display(){
// Your code....
setComponent(); // Initializing the state of Components
}
public void setComponent(){
// Your code.........
Button b = new Button("Click");
b.addActionListener(this); // Registering the button.
// Your code..........
}
public void actionPerformed(ActionEvent event) {
// Do here whatever you want on the Button Click
}
}
2. Use Anonymous class.
- Anonymous class are declared and initialized simultaneously.
- Anonymous class must implement or extend to only one interface or class resp.
Your Display class will NOT implement ActionListener here....
public class Display extends Canvas {
public Display(){
// Your code....
setComponent(); // Initializing the state of Components
}
public void setComponent(){
// Your code.........
Button b = new Button("Click");
// Registering the button and Implementing it
b.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent event) {
// Do here whatever you want on the Button Click
}
});
// Your code..........
}
}
You need to implement ActionListner :
public class Display extends Canvas implements ActionListener
and add yourself to your button as such:
erase.addActionListener(this);
and then implement the required method:
public void actionPerformed(ActionEvent event) {
//do stuff
}
For more info, check out this tutorial on creating ActionListeners.
You'll find that this observable pattern is widely used the in Java GUI.
A couple high level critiques:
You are using many older AWT components (ie Button) when there are similar, but newer (read: more flexible) Swing components available (ie JButton). Take a look at this for a quick explanation on the difference.
The event model that you have implemented was revamped in 1997 to the observable pattern that I suggested above. If you would like to learn more, you can read this.

Java button do the same thing, how do I change this?

Alright, I have a simple java applet with two buttons and a screen. Both the buttons do the same thing. I want to change this. I can't find what it is that changes the action that is performed when either one of the buttons is pressed. They both to the same thing and I don't want this. So my question is how would I change the Inventory button to display "Hello world" instead of a line count?
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
public class projectApplet extends JApplet implements ActionListener
{
/**
*
*/
private static final long serialVersionUID = 1L;
private JTextArea textArea;
private int lineNumber = 0; // this is just to test
public void init() {
JPanel panel = new JPanel();
textArea = new JTextArea();
textArea.setBackground(Color.BLACK);
textArea.setForeground(Color.WHITE);
JScrollPane sp = new JScrollPane(textArea);
panel.add(sp);
Container window = getContentPane();
window.setLayout(new BorderLayout());
window.add(sp,BorderLayout.CENTER);
// this is just to test------------------
JButton b = new JButton("Clik to add a line");
b.addActionListener(this);
window.add(b, BorderLayout.SOUTH);
JButton inventory = new JButton("Inventory");
inventory.addActionListener(this);
window.add(inventory, BorderLayout.NORTH);
//---------------------------------------
}
public void actionPerformed(ActionEvent arg0) {
lineNumber++;
textArea.append("\nLine number: " + lineNumber);
}
public void actionPerformed1(ActionEvent arg0) {
lineNumber++;
textArea.append("RPFL");
}
}
Add a new action listener to it. Typically you can use an anonymous inner class:
inventory.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent ae) {
textArea.append("Hello, world");
}
});
Just have the one actionPerformed method and then find out which button triggered it.
For example:
public void actionPerformed(ActionEvent arg0) {
if(arg0.getLabel()=="Inventory") // Do the following
if(arg0.getLabel()=="Click to add a new line") // Do the following
}
Note, getLabel() method is deprecated so you'll have to use another... can't remember off the top of my head which you should though... maybe getName(). But this is a simple way to test which button was clicked ;)
You can't do arg0.getSOurce() inside the action performed method to checkout which button has generated this event.

Categories

Resources