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


ADVERTISEMENT

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

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 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 :: 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 :: 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 :: 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

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

Swing/AWT/SWT :: How To Get Double Value Into JTable Cell

May 29, 2014

My latest issue was that I wanted to only get a double value into a JTable Cell.

So first I Overrode the getCellEditor(int row,int col) method of my JTable, so if I was in a cell i knew I needed a double value I returned my Editor.

My editor filters out anything but numbers. I also check to make sure we get one and only one decimal point.

Other solutions Ive seen use keyboard Listeners, but to me that doesn't work. What if the user cuts and pastes data? My solution handles that as well.

It doesn't handle if they try to cut and paste something like 1234abcd.bob.123.23 , this would result in 1234..123.23 pasted in. I'll handle faulty values on the getValue method of my AbstractTable, but as long as the user is using mostly valid data this works well.

public TableCellEditor getCellEditor(int row, int col) {
if (row==TheRowWant && col==TheColumnI want) {
return (new DefaultCellEditor(createDoubleTextField()));
}
}
private JTextField createDoubleTextField() {
final JTextField field = new JTextField();

[Code] .....

View Replies View Related

Swing/AWT/SWT :: Filling A Rectangle With Double Precision

Mar 4, 2014

I have run into a bit of a head scratcher, at least to me. I am building multiple rectangles using double precision. I had to switch from int to double due to another issue that requires decimal places. Now, my fillRect (last line in the code section I posted) is causing an error as it only wants to work with int.

public void draw(Graphics2D g2) {
// check that sizes are above 0
if ((rectWidth <= 0) || (rectHeight <= 0))

[Code]....

View Replies View Related

Swing/AWT/SWT :: Graphics2D / Drawing Double Rounded Corners Border?

Mar 26, 2014

I am struggling to make a double border which has rounded corners. The border should be 2 parallel lines.

I made something which looks almost good, but if you look closer there are a few wrong things.To achieve this, I made 2 paths, then painted them in 2 different colours.

The main problem is painting with AA, because the colours overlap and are affected.

I think it would be smoother to paint a single path using 2 colours.

For the first problem, I think some kind of a stroke would be enough. But, if I build a stroke, it's almost same thing like drawing the path twice, like I did before. Anyway i think that would fix AA problem. But still, maybe I can achieve a smoother effect for the corners.

Question:Maybe something with a 2x1 Texture ? It's possible to achieve this ? I know I tried this before and when a curve was encountered the direction of the texture was wrong and looked messy.

PS: Ignore the blueish thing, it's a dragged component to activate the highlight border.

View Replies View Related

Swing/AWT/SWT :: Double Drawing Pixels When Manipulating DataBuffer Of BufferedImage

Feb 9, 2014

I've been working my way through a tutorial to build a simple 2D tile-based engine that also allows the code to manipulate the colour of pixels on the screen based on a monochrome map read from a file. I've got the code right line for line, but I seem to have a bug when it comes to draw to the screen! At regular intervals, despite what is held in the pixel array of ints (which I have confirmed to be correct when debugging), I get the same row of pixels being draw twice in a row. I figured there was some loop issue somewhere, but after attempting to debug this for some time, I haven't come up with the answer.

Original tilesheet:As rendered:

Game.java:
package igg.engine.game;
import igg.engine.game.gfx.Screen;
import igg.engine.game.gfx.SpriteSheet;
import java.awt.BorderLayout;
import java.awt.Canvas;
import java.awt.Color;

[Code] ....

View Replies View Related

Swing/AWT/SWT :: How To Get One Value From JTextField To Show In Another Classes

May 17, 2014

I want the text that the user inputs into the JTextField in the user input panel to be what shows up in the JTextField in the totals panel.

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Scanner;
import javax.swing.*;
import javax.swing.border.TitledBorder;

[Code] ......

I know that I will need to take advantage of the get and set methods, could I get an example of this? How to write the code.

View Replies View Related

Swing/AWT/SWT :: Splitting GUI Into Multiple Classes?

Sep 12, 2014

I'm trying to create a Java swing chess application, but would like to divide the GUI part of it into at least two different classes (possibly more later, but I'm not sure yet). Currently I have a mainGUI class and a ChessBoard class, both of which extend JFrame. I want the main GUI class to contain a JPanel which will house several components, including a JPanel originating from the chessboard class that contains the actual board.

Is there any way to do this? I tried just creating a ChessBoard object in my main GUI class and then adding it to the component, but I got an illegal argument exception, because apparently you can't add one JFrame to another. How to do what I'm trying to do, or just how to split GUIs into multiple classes in general?

View Replies View Related

Swing/AWT/SWT :: How To Get Text From Jtextfield And Transfer It Between Classes

Apr 8, 2014

Im writing a program that records the score in a dart game. One component of the program is a stage where players enter their name.This information needs to be displayed in another part of the program. These two components are in two different classes. My problem is I dont know how to get that information from one class to another.

Im using Jtextfield in Jframe to record the information.

View Replies View Related

Swing/AWT/SWT :: Extending Classes And Importing Libraries Basics?

Mar 12, 2015

1. Tried to use setLayout() but it wouldn't let me. Have imported libs

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

but it doesn't work. It only worked when I extended a class with JFrame. Why do I have to do it? I already have a JFrame instance:

public test(){
JFrame frame = new JFrame();
frame.setTitle("Workshop");
frame.setBounds(500,300,400,300);
frame.setVisible(true);
frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
....
}

This code runs smooth, so it's confusing for me that it lets me create a JFrame object, and put buttons onto it, but it wants me to extend the class onto JFrame class in order to use a method.

2. I've read somewhere that AWT is old and should not be used anymore. Is there a diagram or a list with objects and properties that are manipulated only by Swing so that I could concentrate only on those? For example (made up list):

JPaper -

- width, setWidth(x)
- height, setHeight(x)
- position, setPosition(x,y)
- color, setBackground(RGBa)
- acquire, add(subordinate component)
- deprive, remove( subordinate comopnent)
- etc...

JSticker -

- width ...
- height ...
- write, setText(String)
- etc...

View Replies View Related

Swing/AWT/SWT :: GUI - Create User Interface That Interacts With Setters And Getters Of Some Classes

Apr 14, 2014

I am new a creating GUIs and am not quite sure how to correctly make one. I have done the inheritance parts, and created two extra appliances: a washer and dryer. Now Creating the GUI ....

Here are the instructions to my project.

Introduction to GUIs (+ some inheritance)

For this assignment, you are going to create a user interface that interacts with the setters and getters of some classes that you will create.

First, create an abstract class called Appliance. This abstract class should have two attributes (dealing with household appliances) and two abstract methods called turnOn() and turnOff(). These methods should return void.

Then, create two subclasses of Appliance that represent household appliances (like a Refrigerator or Stove ((don't use those!))). These subclasses should have two attributes that are specific to the various appliance. Each subclass should implement the turnOn() and turnOff() methods. These methods should print to the command line some information about the appliance as it turns on and off.

Now, the fun part! Create a GUI interface!

Your window should have two panels: one for each appliance subclass. Each panel should have 4 textboxes (with appropriate labels) to receive/display information that correspond to the 4 attributes (2 from Appliance and 2 from the subclass) for each subclass.You also need 2 buttons on each panel: A Get button and a Set button.

When the Get button is pressed, the text boxes should be filled with the information from the instantiated object of the appropriate subclass. When the Set button is pressed, the object should then contain the information contained that the user has altered.

In your main method, you should create an object of each subclass, and prefill it with information (either using the constructor or the setters), then display your GUI. You should now be able to get and set the information for your objects from the GUI.

At least one of your attributes for each subclass should be numeric

Note that you will need to handle incorrectly formatted input (You can use exception handling to do this if you want to. Wrapper classes also will work)

If there is text in the boxes when the "Get" button is pressed, it should be overwritten by what is in the object. Remember that these two panels should both be on screen at the same time.

You don't need 2 different windows, one window: 2 panels.

View Replies View Related

How To Call Classes Within Other Classes

Apr 18, 2014

How do you call classes within other classes? Or can you only call classes through the main?

View Replies View Related

EJB / EE :: Same Listener MDB For Different Jms Queue?

Feb 21, 2014

Is it possible to define the same message driven bean as a listener to different queues?

The goal being to define different redelivery configuration for different kind of messages, but handle them all through a single deployment MDB for unity/entity purposes.

The other option is to have many MDBs targeted to the same source code.

View Replies View Related

Why Listener Can't See Frame2

Mar 26, 2015

I have a problem:I have a simple SWING program with buttons and I can't call "frame2.setVisible(true)" from ActionListener registered on a button. I don't understand, why the listener can't see "frame2".

import javax.swing.*;
import java.awt.BorderLayout;
import java.awt.GridLayout;
import java.awt.event.*;
public class test {

[code]....

View Replies View Related

How To Write Key Listener

Jan 7, 2014

I want to make a program that will move a square to the left if you press "a", and to the right if you press "d". (Once I know how to do this I can figure out W and S for up and down by myself). What code would I use for doing that? Here is the program I have now. I used the oracle tutorial but it just shows how to handle for if ANY key is pressed.

This is what I used How to Write a Key Listener (The Java Tutorials > Creating a GUI With JFC/Swing > Writing Event Listeners)

Java Code:

import java.awt.BorderLayout;
import java.awt.Graphics;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JPanel;

public class myGame extends JComponent {
public int x = 20;
public int y = 20;

[code]....

View Replies View Related







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