Swing/AWT/SWT :: Adding ActionListener To Button Component?

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


ADVERTISEMENT

Swing/AWT/SWT :: Adding ActionListener To 9 JButtons

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

Swing/AWT/SWT :: Adding Actionlistener To Jcombobox

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

Swing/AWT/SWT :: Panel Doesn't Refresh When New Component Added If Smaller Than Largest Visible Component

Jan 26, 2014

I have a JPanel that's using a simple GridBagLayout.

JPanel panel = new JPanel();
GridBagLayout qPanelLayout = new GridBagLayout();
qPanelLayout.columnWidths = new int[] { 0 };
qPanelLayout.rowHeights = new int[] { 0 };
qPanelLayout.columnWeights = new double[] { Double.MIN_VALUE };
qPanelLayout.rowWeights = new double[] { 0.0 };
panel.setLayout(qPanelLayout);
componentCount = 0;

Based on user input I am adding sub-panels to the panel. These sub-panels may vary in height. They take up the full width of the Panel.

public void add(Component comp) {
GridBagConstraints gbc = new GridBagConstraints();
gbc.fill = GridBagConstraints.HORIZONTAL;
gbc.insets = new Insets(2,2,2,2);
gbc.gridx = 0;
gbc.gridy = componentCount++;
panel.add(comp, gbc_questionPane1);
}

The odd behaviour I'm seeing is that if the height of the new sub-panel I'm adding is less than the height of the largest sub-panel currently displayed then the main panel does not repaint. If its height is equal or greater then the Panel does repaint, and any sub-panels that were added but not visible before are now painted. What have I missed?

View Replies View Related

Swing/AWT/SWT :: Adding Button To Refresh Tree

Apr 6, 2015

I want to add a button who refresh the tree the problem that i have not "DefaultTreeModel" in my class to do this ((DefaultTreeModel) jTree1.getModel()).reload(); and i try this jtree.updateUI(); but not work ....

View Replies View Related

Implementing ActionListener On Image Button

Nov 28, 2014

Is there something special about this? I would think not, however all of the buttons in my application work except those with images.Here's an example of one:

Java Code: // Class #1
// All components defined as private initially, used in initComponents() later to build GUI
private JButton btnRock = new JButton();
/** Create the frame. **/
public GameView() {
initComponents(); // Initialize components
} // End of GameView Method
public void initComponents() {
// ROCK BUTTON
btnRock = (JButton) mainController.createIcon("rock");
btnRock.setForeground(new Color(0, 0, 0));
btnRock.setBounds(12, 34, 97, 67);

[code]....

View Replies View Related

JMenuItems - Making ActionListener For Each Button

Jul 13, 2014

So I have this issue with ActionListener, I have created JMenuItems in a loop and applied them to JMenu's. But now when I want to make an action listener for each button, I'm not sure how to select them individually as in the loop it's 1 object looped through different names.

Here is the way I made the menu.

package com.simbaorka101.te.gui;

import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import com.simbaorka101.te.reference.Reference;
public class Menu extends JMenuBar{
private static final long serialVersionUID = 3601135828398064405L;

[Code] ....

View Replies View Related

Actionlistener For Calculator / Button Are Created Within A For Loop For Number 1 To 9

Mar 8, 2014

coding the action listener for my button (btBody) which create button displaying 1 to 9 in a nested for loop; the button should allow the user to click on them and to display the number clicked on in a JTextField;my code;

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

Swing/AWT/SWT :: Need To Add ActionListener

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

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 View Related

Swing/AWT/SWT :: KeyAdapter And ActionListener

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

Swing/AWT/SWT :: How To Come Up With GUI Application Involving List Component

Feb 1, 2014

I am learning Java on my own and I am taking on very small project by myself for fun, and I'm just stuck on this small part of the project.
So I have created a text file that contains a small list of toy names and prices, like:

Barbie, 12.95
Lego, 15.99
Hot Wheels, 5.00
Power Rangers, 6.49

And what I would like is my application to read the contents of the file and store the toy names in a list component. And then I want to be able to select a toy name from the list and add it to a shopping cart that is a list component as well. I want to the application to have menu items and buttons to allow me to remove items from the shopping cart, clear the shopping cart of all selections, and check out. When I check out, the application should calculate and display the subtotal of all the toy names in the shopping cart, the sales tax (which can just be 8 percent of the subtotal), and the total.

View Replies View Related

Adding Image To A Button

Apr 9, 2014

I am trying to add an image to this button.

button3 = new JButton();
button3.setIcon(new Image("C:\Users\Owner\Desktop\AUSE.jpg"));
button3.setEnabled(true);
panel.add(button3);
add(panel);

Error I am getting?

BackgroundImageHelper.java:77: error: Image is abstract; cannot be instantiated
button3.setIcon(new Image("C:\Users\Owner\Desktop\AUSE.jpg"));
^
1 error

[Code] ....

View Replies View Related

Adding Pictures To Button

May 18, 2014

I want to add a picture to my buttons. The end statement will be a path finding algorithm that will with show the picture on all the buttons on the shortest path. I am using a constructor at the moment passing a picture object to it. It is suppose to work but its not on line 30...

Java Code:

import java.awt.Color;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;
import javax.imageio.ImageIO;
import javax.swing.*;
public class Screen implements ActionListener {
public JButton[][] b=new JButton[20][20];

[Code] ....

View Replies View Related

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
}

View Replies View Related

Swing/AWT/SWT :: Same Code For Both ActionListener And MouseListener

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

Swing/AWT/SWT :: ActionPerformed And ActionListener On Netbeans Are Same?

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

Swing/AWT/SWT :: How To Access Variable Outside ActionListener

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

Swing/AWT/SWT :: JMenu - Is This OK To Use MouseListener Over ActionListener

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

How To Pass Main Arguments On Execution To Swing Component

Aug 4, 2014

I wish to use args passed to main(String[]) during application execution to a Swing component.

In a simple example, assume args passed are: a b c d

And here is the result I look for:

package argsinswing;
import javax.swing.JOptionPane;
public class ArgsInSwing {
public static void main(String[] args) {
String str1 = args[0];

[Code] ....

View Replies View Related

Swing/AWT/SWT :: Change Location Of Component Using Grid Bag Layout

Jun 13, 2014

How to change the location of a grid bag positioned component after it's already been added to the layout earlier?

Example is if I want component 1 to change from row 1 to row 2.

View Replies View Related

Swing/AWT/SWT :: How To Drag And Drop Component Within Layout Manager

May 28, 2014

For example, say I have I BoxLayout with a few JButtons in it. How could I make it such that I could drag a JButton such that it could be in front or behind the others?

I've thought about using ComponentMover and just switching to a null layout right before moving it and switch it back right after like bellow, but I'm not sure how to make it keep the change in order.

import java.awt.LayoutManager;
import java.awt.event.MouseEvent;
public class LayoutComponentMover extends ComponentMover
{
private LayoutManager layout;

[code]....

View Replies View Related

Swing/AWT/SWT :: Replace Dropdown Of JComboBox By Another Component Like JTable?

Jan 29, 2015

My question is related to combo boxes. A combo box in general is a text field with a dropdown list. I would want to know if we can replace hthe dropdown List by an other component, for example a JTable component.

View Replies View Related

Swing/AWT/SWT :: Cannot Get ActionListener To Change JLabel Text

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

Adding Entire Code In One Button On JFrame

Jan 29, 2014

I am trying to put

Java Code:

import javax.swing.*;
import java.awt.event.*;
public class menu {
public static void main (String[] args){
JFrame frame = new JFrame("Menu");
frame.setVisible (true);

[Code] ....

View Replies View Related

Clear Button - How To Fix Price To Stop Adding Up

Apr 26, 2014

I've got my clear button to work on most of the stuff I want, however I cant figure out how to fix the price to stop adding up. e.g. hit a button image with a price of 599.99, hit the clear button to empty, select price of 599.99 again then it display 1199.98.

sofa method

if (source == jBSofa) {
System.out.println("Sofa"); {
dTotal = dTotal + 599.99;
jTotal.setText(Double.toString(dTotal));
jTotal.setText(String.format("%.2f", dTotal));
}

Reset method

private void Reset() {
jTTotal.setText(" ");
jTotal.setText(" "); //sub total clear
jTTotalTotal.setText(" ");

Attached image(s)

View Replies View Related







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