Swing/AWT/SWT :: JButton Actionlistener Not Working?
Mar 5, 2014
I was suspecting I had a jButton not working, so I added a console print to test. See below. When I click the button, the console doesn't display anything. Am I using the button wrong?
private void jButtonUpdateItemsMouseClicked(java.awt.event.MouseEvent evt) {
System.out.println("click");
System.out.println(drawPreviousRectangle);//this is a boolean
}
I have also tried the following with the same results (or lack of).
private void jButtonUpdateMajorDeckItemsActionPerformed(java.awt.event.ActionEvent evt) {
System.out.println("click");
System.out.println(drawPreviousRectangle);//this is a boolean
}
I am trying to implement user input in my dice rolling program but I do not understand where I need to add the actionListener. What I want the program to do is allow the user to click on each roll button and have the output be between 1 and 6. I figured I can put it in the "main" class due to the size of the program but I would like to know the best way to go about adding the actionListener into a sub-class for practice purposes.Here is my code thus far:
package com.jprogs; import java.awt.*; import javax.swing.*; public class DiceGenerator extends JFrame { DiceRollEvent dice = new DiceRollEvent(); // create the roll buttons JButton roll_1 = new JButton("Roll #1"); JButton roll_2 = new JButton("Roll #2"); JButton roll_3 = new JButton("Roll #3"); JButton roll_4 = new JButton("Roll #4"); JButton roll_5 = new JButton("Roll #5"); // create output field for each roll
I was working on implementing the game Hunting the Wumpus [URL] .... for one of my projects in College. I've got the major parts of the game already implemented. I am working on creating a start screen right now. But I've run into a strange problem. In my code the main method that draws is the PaintComponent method in the private class HTWPanel. I need the Actionlistener to listen to the "QWES" keys while the state of the game is PlayState.Playing so that the user can enter the direction he's going in. If I have the following code for my paint component, the Actionlistener works fine.
public void paintComponent(Graphics g) { super.paintComponent(g); map.draw(g, 100); }
But when I try to add a start screen (by changing the background color and adding a label called "StartScreen"), the controller only listens to keys while the start screen is showing, not while the game is playing. I'm pretty sure this problem is due to some internal implementation of KeyAdacpter or ActionListener, but I'm not sure what.
public void paintComponent(Graphics g) { super.paintComponent(g); if (state == PlayState.START) { this.setBackground(Color.orange); this.add(startScreen); } else if (state == PlayState.PLAYING) { if (this.getBackground() != Color.WHITE) {this.setBackground(Color.WHITE);} map.draw(g, 100); }
I attached a screenshot of my main class, HuntTheWumpus, and a screenshot of what my game looks like so far. Each square is a different room in the Wumpus's lair. The black rectangles are door representing which direction you can go to. The rooms that are 2 squares or less from the wumpus are colored red. The hunter is represented by the black circle in the upper left hand square. His he can move left right up and down into different rooms with the "QWES" keys.
is any simple way to apply the same code on both ActionListener and MouseListener, so that the same code is executed when the user either presses a JButton or presses Enter when a JTextField is focused.I know that I can define a method, while it is not preferred as I may use break; in my MouseListener.
A simple example:
import java.awt.*; import java.awt.event.*; import javax.swing.*; public class TwoListener extends JFrame {
[code]....
Although I can define a new method that prints out the three numbers, I have to add certain break; code in different cases. So I just wonder whether there is another way to do things like this.
ActionPerformed and ActionListener on Netbeans...If I create a ComboBox on Netbeans using GUI design, then I right click over the Combobox I can find Events - Action - ActionPerformed... but I can't find ActionListener...
Are ActionPerformed and ActionListener the same?
or...
where is ActionListener on GUI Netbeans? Do I need to manualy write the ActionListener part?
I'm trying to acces my variable "cost" so when i press a button it prints the cost(for now) but the int value just prints 0
public class Holiday extends JFrame { private RoutinePanel routine; // A panel for routine charge checkboxes private JPanel HolidayPanel; // A panel for non-routine charges
implementing a swings application. I have a JFrame containing 2 JPanels : Top & Bottom panel. Top panel has a Jcombobox, Botton panel contain a Graph plot. On changing value in combo box, graph plot should get updated. I thought of adding actionlistener to Jcombobox. Not sure what to implement in its actionPerformed()
Can I add actionListener to a button component without creating a reference to it? Look at my code below:
public void init() { setBackground(Color.red); //Create the layout setLayout(new BorderLayout(20, 5)); //Add buttons add("North", new Button("Red")); add("South", new Button("Yellow")); add("East", new Button("Cyan")); add("West", new Button("Magenta")); add("Center", new Button("White")); }
Or should I just do the usual instance.addActionListener(this) like myButton.addActionListener(this)?
I am trying to write an application where if you click and hold down on 1 button and move the mouse to another button and release the text on the 2 buttons should be switched around.I have a print line so I can se the text are supposed to get switched around but it is just not happening in the GUI.
I attached all my code in case the flaw was to find somewhere else in the code, but the functionality I am working on is the lines 76 to 121.
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.
I have a jButton that when the action perform runs it only does part of the tasks some of the code doe not appear to run and i just can not figure out why i have tried wrapping parts of it in an if statement and it still do not run so here is the code behind the button:
in the code the parts that do not run are jTextField10.setText("Working Please Wait"); and jButton15.setVisible(false);
what i am expecting to happen is the text field has the words Working Please Wait placed in it , this does not happen and the button should be hidden to prevent it being clicked a second time again this does not happen
the rest of the code works as expected
InputStream inStream; OutputStream outStream; try { File afile = new File("C://templates//amendments.xls"); File bfile = new File("C://amendments.xls"); inStream = new FileInputStream(afile); outStream = new FileOutputStream(bfile);
I'm not sure what I'm doing wrong but none of the buttons will show below. I tried using .add with the container name but it kept giving me an error. When I just put add.(); it seems fine but then nothing shows. Is there something I'm missing that's required?
Recently I have been working on an executable launcher. I tried to get the executable icons and set them on the JButtons. Still there are some problems. Here is a part of the code:
Here the getConfig() method is already defined to get a string in a "properties" file. However, the icons are very small and they don't fill the entire button.
How can the icon fill the entire button (for example, having a custom size of 48x48, or automatically filled the button)? The frame will not be resized.
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.
one of the java swing challenge I am facing. Problem statement: there are two JPanels panel 1 and panel 2 placed on a JFrame one below the other as panel 2 below panel 1. Now, I need to put a JButton in any one of the panels such that this JButton displays half in panel 1 and half in panel 2.
We are creating a quiz as a school program, we are using netbeans and we have a little problem. The problem is that our JButton opens a new JFrame in a new window and we want it to stay in the same window. The code we are using is:
private void PlayButtonActionPerformed(java.awt.event.ActionEve nt evt) { Question1 s = new Question1 (); s.setVisible(true); }
Is it possible to make it stay in the same window?
public class ComparativoV3{ public void loadComparative(String path){ } public void compareDbs(){
[Code].....
how to set it up the private void CompareActionPerformed(java.awt.event.ActionEvent evt) as the ComparativeV3 main class, not giving it any argument, because the files are load from the arguments section & it reads diferent files with diferent arguments.
I am trying to plot a graph and graph should display when JButton is clicked. To create data set, I am taking some value through JTextField and then created a chart and plotted it. I've got a problems: the chart doesn't refresh/update when I change the text field value.
public class Test2 extends JFrame { /** * */ private static final long serialVersionUID = 1L; private JPanel contentPane; private ChartPanel chartPanel; private JTextField textField_1; double a;
With the MigLayout for Swing, I'd like to see the JDialog looking like the standard windows Dialog,where to have the OK and cancel buttons at the lower right corner.
i just started java and I'm trying to change the JLabel text by clicking on the JButton however it isn't working,
import java.awt.BorderLayout; import javax.swing.JFrame; import javax.swing.JButton; import javax.swing.JLabel; public class BorderLayoutJFrame extends JFrame { public static final int WIDTH = 400; public static final int HEIGHT = 400;
I have a properties file with key values in it, now when I use my search box to search for a key in the properties file to check whether or not it is there - I can't get it to check and return the key value if it is theere. I've tried a few methods but keep getting a null pointer exception when it hits line 15 in the code all the time so I'm stumped at the minute.