AWT Action Listener In Swing GUI?

Feb 22, 2014

I'm using Eclipse with the Window Builder Pro plugin to create a Java program. I noticed that when I had Eclipse create an action listener for a combobox in a Swing GUI it created an AWT listener.

Did I choose the wrong type of listener? I want my code to use the Swing components because I understand that they are more portable.

View Replies


ADVERTISEMENT

Swing/AWT/SWT :: JComboBox Listener - Bound Action?

Jul 14, 2014

I have three JComboBoxes. When the user selects an entry in the first JComboBox the entries in the second are set. For this I use an Action extends AbstractAction which is bound to the first JComboBox.

I have also bound an Action to the second JComboBox.

Problem: this also fires when the entries on the second JCombox are added which leads to a Nullpointer.

I need a Listener which only reacts to user input, and does not react when the model of the JComboBox is changed.

View Replies View Related

Swing/AWT/SWT :: Action Listener Does Not Work With Button Click

Feb 22, 2015

I have buttons created on a frame and then I register the listener from a controller in a controller-view relationship.

When I click the button on the face, the action never executes for some reason. I thought maybe there was a problem registering the listener but I can call the buttons doclick() method and it executes as it should. Perhaps I'm overlooking something really obvious here but I can't see it.

btnEight = new JButton( "8" );
// btnEight.addActionListener( controller );
btnEight.setBounds( 212, 90, 41, 23 );
frame.getContentPane().add( btnEight );

and then I add the listener

public void setController( ActionListener theController ) {
Component[] components = frame.getContentPane().getComponents();
for ( Component component : components ) {
if ( component instanceof JButton ) {
JButton button = ( JButton ) component;
button.addActionListener( theController );
}
}

again, when i click the button nothing happens. but if i add the following code

btnEight.doclick()

the actionPerformed invokes in theController as I intended.

You can see the entire project on GitHub so you can see the full context. The controller class contains the listener and the view class contains the buttons.

View Replies View Related

Timer Action Listener

Oct 5, 2014

Whenever i click start button it will just printout start to the jtextfield it wont start the time .

import java.awt.*;
import java.util.Date;
import javax.swing.*; 
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.event.*;
 
[code]...

View Replies View Related

JPanel - JButtons Not Using Action Listener

Apr 22, 2014

In my code 0,1,2 work fine an present my prompts an use my listener correctly but for some reason 3-6 is not an I don't know why? Here's my attached code.

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.*;
public class Guess extends JFrame

[Code] ....

I meant 3-5 not 6

View Replies View Related

Split Panel In Two Halves And Add Action Listener To It

Mar 5, 2014

Which contains a tabbed pane and in the sub panel it has two sub panels

1. new user
2.list if i click a button in panel ---

one new user --- i need to get call a function which is of other class so that it displays the list in the right panel..

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
public class Test extends JFrame{

[code]...

View Replies View Related

How To Call Action Listener Automatically And Without Doing It Manually

May 16, 2014

I'm not a java developer, i'm a tester. I am currently testing a java swing application and to do that I have to automate how its used. IE I have to write code which will press buttons for me rather than depending on an end user to do this. I have managed to reverse engineer the entire application (hooray for me), however I am struggling to work out how to invoke methods that would typically be kicked off by a user pressing a button. how to I can call actionPerformed(ActionEvent ae) method which sits in the ATMMainPanel class?

I will be calling it from inside another method which is the equivalent of the main() method.

Java Code:

public class ATMMainPanel extends JPanel implements ActionListener {
[declarations here]
//here - User is pressing the Enter button after putting in pin.
public void actionPerformed(ActionEvent ae) {
[code performed when button is pressed]
} mh_sh_highlight_all('java');

View Replies View Related

I/O / Streams :: 3D Project - How To Activate Key Repeating With Action Listener

Dec 8, 2014

I am working on a 3D-Projekt, but that's not the point. I wanted to call a function to turn the camera and want, that the function ist called while the key is pressed. I tried following:

private ActionListener actionListener = new ActionListener() {
public void onAction(String name, boolean pressed, float tpf)
if (name.equals("Camera Left") && pressed) {
setViewAngle(-1);

I think, I have to change the ActionListener "onAction", but I can't remember in which way. In the actual code the programm only once perform the action when the key is pressed.

I already tried :

while(pressed) {setViewAngle(-1)}

But that didn't work, but get the whole programm to halt.

So I thought of key repeating.

View Replies View Related

Involving Action Listener And If Statements For Multiple Conditions

Jan 25, 2014

I'm writing a program for exemplary reasons that is basically two text fields, one for a username, and one for a password. the way its written is asking testing for what is inputed into the text fields if its equal to a "valid" username and password. if it is, a variable representing that is set equal to 1. there is another if statement asking if the actionevent.getsource is equal to the name of the textfield. the issue I had was that when I ran the program, it seemed to only test for the first condition, so I got the same result from the program regardless of what I input into the text field.

The Code

Main class:
import javax.swing.JFrame;
public class Alpha {
public static void main(String args[]){
log l = new log();
l.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
l.setSize(360, 240);
l.setVisible(true);

[Code] .....

The code originaly was written "eve.getSource() ==" but I changed that to see if it would resolve the issue with no avail. With it written this way, the program does nothing upon entering the text and pressing enter. Currently, there is nothing written to change the variable "val" to 1 to show that the username is valid. I did this because I can't figure out how I would go about doing this.

View Replies View Related

How To Add Action Listener To Code To Make Calculator Work

Mar 7, 2014

here is the code i already have made;

import java.awt.*;
import javax.swing.*;
public class EX_7_2 extends JFrame
{
public EX_7_2()
{
setLayout(new BorderLayout(5, 10));

[Code] ....

View Replies View Related

IOexception Expected Error Action Listener Method

May 9, 2015

Need to write two files but getting an expected exception error.
 
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.util.Scanner;
import java.io.FileWriter;
public class TestingPanel extends JPanel

[Code] .... 
 
TestingPanel.java:49: error: unreported exception IOException; must be caught or declared to be thrown
FileWriter outputFileQuestions = new FileWriter("Test.txt");
^
TestingPanel.java:50: error: unreported exception IOException; must be caught or declared to be thrown
FileWriter outputFileAnswers = new FileWriter("Answers.txt");

[Code] ....

View Replies View Related

Swing/AWT/SWT :: Multiple Action Listeners?

Oct 25, 2014

So im making a calculator just basic 4 function with a clear button. as i am working into this project i am running into an issue of assigning which click sets what. For instance i click the two button how do i tell it to assign it to firstNum, versus secondNum?

private class theHandler implements ActionListener{
public void actionPerformed(ActionEvent event){
if(event.getSource()==one){
firstNum=1;
}
if(event.getSource()==two){
secondNum =2;
}
if(event.getSource()==three){
secondNum =3;
}

So this for instance allows me to set one to the firstNum and then add it to two or three but im not sure what to do next. I have in my mind i want to do something like make three listener classes, that operate in sequential order like you pick the first numbers, click an operation, and then select the second numbers then hit the operation(equal) button to display answer. Am i on the right track or what?

View Replies View Related

Swing/AWT/SWT :: Key Listener On Jbutton

May 25, 2014

I have a Jbutton in my application named as SUBMIT,on clicking SUBMIT the text of jButton changes to ABORT. I wish to apply enter key listener on both the SUBMIT and ABORT button in my applcation, however I am not able to get the text of the button in key listener,so that i can apply my code based on the current text of Button.

View Replies View Related

Swing/AWT/SWT :: How To Change Action After 1st JButton Click

Aug 3, 2014

I am relatively new to java. . I cannot...(actually do not know) how to change the actionlistener (actionPerformed) after the second click in an array of JButtons. This is how the program performs right now.

On the 1st click on the grid, the border of the button clicked will change to 'blue' and another button will change its boarder to 'red'. on the next click the same action is performed.

What I need is to change action when the 'red' bordered button is clicked, lets say change the color of the buttons from the 'red' to the 'blue' buttons.(x 3 buttons).

The logic about how to perform the final result I think I can do on my own but my problem is how to change the action when the red bordered button is clicked.

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.BorderFactory;

[Code] .....

View Replies View Related

Swing/AWT/SWT :: Action Listeners - Pizza Ordering GUI

Dec 3, 2014

I've got the layout put correctly but I can't seem to get my action listeners to work correctly.

import java.awt.event.*;
import javax.swing.*;
import java.awt.*;
public class PizzaOrderDriver
{
public static void main(String[] args)
{
JCheckBox show1;

[Code] .....

View Replies View Related

Swing/AWT/SWT :: Implementing Listener Of GUI In A Different Class?

Jun 26, 2014

I have two different classes called Login (in the package View) and RegButtonListener (in the package Controller). I want to use the MVC pattern.

Login creates a GUI for login. I used NetBeans GUI Builder to create it.

In the class RegButtonListener there is the code for the click of the buttons, then various listeners of the GUI.

Now I want to close the login frame at the click of a button so I want to use the dispose() method.

If I had all the code (GUI code and code of the listener) in the same file, close the frame would be easy enough to do because this.dispose(). But in my case I have two different classes.

I also have a second problem.

In Login class (in the packege View) I have included the line :

regButton.addActionListener(Controller.RegButtonListener).

However, Netbeans tells me an error "package regButton does not exist. <identifier> expected". Why does he consider regButton a package? How can I fix these two problems?

How do I do then?
 
Below is the code of the two classes.

Login class.
public class Login extends javax.swing.JFrame {
/** Creates new form Login */
public Login() {
initComponents();

[code]....

View Replies View Related

Swing/AWT/SWT :: Double Listener With Inner Classes

Feb 19, 2014

In the book we made a GUI which has 2 buttons, a label and a panel (the panel is a subclass of JPanel). What the program does is the following: When I press one of the two buttons, in the panel there is a rectangle that changes its color. When I press the other button it has to change the text on the label. Now the problem is that when I start the program, the FIRST time I press the button on which the label must change, this also changes the color in the rectangle, which it should not (I also noticed that when i FIRST click the rectangle shifts a little bit to the left). After that the program works fine.

Here is my code:

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class TwoButtons{
JFrame fr;
JButton labelButton;
JButton colorButton;
JLabel label;

[Code] ....

View Replies View Related

Servlets :: There Is No Action Mapped For Namespace And Action Name Associated

Nov 16, 2014

This is my first servlet program. I wanted to try a web application where "register" user module will be in servlet program.I can access my index.jsp but when I enter values and click submit.

I get "There is no Action mapped for namespace [/] and action name [RegisterUserServlet] associated with context path [/TrainingApplication]. - [unknown location]".

Here's my index.jsp file:

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<%@ taglib prefix="s" uri="/struts-tags"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>

[code]....

I am using apache-tomcat-8.0.14 server.

View Replies View Related

Swing/AWT/SWT :: How To Pass On Object To Event Listener

Sep 22, 2014

I’m teaching myself Java. I am a fairly proficient programmer in other languages, but this is the first OO language I’m doing.I have a question that is a bit hard to summarize. Or it should be: how can I pass on an object (or variable) to an event listener?

I am writing an application in which you can play a Sudoku game. I have separated the “logic” or the “model”(the classes with Sudoku data structures and methods to manipulate them) from the presentation (the view and the controller).The main method starts off as follows:

SudokuModel model = new SudokuModel();
SudokuView viewController = new SudokuViewController(model);

The first line creates class for the logic and the second line creates the class for the view and the controller. Since the view and the controller need access to the business logic, the model is passed on to the ViewController class.The SudokuViewController class creates the user interface in Swing and it handles the user input. For the user input I have created a number of listeners, like this:

table.addKeyListener(this);

Now these listeners need access to the model since they update it. However, as far as I’m aware the only parameter passed on to an event listener is the event itself. So these event listeners do not have direct access to the model, even though it is passed on to the constructor of the class SudokuViewController.

To circumvent this, I made model2 an attribute (variable) of the class SudokuViewController. The constructor of the class sets this variable as follows:

model2 = model;

Now the event listeners have access to model2, which they can manipulate.This works. However, I think it is an ugly solution, introducing an additional object (model2). I’d like to pass on the object named model to the event listener, but this doesn’t seem to be possible.

View Replies View Related

Swing/AWT/SWT :: Instantiation Of Listener Using Anonymous Inner Class

Jun 2, 2014

JMenuItem blueChoice = new JMenuItem("Blue");
blueChoice.addActionListener(new InnerMenuListener(){
public void actionPerformed(ActionEvent e){
String buttonCommandString = e.getActionCommand( );
if (buttonCommandString.equals("Red"))
redPanel.setBackground(Color.RED);
else if (buttonCommandString.equals("White"))
whitePanel.setBackground(Color.WHITE);
else if (buttonCommandString.equals("Blue"))
bluePanel.setBackground(Color.BLUE);
else
System.out.println("Unexpected error.");
}
});

Can a method in an anonymous inner class ever be private? why or why not?

View Replies View Related

Swing/AWT/SWT :: Void Is Invalid Type For Variable Action-performed

May 1, 2015

Here is part of my program that contains the code giving me problems.

import java.awt.EventQueue;
import javax.swing.JFrame;
import java.awt.Label;
import java.awt.BorderLayout;
import java.awt.Button;
import java.awt.TextArea;
import java.awt.Choice;
import javax.swing.JRadioButton;

[code]....

View Replies View Related

Swing/AWT/SWT :: Adding Items To JComboBox Taken From JTextField Listener

Nov 11, 2014

I need to take the value of what's typed in my JTextField, and once the submit button is clicked, I want to add that string to my JComboBox list items.

Depending which RadioButton they selected, I need to put the team in HOME or AWAY teams, and so on and so on, until they continue clicking submit.

View Replies View Related

Swing/AWT/SWT :: JSpinner - Adding Listener To Arrow Buttons

Jan 25, 2007

I need to change the value only when I press one of the arrows in a JSpinner component.How can I add a listener to the arrow buttons. I tried ChangeListener but it was not what I needed

View Replies View Related

JavaFX 2.0 :: TreeView - Swing Tree Will Expand Listener?

Jun 3, 2014

Is there some equivalent of the Swing TreeWillExpand-listener for TreeView?
 
In my particular case I want to check whether some constrains are set to show some children before they will populated in the view.

View Replies View Related

Swing/AWT/SWT :: Listener Called Multiple Times On Single Click?

Jan 30, 2015

I have the following listener on a tableset of names:

ListSelectionListener singleListener=new ListSelectionListener() {
public void valueChanged(ListSelectionEvent e) {
int row=e.getFirstIndex();
if (row==previousRow) {
row=-1;

[Code] .....

The println shows that getValueIsAdjusting is called 4 times when I click a single selection from the list. Is this normal behavior? If so how do I determine which call to really use. If not, where can I look to see why it is being called 4 times on a single click?

View Replies View Related

Swing/AWT/SWT :: Possible To Stop And Start A Listener Alternatively In Java Frames?

Dec 3, 2014

I was developed a simple GUI, in that it requires modification of jtextfield content while JComboBox item selected and vice-versa. I was used itemListener on JComboBox and Document Listener on JTextField. It was gives exception while running the code. Because one listener source effected by another one..

Exceptions are like this:

Exception in thread "AWT-EventQueue-0" java.lang.IllegalStateException: Attempt to mutate in notification
at javax.swing.text.AbstractDocument.writeLock(Unknown Source)
at javax.swing.text.AbstractDocument.replace(Unknown Source)
at javax.swing.text.JTextComponent.setText(Unknown Source)
at ronanki.swing.pcahostsimUI$5.itemStateChanged(pcahostsimUI.java:368)
at javax.swing.JComboBox.fireItemStateChanged(Unknown Source)

[Code] ......

View Replies View Related







Copyrights 2005-15 www.BigResource.com, All rights reserved