FAQ Database Discussion Community


Buttons and paint method

java,swing,graphics,jbutton,actionlistener
I'm having trouble with my buttons. I know they're working because I've tested them out by exiting the problem through System.exit. Here is what my output looks like: http://imgur.com/Ks7mIFa When I click the close button, the handle on the switch should redraw to the other side and the close button...

Create boolean to show true when correct JRadioButtons have been selected

java,actionlistener,jradiobutton
previous related question: Check which jRadioButtons have been selected if they have been created in a loop I now want to create a boolean, that when all correct JRadioButtons are selected from each row, this boolean will be true. To clarify, it can only be true when all radio buttons...

ActionListener and for loop

java,swing,loops,for-loop,actionlistener
Dear friends of Stackoverflow, here what I'm trying to do: - I would like to have a simple frame with some buttons and a JTextArea - I would like to have a loop that, at every iteration, expects for me to click on a button: when I click on this...

Have an ActionListener wait for a click event to occur

java,swing,actionlistener
I have a Button with an attached ActionListener. I need to click the button but then have it wait for another click event to occur in order to complete actoionPerformed(). this click could happen anywhere on the gui. How can I go about doing this? UPDATE: There are two areas...

Calling a Java AbstractAction from a button/mouse release

java,swing,jbutton,actionlistener,abstract-action
In a simple calculator app, I use abstract actions to handle the buttons being clicked with the mouse and the respective number pad keys (with key bindings) being pressed. I wanted certain cosmetic changes to occur upon using the keyboard, such as changing the border of JButton number 1 when...

Remove elements from pane when timer reaches 0?

java,timer,actionlistener,pane
I am trying to remove elements from a Pane when the timer reaches zero, more specifically a label and a text area. However, when the timer has reached 0 and I call this method I get this exception. Exception in thread "AWT-EventQueue-0" java.lang.IllegalStateException: Not on FX application thread; currentThread =...

Why aren't the action listeners working

java,swing,jbutton,actionlistener
The ActionListeners are set up properly but for some reason are not working when I press one of the buttons. Any ideas? I have searched for ways to do it but nothing seems to work. Here is my code: import java.awt.*; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.*; public class Master...

Camera listener service?

android,camera,actionlistener
I am looking for a way to run a service process in the background of a device, that listens for camera actions. Is this possible? I have been reading a lot of forums that are around launching the native camera from within an activity, and I can already do this....

Exiting out of or quitting an actionListener

java,actionlistener
I'm creating the generic Battleships game in java and am currently making the GUI, I have set the GUI so that the user is entering the locations into a JTextField and then clicking a Button which does Label.getText() my code is as follows. int state = 0; if (state ==...

Add new label and textfield on button click Java

java,swing,jbutton,actionlistener,jlabel
I have this code with three buttons. I want to add a label and a textfield after a buttonclick. So for the 'button' I want to add the 'label' and 'textfield. For 'button2' I want to add 'label2' and 'textfield2'. And so on. I already have this code, but it...

How to use ActionListener in ComboBox

java,button,actionlistener
package mainpanel; import java.awt.FlowLayout; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.io.File; import java.io.FileNotFoundException; import java.util.*; import javax.swing.*; public class MainPanel extends JFrame implements ActionListener { String[] deck = null; String[] discard = null; Player p1; Player p2; Player p3; public String[] playerList = new String[3]; JPanel panes = new JPanel();...

How to write a KeyListener for JavaFX

javafx,java-8,actionlistener,keylistener
I want to write a little game where I can move a ball on a JavaFX Panel using the W, A, S, D keys. I have a getPosX() and setPosX() but I don't know how to write a KeyListener which will e.g. calculate setPosX(getPosX()+1) if I press D. What do...

ActionListener not responding?

java,swing,actionlistener
I am extremely new to Java code, however, I cannot find the answer to my problem anywhere else. I have a JFrame with 11 JButtons, each of which has called the .addActionListener(this); method. The first three and the last one respond like they are supposed to, however, JButtons buy4-buy10 do...

Actionlistener outputing Jlist info to textarea

java,swing,actionlistener,jlist,thread-sleep
Could someone help me with the following problem? I want to implement a actionlistener that outputs info about the highlighted Jlist object into the already present textarea. Here is the code i've managed to cook up: import java.awt.*; import java.awt.event.*; import javax.swing.*; public class ListModel extends JFrame { private JList...

Highlight text in JComboBox then erase it if user input something

java,swing,text,actionlistener,jcombobox
I'm trying to make a 'JComboBox' that can take user input, but that's not the problem. I want that if the 'JComboBox' loose the focus then regain it the text is highlighted. Then if the user input anything it erase the text in place and replace it by the new...

Fetch data from textfield and display it in another textfield

java,swing,actionlistener
//combo box actionperformed method. private void CmbActionPerformed(java.awt.event.ActionEvent evt) { //created aray of objects JTextField t[]=new JTextField[8]; String num=null,s1; int num1=0; num=Cmb.getSelectedItem().toString(); num1=Integer.parseInt(num); //applied a logic to create same no. of textfields that selected in combo box. while(num1!=0){ for(int i=0;i<num1;i++) { t[i]=new JTextField(10); jPanel2.add(t[i]); b1.setText("the objects has created"); jPanel2.revalidate(); validate(); num1--;...

How to pass an object argument to a method called in actionPerformed?

java,actionlistener
I am writing a stock control system program for a school project. This is the last thing I need to do, but seeing as I am a relative Java noob, I kindly request your assistance. I have a DisplayRecord class, which is created by taking String input from a "search"...

How to invoke button click without explicit click on that button

java,events,actionlistener,sphinx4
I am developing an application in JAVA that takes input as speech and performs actions. What I want to achieve is button click without explicitly clicking on that button. The idea is to take the speech input, use the result which is a String object, and invoke button click just...

actionPerformed confusion on button events

java,swing,actionlistener
I am trying to make a game in java, when a button is pressed I want it to change icon to indicate that button has been selected, then when it is clicked again I want to change it to its original icon to show it has been deselected. public void...

Coding the Next and Previous button

java,io,actionlistener,next
I want to code for the next and previous buttons. I have made the action listener but do not know how to code for next and previous buttons. My code is as follows: public class panel extends OrderSystem { final JTextField items; final JTextField number; final JTextField cost; final JTextField...

getText for JTextField input after action event

java,swing,actionlistener,jtextfield
I am a complete newb to Java and I don't quite understand why I am not able to getText() from the JTextFields I have created. I want to be able to create a new User with the email and password that has been input, but I am getting a nullPointerException...

try/catch block not working in actionlistener

java,user-interface,try-catch,actionlistener,jtextfield
I am working on 2 programs for school, one to take contact info and save it to a text file and the other is to read from the text file. All works perfectly but I have to add a try/catch block the program that takes input so that it would...

Incrementing value of an integer in JTextArea

java,swing,jbutton,actionlistener,jtextarea
I have the following code in which I have created a JTextArea and Menubar in which I have added menuItem Inc. I have coded the value of "val" = 0 , so that when the code runs and frame appears, the first thing that is seen is zero. But i...

AbortProcessingException leaves stack trace in server log, how to disable this?

jsf,exception,logging,jsf-2,actionlistener
There is a commandButton on a page that asynchronously invokes some service, and when user clicks a button I want to verify service availability by checking special config from database. This config can be updated every minute. So if the service is not available, action of commandButton should not be...

How to specify an int from another class

java,actionlistener
This is going to be a embarrassing question but I'm new to Java. I have a class that implements ActionListener with this code in it: public class Shop1 implements ActionListener{ public void actionPerformed(ActionEvent event){ int alis = 0; alis++; System.out.println(alis); } } Everytime I press the button it shows 1....

How is action on JButton being invoked in this code?

java,swing,jbutton,actionlistener,windowlistener
I have problem understanding how is actionListener used in the following code and what is the addWindowListener method doing in the code below: kindly help me with it . public class SwingListenerDemo { private JFrame mainFrame; private JLabel statusLabel; public SwingListenerDemo(){ prepareGUI(); } public static void main(String[] args){ SwingListenerDemo swingListenerDemo...

Figure drawn without function call?

java,swing,actionlistener
I am getting a problem in java program for moving a circle. The program has 2 basic buttons: START: used for moving the circle STOP: used for exiting the program My problem is that the circle appears before I press the start button. However I make the call to move...

ActionListener of JComboBox and initialize JPanel

java,swing,compiler-errors,actionlistener,jcombobox
I want to write program in which you can chance scene (JPanel) thanks to JComboBox. I used ActionListener, but it doesn't work. At the beginning of constructor I defined panel as final, but it didn't help. scene.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { String choice = String.valueOf(scene.getSelectedItem()); if(choice=="Sceneria"||choice=="Scene"){ slider.setEnabled(false);...

Passing an Application Class Reference to Constructor (Java)

java,javascript-events,parameter-passing,actionlistener
I have an application class, as shown directly below, which instantiates objects from i) a UI class I wrote that extends JFrame and implements ActionListener, and ii) a simple "Invoice" class. The former will be used as a main interface that accepts text entry, for particular values ("invoice number"), and...

Detect clicks on button array with a single method

arrays,methods,jbutton,actionlistener
Im working in a school project, a minesweeper. Will be 20x20, so it has 400 buttons. Its there a way to add an actionEvent/actionPerformed and implement a generalized method for the whole array? Or there is an easier way?

Looking for a simple way to change text in a JTextField with a button click

java,swing,button,actionlistener
I'm trying to get the action listener and button called French to change the text in the Output field to say Bonjour but many errors come up in the console when I click the button. I would like to do this in as few lines as possible. import java.awt.BorderLayout; import...

JFrame does not draw content when called inside ActionListener

java,swing,actionlistener,event-dispatch-thread
I am trying to make a set of 2 GUIs: one, when a button is clicked, calls another, which, based on which button is clicked in the second GUI, returns a value to the first GUI. Unfortunately, when called from the first GUI's actionPerformed method, the second GUI appears blank....

Why isn't my ActionListener working for my buttons?

java,css,swing,button,actionlistener
I'm having some trouble getting anything to happen when I click a button. This is where I declare my buttons public class DoND extends JFrame implements ActionListener { public JButton btnsuit1, btnsuit2, btnsuit3, btnsuit4, btnsuit5, btnsuit6, btnsuit7, btnsuit8, btnsuit9, btnsuit10, btnsuit11, btnsuit12, btnsuit13, btnsuit14, btnsuit15, btnsuit16, btnsuit17, btnsuit18, btnsuit19, btnsuit20,...

Use non-local variables in actionlistener

java,icons,actionlistener
I want use a variable in action listener of it . this is my code : public class Face { public Face(){ Btn1 = new JToggleButton(); Btn2 = new JToggleButton(); Btn3 = new JToggleButton(); Btn4 = new JToggleButton(); Btn5 = new JToggleButton(); Btn6 = new JToggleButton(); } public Start6(){ JToggleButton[]...

GUI Animation: Slider Value between Action and Change classes?

java,swing,user-interface,actionlistener,jslider
My GUI frame comes up now but the slider value (speed) I get from my slider doesn't appear in my ActionLlistener class where I need use it as a delay in my timer. How do I bring that value over? The point is to run 12 images, like frames, at...

JFrame error actionperformed

java,swing,jframe,actionlistener,visibility
I want to set the visibility of frame 2 to false, but whenever I do that, I get an error. I read something about this and it said that I needed to make my method I private method, but I can't do that since my method is actionPerformed. Somebody got...

Focus on the button using tab key and press enter

java,swing,netbeans,jbutton,actionlistener
I have a simple jFrame with a submit button and a cancel button. Currently, everything works perfectly. User perform action on mouse click. Now I want to press 'tab' first to focus the button then it should fire whenever someone presses the enter key. It should work for both keys....

Listener using JFrame in Java

java,swing,actionlistener
I'm learning right now the basics of the ActionListeners and I've been searching for help over here but can't quite find/figure what I'm doing wrong. I've got a class (Client) which implements the main call: ... public static void main(String[] args) { Myframe test = new Myframe(); N = test.setVisible();...

Calling startTimer Method Multiple Times(Java)

java,timer,actionlistener
So I've been browsing the web for answers but feel overwhelmed and can't seem to find an explanation of this. For one of my classes we were given an example with code very similar to what I'm using and I copied it (dangerous I know but not sure how else...

Same ActionListener for Different Buttons - Best Practices

java,swing,actionlistener
The code works, but I wanted to know whether this was the best way/most recommended way of adding an actionListener when the buttons are performing the same task on different files. I've read the debate between making a class implement the ActionLister interface vs. adding an anonymous actionListener class to...

How to return variable from within an if statement for use in a separate method?

java,swing,methods,actionlistener
public int randomNumber() { return (int) (Math.random() * (11 - 1) + 1); } public void actionPerformed(ActionEvent e) { if(e.getSource() == question) { int one = randomNumber(); int two = randomNumber(); int three = randomNumber(); int four = randomNumber(); label.setText("Is " + one + "+" + two + " greater...

How to stop an actionListener from executing its code if the user chooses no on a confirm dialog box?

java,swing,actionlistener
I have an actionListener set up on a button. If the user clicks it it adds 10 to a progress bar. I want to allow the user to cancel their button press and prevent the code within the actionListener from executing. I.e. i want my program to just disregard the...

How to use swing timer?

java,swing,timer,actionlistener
I'm trying to write a program that draws a new square every second. This is my code for my JPannel class. I have two other classes but I beleive they are irrelivant to my question. One has the main method where it creates an object of the other class that...

Unable to add text to JTextArea in Java from another function

java,function,static,actionlistener,jtextarea
I made a simple program in Java that contains only one text area and a button. The button is suppose to add a "text". However, it doesn't work for me. On a side note: I'm trying to keep my functions as short as possible. (I don't want a function with...

Using JRadioButton to display images

java,swing,actionlistener
I need help with a small little problem. I'm not sure how to display an image properly after clicking a JRadioButton. Nothing shows up after I click any button and idk where to begin. Any help is appreciated. public class DisplayImage extends JFrame{ private JRadioButton pic1Button, pic2Button; private ImageIcon pic1Image,pic2Image;...

Passing “this” as a Method Argument - Clarification

java,swing,methods,this,actionlistener
I am currently working on Java Swing lessons after finishing my first set of lessons in Java. In this lesson, we are working on communication between different components (buttons,toolbars,etc.) that we have been studying. The problem is, "this" is being passed as a method argument for the addActionListener() method. This...

ActionListener Not Detecting Buttons?

javascript,actionlistener
The code looks pretty long, but I just don't know how to get ActionListener working for discard button and draw button. I read some tutorials, but I still don't really get it. I think you use getSource() or something? Thanks! package mainpanel; import java.awt.*; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.io.*;...

How to launch a second JFrame from clicking a JButton?

java,swing,jbutton,actionlistener
I'm making a To-Do List application and I have a PrimaryList frame and a SubList frame. When a user selects something from the PrimaryList (Grocery...or something like that) and then hits a forward arrow JButton, it is supposed to launch up the SubList frame. Now here is what I have...

Issue about JComboBox with actionlistner

java,swing,actionlistener,jcombobox
For the below example, I am writing a JComboBox with data 1,3,5,7,9, and expecting it will change to 2,4,6,8,10 after OK is pressed. However it just doesn't work..... Any advice would be appreciated, thanks. public class Test extends JFrame{ Test (){ final ArrayList<Integer> value = new ArrayList<>(); value.add(1); value.add(3); value.add(5);...

I want to update table when Button is clicked

java,swing,jtable,actionlistener,defaulttablemodel
I am newbie of Swing. I want to update table after click button (done button). I think data correct but the screen does not work. The followings are explanation of my program check checkBoxes and the click Done button the bottom layer should change. there is no main This is...

Java import wildcard not importing everything inside package

java,import,awt,actionlistener,wildcard
I'm writing a Java program that has a button with an action listener: JButton button = new JButton("Change"); button.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { label.setText(textField.getText()); } }); At the top of the file, I have these two import statements: import java.awt.*; import javax.swing.*; I know this is...

Why isn't my actionListener working?

java,swing,jbutton,actionlistener
I am trying to make a button show up if I press the hit button, if z==21, but the button I'm trying to make show up is not showing up. This is for a BlackJack game, but I pulled a little bit of code out of it, the part that...

ActionListener for one of the Jbuttons doesn't get called

java,swing,jbutton,actionlistener
import java.awt.BorderLayout; import java.awt.Color; import java.awt.GridLayout; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.awt.event.ItemEvent; import java.awt.event.ItemListener; import javax.swing.ButtonGroup; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JOptionPane; import javax.swing.JPanel; import javax.swing.JRadioButton; import javax.swing.JTextField; @SuppressWarnings("serial") public class Gui extends JFrame { private ExpPanel...

JButton not performing it's Required Function in java

java,jbutton,actionlistener
Here's My Code of Currency Converter Program . Earlier Null Pointer Exception Occurred , but it was Rectified . Now there are No Errors in the Program . Program Runs but Convert Button Does not Perform Any Operation . I believe Problem Lies in the ActionPerformed Function , But I'm...

Java Swing Timer infinite loop

java,swing,timer,actionlistener,infinite-loop
I'm having an issue with a simple Java swing Timer I set. It does everything fine up until having to stop. I think it is creating an infinite loop of timers upon timers but I've been following multiple tutorials and I can't see where I have gone wrong. Code explanation:...

Java GUI - JButton opens another frame from another class

java,swing,jframe,jbutton,actionlistener
i'm having trouble to get the 2nd frame to display the GUI Components. i've opened the frame using the JButton from the 1st frame. Here's the screen shot This is the first frame - ClientModule.java 2nd frame - ClientMenu.java Here's the codes i've tried ClientModule.java import java.net.*; import java.util.*; import...

ActionListener principles

java,swing,compiler-errors,actionlistener
I am trying to find basic principles of adding action to JButton or other components. Here is what I am doing and what am I getting. I have created a class named : Ali which has a main method in it, inside the main method Instantiate another method called: MainFrame...

Adding textfield and label on mouse click not working Java

java,swing,nullpointerexception,jbutton,actionlistener
I have this code: import java.awt.BorderLayout; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JPanel; import javax.swing.JTextField; public class Interface2 { private JLabel label; private JLabel label2; private JLabel label3; private JTextField textfield; private JTextField textfield2; private JTextField textfield3; private JButton button; private JButton button2; private JButton...

Java actionPerformed does not add the changed label until the GUI is resized

java,swing,label,actionlistener,jlabel
I am a high school student trying to learn java and any help or advice is appreciated. The code does not set the text after I press the button. The actionPerformed does not do what it is supposed to until I change the size of the GUI for some reason....

Check which jRadioButtons have been selected if they have been created in a loop

java,swing,actionlistener,jradiobutton
I have a loop that creates rows, where each row has another loop to create 6 JRadioButtons. Only one JRadioButton can be selected from each row (as it should be), but I'm finding it difficult to use Action Listeners to check whether the correct JRadioButton has been selected from each...

Error: Reference not found. Binding an ActionListener method to a button in

jsf,parameter-passing,actionlistener,oracle-adf
I am creating a program using Jdeveloper 12c. This program will use a user generated input of an address which is then marked on a map. The map used is the thematic map feature of Jdeveloper. The problem I am having is with the button's actionListener. It is supposed to...

Updating swing components within edt

java,multithreading,swing,actionlistener,event-dispatch-thread
If I understand correctly then when I create GUI swing components, for example I have this: public class frameExample extends JFrame{ public frameExample(){ //Here adding bunch if components setVisible(true); } } So as long as I don't call the setVisible method the components are being made from the thread the...

java.lang.InstantiationException: my.package.CustomActionListener with non-default constructor

jsf,serialization,actionlistener,commandbutton
I'm try to generate a commandButton on primefaces 5.1, but I have an error: Here the xhtml file: <h:head> </h:head> <h:body styleClass="appDefaultBg"> <h:form> <p:remoteCommand autoRun="true" action="#{bean.pageRender()}" update=":pageElement" /> </h:form> <h:panelGroup id="pageElement"> </h:panelGroup> </h:body> Here the bean class: @ManagedBean(name = "bean") @SessionScoped public class Bean implements Serializable { private static final...

JButton text won't update until other operations are done

java,swing,jbutton,actionlistener
I have a JButton which performs an action when clicked. In the actionPerformed I want to update the button text before calling the NotifyObserver, which contains a big number of calculations. The problem is that the buttontext won't update until all the operations called by the NotifyObserver are done. Here's...

Changing label text using ActionListener

java,jbutton,actionlistener,jlabel,settext
I'm trying to make a program that allows me to change the text of a label when clicking different buttons, however regardless of what button I click the text of the label changes to the last line of actionPerformed. import java.awt.*; import java.awt.event.*; import javax.swing.*; public class Gui extends JFrame{...

Javascript - Too much click listener in app

javascript,jquery,events,actionlistener,onclicklistener
I have a general question about javascript (jQuery) events/listener. Is there any limit for the number of click listener without getting performance problems? Thank you for your help! I would appreciate every answer....

ActionListener for multi-dimensional array only works for first button?

java,arrays,swing,multidimensional-array,actionlistener
I am attempting to create a grid of 12x12 of JButtons, and then randomly assign ten of the buttons to have a "gold nugget" underneath, each time you run the program. When you click a button that has a nugget underneath, it should change the text of the button to...

How to handle addTextChangedListener on two EditText together?

android,layout,android-edittext,actionlistener
Reququirement : There are two EditText one is Username and another is Password. I am checking on both EditText has more than one character than button of Signup should enable. Can anyone pls suggest me ? etUsername.addTextChangedListener(new TextWatcher() { @Override public void onTextChanged(CharSequence s, int start, int before, int count)...

JButton does not change a field value

java,swing,jframe,jbutton,actionlistener
I am facing a strange problem with a simple JFrame application which has only one button in order to change a field value. When I try to debug my application, it works fine, but when I try to run it, it doesn't work and gets stuck in the while loop....

Have the selected radio button to display in text area

java,swing,actionlistener,jradiobutton
I'm making a program in netbeans that allows users to import their computer specs and I have 2 radio buttons and I want it when you would select the choice and when pressed display it would display your choice in the text area. I already have other text fields which...

Two JCheckbox sources?

java,swing,listener,actionlistener
I'm writing a small body weight program for my assignment. I have 2 JRadioButton for gender, and five JRadioButtons for the heightt categories. I added an ActionListener for each of these buttons. In the actionPerformed function, how can I put an an if() condition that lets me decide on the...

JButton listener not firing

java,swing,button,actionlistener
I seperated my codes into MVC model and now my confirm button action listener is not printing the username and password even though I added a Actionlistener for it. please help thanks. Codes LoginDialog.java import java.awt.BorderLayout; import java.awt.FlowLayout; import java.awt.GridBagConstraints; import java.awt.GridBagLayout; import java.awt.Insets; import java.awt.event.ActionListener; import javax.swing.JButton; import javax.swing.JDialog;...

Action Listener not working in a MVC application

java,model-view-controller,actionlistener
I have a following problem: I have a small application with a very simple UI. Implemented Action Listeners work there just fine. However, the Action Listener for the Dialog window and its Save button simply refuses to print out the test message. The code is rather lengthy so i put...