Bay 12 Games Forum

Please login or register.

Login with username, password and session length
Advanced search  
Pages: 1 [2] 3

Author Topic: Basic Java GUIs  (Read 4405 times)

Nadaka

  • Bay Watcher
    • View Profile
    • http://www.nadaka.us
Re: Basic Java GUIs
« Reply #15 on: November 27, 2009, 01:22:06 pm »

AWT is older, dependant on the architectures native gui rendering (inconsistent and in some case not portable) and has fewer features.

SWING is newer, faster and consistently rendered using the java2d api and supports more features such as skinning and data binding (though I think its called something else).

JApplet can only be used in a browser or applet viewer and effectively acts as a frame. JFrame can be used in command line java applications to provide gui displays and interfaces.
Logged
Take me out to the black, tell them I ain't comin' back...
I don't care cause I'm still free, you can't take the sky from me...

I turned myself into a monster, to fight against the monsters of the world.

eerr

  • Bay Watcher
    • View Profile
Re: Basic Java GUIs
« Reply #16 on: November 27, 2009, 09:47:03 pm »

Note that despite the similarity of SWING to AWT

Swing and Awt work very poorly together. This may have caused some previous frustration working on my java homework.
Logged

AlStar

  • Bay Watcher
    • View Profile
Re: Basic Java GUIs
« Reply #17 on: December 02, 2009, 10:51:07 pm »

With the Nethack Tourny over, I've finally got some more time to program.

Ok, after banging my head uselessly against the wall of KeyEvents and getting bloody KeyListeners to work with them (I just CANNOT seem to get a if(KeyEvent.VK_ENTER) to work whatever I try), I've come up with a solution that seems to work.

while (jTextField1.getText().length() == 0){

         }
         input = jTextField1.getText();
...
         jTextField1.setText(null);
... (repeat where needed)


I'm certain there's GOT to be a better way to deal with this crud, but this seems to work as close to my original console interface as I can get at the moment. I'm open to cleaner implementations.

eerr

  • Bay Watcher
    • View Profile
Re: Basic Java GUIs
« Reply #18 on: December 03, 2009, 01:41:22 pm »


I did it fancy or something, instead of copying me, make a class that:

import java.awt.event.*;
import javax.swing.*;
-implements KeyListener
    public void keyTyped(KeyEvent e) {}
    public void keyPressed(KeyEvent e) {}
    public void keyReleased(KeyEvent e) {}

-make an object of said class
-send the Keylistener object to the Text field.

//Bear in mind that Sun and Awt, have two diffrent names for keylistener
//They differ only in capitalisation, and are not omi-compatible.


I used
http://java.sun.com/docs/books/tutorial/uiswing/events/keylistener.html
as a refrence.


Or send me your broken keylistener code so I can fix it.
« Last Edit: December 03, 2009, 02:03:20 pm by eerr »
Logged

AlStar

  • Bay Watcher
    • View Profile
Re: Basic Java GUIs
« Reply #19 on: December 03, 2009, 02:16:18 pm »

I used
http://java.sun.com/docs/books/tutorial/uiswing/events/keylistener.html
as a refrence.

It's funny, I looked through the same document while I was trying to get my code working. I think there may just be something with KeyEvents that brake my brain or something.

Anyway, I'm at work now, but I'll give your example above a shot when I get back home.

Thanks for the offer of fixing my code up for me, but I'm afraid that my code probably looks like chicken scratches (uncommented chicken scratches, at that) and I wouldn't want to inflict that on someone else... besides - if I don't learn how to do it myself, how would I be able to do it again in the future? Learning experience and all that.

eerr

  • Bay Watcher
    • View Profile
Re: Basic Java GUIs
« Reply #20 on: December 03, 2009, 02:28:46 pm »

The key events probably work.


Code: [Select]
if(KeyEvent.VK_ENTER)Has at least two problems, I'm not sure what to correct first!

KeyEvent is the Class KeyEvents are made from, not a KeyEvent "e".
VK_ENTER is type char not boolean. As a compiled value representing "Enter" on any system, it's also constant!
You need the .getKey() == KeyEvent.VK_ENTER that was in my code previously.

Try:
Code: [Select]
if(e.getKey() == KeyEvent.VK_ENTER){then stuff happens}
Where e is an object of type "KeyEvent"
So "if" the char key inside e equals the constant char VK_ENTER inside KeyEvent, then stuff happens.
« Last Edit: December 03, 2009, 09:17:26 pm by eerr »
Logged

AlStar

  • Bay Watcher
    • View Profile
Re: Basic Java GUIs
« Reply #21 on: December 03, 2009, 11:02:08 pm »

Heh, I wasn't actually using that exact if() phrase, but your explaination was very handy anyway.

Ok, so, progress. I've got KeyListener implemented, keyTyped, Pressed and Released all set up as per your example above.

I've got a KeyListener (kl) and a KeyEvent (e).

I've got jTextField1 addKeyListener(kl); and jTextField1.transferFocus();

Now, I'm trying to actually use the command if(e.getKeyCode()  == KeyEvent.VK_ENTER){
//stuff
}
First time, it nulled out, so I added if (e != null) in front of the above.

That got the program running, but now I'll enter an input and hit enter... and nothing happens. No Error, no nothing.

eerr

  • Bay Watcher
    • View Profile
Re: Basic Java GUIs
« Reply #22 on: December 03, 2009, 11:18:19 pm »

What is jTextField1.transferFocus(); for?

That may break/disrupt some ear-full java components, including jTextField1.

Shouldn't need transferFocus() unless you've got other input components.

Either that, or "VK_ENTER" doesn't generate a "key typed" event, just "key pressed" and "key released". Because the enter key does nothing in a text field?

It is a mystery!
« Last Edit: December 03, 2009, 11:31:36 pm by eerr »
Logged

AlStar

  • Bay Watcher
    • View Profile
Re: Basic Java GUIs
« Reply #23 on: December 03, 2009, 11:31:00 pm »

I thought it would make sure that the correct textbox was accepting input.

However, it doesn't look like that's the problem, unfortunately, since commenting it out doesn't seem to change anything (for better or worse).

Edit: hmm, also tryed if(e.isActionKey()  == true){} same thing.

It's strange, because I don't see anything obviously wrong with any of it...
« Last Edit: December 03, 2009, 11:38:41 pm by AlStar »
Logged

SolarShado

  • Bay Watcher
  • Psi-Blade => Your Back
    • View Profile
Re: Basic Java GUIs
« Reply #24 on: December 04, 2009, 12:02:02 am »

Either that, or "VK_ENTER" doesn't generate a "key typed" event, just "key pressed" and "key released". Because the enter key does nothing in a text field?

This would be my guess.

If you're running with a terminal window open (you should, makes debugging much easier), toss a few System.out.println(e.toString())s into your event handlers.
Logged
Avid (rabid?) Linux user. Preferred flavor: Arch

eerr

  • Bay Watcher
    • View Profile
Re: Basic Java GUIs
« Reply #25 on: December 04, 2009, 01:07:16 am »

Either that, or "VK_ENTER" doesn't generate a "key typed" event, just "key pressed" and "key released". Because the enter key does nothing in a text field?

This would be my guess.

If you're running with a terminal window open (you should, makes debugging much easier), toss a few System.out.println(e.toString())s into your event handlers.

I was under the impression he could already print somewhere, and was doing so.
Logged

SolarShado

  • Bay Watcher
  • Psi-Blade => Your Back
    • View Profile
Re: Basic Java GUIs
« Reply #26 on: December 04, 2009, 01:21:17 am »

Just making sure.

Personally, I use that trick (dump all the event info to the term.) whenever I try something new, particularly the KeyListener class, because it can be so confusing. Plus, if you're dumping from all 3 KeyListener methods, you can see exactly what is called when, and in response to what user action.
Logged
Avid (rabid?) Linux user. Preferred flavor: Arch

eerr

  • Bay Watcher
    • View Profile
Re: Basic Java GUIs
« Reply #27 on: December 05, 2009, 10:58:16 pm »

Do keyevents support .toString?
Logged

SolarShado

  • Bay Watcher
  • Psi-Blade => Your Back
    • View Profile
Re: Basic Java GUIs
« Reply #28 on: December 05, 2009, 11:29:42 pm »

Do keyevents support .toString?

Last I checked. Technically, any object does; as I recall KeyEvents include the event type,  the key code (converted the the approptiate constant I think), and the char produced (if applicable), plus some other, less useful, stuff.

EDIT: Using a quick throw-away app, here's the toString()s from typing "Bay12Games"<enter>:
Code: [Select]
java.awt.event.KeyEvent[KEY_PRESSED,keyCode=16,keyText=Shift,keyChar=Undefined keyChar,modifiers=Shift,extModifiers=Shift,keyLocation=KEY_LOCATION_LEFT] on textfield0
java.awt.event.KeyEvent[KEY_PRESSED,keyCode=66,keyText=B,keyChar='B',modifiers=Shift,extModifiers=Shift,keyLocation=KEY_LOCATION_STANDARD] on textfield0
java.awt.event.KeyEvent[KEY_TYPED,keyCode=0,keyText=Unknown keyCode: 0x0,keyChar='B',modifiers=Shift,extModifiers=Shift,keyLocation=KEY_LOCATION_UNKNOWN] on textfield0
java.awt.event.KeyEvent[KEY_RELEASED,keyCode=66,keyText=B,keyChar='B',modifiers=Shift,extModifiers=Shift,keyLocation=KEY_LOCATION_STANDARD] on textfield0
java.awt.event.KeyEvent[KEY_RELEASED,keyCode=16,keyText=Shift,keyChar=Undefined keyChar,keyLocation=KEY_LOCATION_LEFT] on textfield0
java.awt.event.KeyEvent[KEY_PRESSED,keyCode=65,keyText=A,keyChar='a',keyLocation=KEY_LOCATION_STANDARD] on textfield0
java.awt.event.KeyEvent[KEY_TYPED,keyCode=0,keyText=Unknown keyCode: 0x0,keyChar='a',keyLocation=KEY_LOCATION_UNKNOWN] on textfield0
java.awt.event.KeyEvent[KEY_RELEASED,keyCode=65,keyText=A,keyChar='a',keyLocation=KEY_LOCATION_STANDARD] on textfield0
java.awt.event.KeyEvent[KEY_PRESSED,keyCode=89,keyText=Y,keyChar='y',keyLocation=KEY_LOCATION_STANDARD] on textfield0
java.awt.event.KeyEvent[KEY_TYPED,keyCode=0,keyText=Unknown keyCode: 0x0,keyChar='y',keyLocation=KEY_LOCATION_UNKNOWN] on textfield0
java.awt.event.KeyEvent[KEY_RELEASED,keyCode=89,keyText=Y,keyChar='y',keyLocation=KEY_LOCATION_STANDARD] on textfield0
java.awt.event.KeyEvent[KEY_PRESSED,keyCode=49,keyText=1,keyChar='1',keyLocation=KEY_LOCATION_STANDARD] on textfield0
java.awt.event.KeyEvent[KEY_TYPED,keyCode=0,keyText=Unknown keyCode: 0x0,keyChar='1',keyLocation=KEY_LOCATION_UNKNOWN] on textfield0
java.awt.event.KeyEvent[KEY_RELEASED,keyCode=49,keyText=1,keyChar='1',keyLocation=KEY_LOCATION_STANDARD] on textfield0
java.awt.event.KeyEvent[KEY_PRESSED,keyCode=50,keyText=2,keyChar='2',keyLocation=KEY_LOCATION_STANDARD] on textfield0
java.awt.event.KeyEvent[KEY_TYPED,keyCode=0,keyText=Unknown keyCode: 0x0,keyChar='2',keyLocation=KEY_LOCATION_UNKNOWN] on textfield0
java.awt.event.KeyEvent[KEY_RELEASED,keyCode=50,keyText=2,keyChar='2',keyLocation=KEY_LOCATION_STANDARD] on textfield0
java.awt.event.KeyEvent[KEY_PRESSED,keyCode=16,keyText=Shift,keyChar=Undefined keyChar,modifiers=Shift,extModifiers=Shift,keyLocation=KEY_LOCATION_LEFT] on textfield0
java.awt.event.KeyEvent[KEY_PRESSED,keyCode=71,keyText=G,keyChar='G',modifiers=Shift,extModifiers=Shift,keyLocation=KEY_LOCATION_STANDARD] on textfield0
java.awt.event.KeyEvent[KEY_TYPED,keyCode=0,keyText=Unknown keyCode: 0x0,keyChar='G',modifiers=Shift,extModifiers=Shift,keyLocation=KEY_LOCATION_UNKNOWN] on textfield0
java.awt.event.KeyEvent[KEY_RELEASED,keyCode=71,keyText=G,keyChar='G',modifiers=Shift,extModifiers=Shift,keyLocation=KEY_LOCATION_STANDARD] on textfield0
java.awt.event.KeyEvent[KEY_RELEASED,keyCode=16,keyText=Shift,keyChar=Undefined keyChar,keyLocation=KEY_LOCATION_LEFT] on textfield0
java.awt.event.KeyEvent[KEY_PRESSED,keyCode=65,keyText=A,keyChar='a',keyLocation=KEY_LOCATION_STANDARD] on textfield0
java.awt.event.KeyEvent[KEY_TYPED,keyCode=0,keyText=Unknown keyCode: 0x0,keyChar='a',keyLocation=KEY_LOCATION_UNKNOWN] on textfield0
java.awt.event.KeyEvent[KEY_RELEASED,keyCode=65,keyText=A,keyChar='a',keyLocation=KEY_LOCATION_STANDARD] on textfield0
java.awt.event.KeyEvent[KEY_PRESSED,keyCode=77,keyText=M,keyChar='m',keyLocation=KEY_LOCATION_STANDARD] on textfield0
java.awt.event.KeyEvent[KEY_TYPED,keyCode=0,keyText=Unknown keyCode: 0x0,keyChar='m',keyLocation=KEY_LOCATION_UNKNOWN] on textfield0
java.awt.event.KeyEvent[KEY_RELEASED,keyCode=77,keyText=M,keyChar='m',keyLocation=KEY_LOCATION_STANDARD] on textfield0
java.awt.event.KeyEvent[KEY_PRESSED,keyCode=69,keyText=E,keyChar='e',keyLocation=KEY_LOCATION_STANDARD] on textfield0
java.awt.event.KeyEvent[KEY_TYPED,keyCode=0,keyText=Unknown keyCode: 0x0,keyChar='e',keyLocation=KEY_LOCATION_UNKNOWN] on textfield0
java.awt.event.KeyEvent[KEY_RELEASED,keyCode=69,keyText=E,keyChar='e',keyLocation=KEY_LOCATION_STANDARD] on textfield0
java.awt.event.KeyEvent[KEY_PRESSED,keyCode=83,keyText=S,keyChar='s',keyLocation=KEY_LOCATION_STANDARD] on textfield0
java.awt.event.KeyEvent[KEY_TYPED,keyCode=0,keyText=Unknown keyCode: 0x0,keyChar='s',keyLocation=KEY_LOCATION_UNKNOWN] on textfield0
java.awt.event.KeyEvent[KEY_RELEASED,keyCode=83,keyText=S,keyChar='s',keyLocation=KEY_LOCATION_STANDARD] on textfield0
java.awt.event.KeyEvent[KEY_PRESSED,keyCode=10,keyText=Enter,keyChar=Enter,keyLocation=KEY_LOCATION_STANDARD] on textfield0
java.awt.event.KeyEvent[KEY_TYPED,keyCode=0,keyText=Unknown keyCode: 0x0,keyChar=Enter,keyLocation=KEY_LOCATION_UNKNOWN] on textfield0
java.awt.event.KeyEvent[KEY_RELEASED,keyCode=10,keyText=Enter,keyChar=Enter,keyLocation=KEY_LOCATION_STANDARD] on textfield0
That was typed into an AWT TextField, things might be a bit different from the swing equivalent.
« Last Edit: December 05, 2009, 11:45:28 pm by SolarShado »
Logged
Avid (rabid?) Linux user. Preferred flavor: Arch

eerr

  • Bay Watcher
    • View Profile
Re: Basic Java GUIs
« Reply #29 on: December 06, 2009, 03:22:48 am »

I have to admit, thats alot of information.

useful information.

java.awt.event.KeyEvent[KEY_PRESSED,keyCode=10,keyText=Enter,keyChar=Enter,keyLocation=KEY_LOCATION_STANDARD] on textfield0java.awt.event.KeyEvent[KEY_TYPED,keyCode=0,keyText=Unknown keyCode: 0x0,keyChar=Enter,keyLocation=KEY_LOCATION_UNKNOWN] on textfield0

Null  keytext under key_typed for the enter button? hmmm
That actually explains the null ptr error alstar got.
Logged
Pages: 1 [2] 3