Swing/AWT/SWT :: How To Implement ActionListener
Feb 7, 2014
I have a class with jbutton declared inside like these:
public class UI{
private JFrame ventana;
private JTable table;
private JPanel panel;
private JScrollPane tableScrollPane;
private JTextField aBuscar;
[Code] .....
I would like to add actionlistener in my JButton's. Where must I declared these listener. How should I do these?
View Replies
ADVERTISEMENT
Mar 25, 2015
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
[code].....
View Replies
View Related
Nov 27, 2014
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.
View Replies
View Related
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
}
View Replies
View Related
Mar 8, 2014
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.
View Replies
View Related
Mar 3, 2014
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?
View Replies
View Related
Mar 8, 2014
I need to add actionlistener to my code which allow the number 1 to 9 to function which are located in the nested for loop.
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
public class EX_7_2 extends JFrame {
public EX_7_2() {
setLayout(new BorderLayout(5, 10));
[Code] .....
View Replies
View Related
Jul 26, 2014
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
[Code].....
View Replies
View Related
Aug 13, 2014
I know that JMenu and JMenuItem is suppose to use action listeners but would you consider this implementation an exception?
menuAbout.addMouseListener (new MouseListener () {
@Override
public void mouseClicked (MouseEvent arg0) {
JOptionPane.showMessageDialog (null, "Server Tec Software System
}
@Override
public void mouseEntered (MouseEvent arg0) { }
@Override
public void mouseExited (MouseEvent arg0) { }
@Override
public void mousePressed (MouseEvent arg0) { }
@Override
public void mouseReleased (MouseEvent arg0) { }
});
View Replies
View Related
Jun 10, 2014
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()
View Replies
View Related
Mar 7, 2014
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)?
View Replies
View Related
Apr 24, 2014
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.
import javax.swing.BoxLayout;
import javax.swing.JFrame;
import javax.swing.JButton;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
[code]....
View Replies
View Related
Apr 20, 2014
I have dragged and dropped a jTextArea from netbeans palette onto my form. But when I right clicked for Event > action > actionPerformed for implementation, I realized such was not there, unlike the jTextField component. For example, I have tried this but it didn't work:
//{
Import java.swing.*;
JTextArea txa = new JTextArea();
txa.getText(jTextArea1);
txa.setText(jTextArea1);
txa.setTextWrap(true);
//}
View Replies
View Related
Jun 10, 2014
I have to implement the median filter. I found an example on the internet but it does not run, I do not see the image
public void median_RGB(Immagine img) {
int maskSize = 3;
int width = img.getW();
[Code]....
View Replies
View Related
Aug 5, 2014
I'm trying to implement CSS in Java via a JEditorPane and an HTMLEditorKit. The CSS is included below. It *should* create a container with one on the left, small, and one on the right, large. It works flawlessly on JSFiddle [URL] ...., but on Java, it won't put the two divs next to each other. Is this a problem in Java/Netbeans or is it my problem? It creates the divs, but beneath each other. The JEditorPane is wide enough for all of them to fit.
Coding:
Java:
private void loadBulletins() {
//<editor-fold defaultstate="collapsed" desc="HTML/CSS Script">
String eBullStyles = "#divMsgs{ "
+ "height: 99px; "
+ "margin: auto; "
+ "background-color: #3399FF; "
[code]....
View Replies
View Related
Mar 20, 2015
I have a stage,it has two layers scene, if user click the button,then change to another layer, and also can change back. It just like swing cardlayout. How can i implement cardlayout in JAVAFX?
View Replies
View Related
Sep 12, 2014
I am trying to make a ChessBoard class composed of an array of JLabels inside a JPanel with a grid layout. I am also trying to override the getPreferredSize method so that the board will change size when I resize the main window (in another class in which I will instancize this class as part of a larger GUI). I got this kind of layout working before, but now I am trying to get it to work with multiple classes. However, after copying in the part of the previous code corresponding to the panel's layout, I am encountering some errors that I don't know how to solve. Specifically, when I try to override the getPreferredSize method, the compiler tells me "method does not override or implement a method from a super type, " and that it can't find the method "getPreferredSize"
Here's my code:
public class ChessBoard extends JPanel//the panel that this class extends is the boardHousing
{
//mental chess board piece array
Piece mentalBoard[][] = new Piece[8][8];
//actual GUI chessboard JLabel Array
static JLabel chessBoard[][] = new JLabel[8][8];
[Code] ....
I would just think that I was overriding the method incorrectly, but the weird thing is that I got that specific section of code to work before -- the only thing different now is that there are multiple classes, so my ChessBoard class itself is extending JPanel.
View Replies
View Related
Oct 27, 2014
This code is directly from Swing: I'm using Eclipse and keep getting an error on line 10 saying :
"The type JTextField must implement the inherited abstract method ActionListener.actionPerformed(ActionEvent)."
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextField;
import javax.swing.SwingUtilities;
[Code] ......
View Replies
View Related
Nov 17, 2014
public void paint(Graphics g) {
g2=(Graphics2D)g;
rectangle=new Rectangle(50,50,100,100);
}
public action() {
JFrame frame=new JFrame("Assignment 2");
frame.setSize(700,700);
frame.setVisible(true);
frame.setLocationRelativeTo(null);
[code]....
I'm trying to add shapes on my panel once a button is clicked.The tutorials I found is having only 1 panel & therefore they can use g.drawRect() directly but I have few panels & that's the part I don't know how to implement it.The coding above with (//invalid) is the part I would like to write it out but it's invalid.
View Replies
View Related
Mar 31, 2014
I want to make a game and I have the following:
A main JFrame which include some text and a Button (The Start Button), and I made an ActionListener for that Button to another ActionListener which have a for, but when I run the project I only see the final of the for loop.
View Replies
View Related
Feb 11, 2015
What Is the difference between an ActionListener and an EventHandler ?
button.addActionListener(new ActionListener() {
button.setOnAction(new EventHandler<ActionEvent>() {
View Replies
View Related
Apr 18, 2014
I am using the DJWebbrowser in a Java Swing Application that I am working on
// Browser addon as Panel
JPanel browser = new JPanel();
browser = (JPanel) util.BPanel.createContent();
wrapper.add(browser);
and am sending data from a Google Map API , Distance Matrix Query with following code
function callback(response, status) {
if (status != google.maps.DistanceMatrixStatus.OK) {
alert('Error was: ' + status);
} else {
var origins = response.originAddresses;
var destinations = response.destinationAddresses;
var outputDiv = document.getElementById('outputDiv');
[code]...
I haven't found a way to access a class property from the class that invoked the web browser, because the above WebBrowserAdapter is in an anonymous Inner Class.´I would like to have the distance that is sent from the Javascript to the application be passed on to the class member "distance" that in the invoking class.
View Replies
View Related
Dec 25, 2014
I have a snippet here that I'm working with and I have a few questions about it.
button.addActionListener( new ActionListener(){
@Override
public void actionPerformed(ActionEvent ev){
System.out.println("Button Pressed");
}
});
My questions are:
1. How is it possible to use new on ActionListener when ActionListener is an Interface, not a Class. Isn't it illegal to instantiate an Interface?
2. What is going on with the argument to addActionListener? We have the new ActionListener, but we also have a method being defined as well?
View Replies
View Related
Jul 24, 2014
i have a row of int JTextfields and i want to get a running total of the sum of all boxes in the last total JTextfield, as the user types.I understand its an actionlistener but whats the best way of doing it. What the action to listen out for?
View Replies
View Related
Mar 18, 2015
How can I remove the ActionListener from the buttons of my application after i have got something happen.
Please consider the following application:
Java Code:
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class Exer1218659 extends JFrame {
private String[] textButtons;
private Container contents;
[Code] .....
After the line 37 executes I expect that the ActionHandler (ah) will be removed from all of the buttons but this do not happen, then all the remaining button still responding to the clicks. Where is the problem in my code.
View Replies
View Related
Jun 21, 2014
In my given below code..in Action listener #Report function does not calling.
Java Code:
import java.awt.Color;
import java.awt.Container;
import java.awt.Font;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
[Code] ....
View Replies
View Related