Lost Password?

Go Back   CodeCall Programming Forum > Software Development > Java Help

Unregistered, Check out the Coder Battles in the Announcement and Game forums.

Java Help Java Help forum discussing all Java platforms - J2ME, J2SE and J2EE - as well as relevant standards, APIs and frameworks such as Swing, Servlets, JSPs, Applets, Struts, Spring, Hibernate, ANT, EJB, and other Java-related topics.

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 07-11-2007, 12:45 AM
Plutonic Plutonic is offline
Newbie
 
Join Date: Jul 2007
Posts: 3
Credits: 0
Rep Power: 0
Plutonic is on a distinguished road
Default Using input to change a variable?

Hey I was wondering if anyone could tell me how I would use input such as typing a key to change an int variable? Everything I've tried so far compiles fine but the variable still doesn't change.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote

Sponsored Links
  #2 (permalink)  
Old 07-11-2007, 06:45 AM
John's Avatar   
John John is offline
Co-Administrator
 
Join Date: Jul 2006
Age: 20
Posts: 3,238
Last Blog:
Passwords
Credits: 881
Rep Power: 20
John has much to be proud ofJohn has much to be proud ofJohn has much to be proud ofJohn has much to be proud ofJohn has much to be proud ofJohn has much to be proud ofJohn has much to be proud ofJohn has much to be proud ofJohn has much to be proud of
Send a message via AIM to John Send a message via MSN to John
Default

Post what you have tried
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #3 (permalink)  
Old 07-11-2007, 05:29 PM
Plutonic Plutonic is offline
Newbie
 
Join Date: Jul 2007
Posts: 3
Credits: 0
Rep Power: 0
Plutonic is on a distinguished road
Default

Ok so far I've tried using the getKeyCode method in the keyPressed and keyReleased methods. eg:

public void keyPressed(KeyEvent input) {

int key = input.getKeyCode();
if (key == 68)
p1xMove = 1;
}

public void keyPressed(KeyEvent input) {

char key = input.getKeyChar();
if (key == 'd')
p1xMove = 1;
}

Where p1xMove is an int variable I've set up earlier. I've even tried using boolean variables but it still doesn't work!
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #4 (permalink)  
Old 07-17-2007, 04:02 PM
John's Avatar   
John John is offline
Co-Administrator
 
Join Date: Jul 2006
Age: 20
Posts: 3,238
Last Blog:
Passwords
Credits: 881
Rep Power: 20
John has much to be proud ofJohn has much to be proud ofJohn has much to be proud ofJohn has much to be proud ofJohn has much to be proud ofJohn has much to be proud ofJohn has much to be proud ofJohn has much to be proud ofJohn has much to be proud of
Send a message via AIM to John Send a message via MSN to John
Default

Well here are a few things to keep in mind.

1. You have to implement the KeyListener class
2. You have to add a KeyListener to a component [maybe the main frame]
3. Make sure the instance variable is initialized
4. Make sure you change the variable to something different other than what it was initialized to.
5. If your using the key to update a text value in a component, make sure you repaint the component.

Since you didn't provide the entire class, I cant check for these. But here is a small demo I just created.

Code:
package net.codecall.forums;
import java.awt.Dimension;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import javax.swing.JFrame;
import javax.swing.JLabel;
public class ButtonListener implements KeyListener{
	//Instance Variable
	private int i = 0;
	private JLabel label;	
	public ButtonListener() {
		//create a new JFrame
		JFrame frame = new JFrame("Key Listener");
		//set the dimensions
		frame.setPreferredSize(new Dimension(200, 200));
		/******   ADD AN ACTION LISTENER TO THE FRAME   ******/
		frame.addKeyListener(this);		
		//create a new JLabel
		label = new JLabel();
		//set the text on the label
		label.setText(String.valueOf(i));
		//add the label
		frame.add(label);
		//make the frame visible
		frame.setVisible(true);
		//set the default close operation
		frame.setDefaultCloseOperation(frame.EXIT_ON_CLOSE);
		//pack the frame
		frame.pack();		
	}
	public void keyPressed(KeyEvent e) {
		//If the key D was pressed
		if(e.getKeyCode() == 68){
			//increase i by one
			i++;
			//'repaint' the label
			label.setText(String.valueOf(i));
			//and output the value to the console
			System.out.println(i);
		}
	}
	public void keyReleased(KeyEvent e) {	}
	public void keyTyped(KeyEvent arg0) {	}	
	public static void main(String[] args){
		new ButtonListener();
	}
}
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Reply



Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Similar Threads
Thread Thread Starter Forum Replies Last Post
Coding a change password form InternetGeek Visual Basic Programming 11 02-16-2008 01:53 PM
[C++] Validating user input Xochiquetzal C and C++ 2 07-08-2007 05:18 PM
Java:Tutorial - User Input John Java Tutorials 0 12-09-2006 07:25 PM
Java:Tutorial - The Variable John Java Tutorials 0 12-09-2006 09:59 AM
Using change management Cosmet General Programming 2 10-30-2006 06:16 PM


All times are GMT -5. The time now is 07:36 PM.

Contest Stats

Xav ........ 1333.07
MeTh0Dz|Reb0rn ........ 1055.7
John ........ 881.37
morefood2001 ........ 879.43
marwex89 ........ 869.98
WingedPanther ........ 851.68
Brandon W ........ 757.44
chili5 ........ 312.39
Steve.L ........ 247.05
dcs ........ 219.87

Contest Rules

CodeCall Goal

Goal: 100,000 Posts
Complete: 82%

Ads