• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Usability features

 
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hey everybody, I've been writing a simple chat program, and I was wondering about a few features that I haven't been able to figure out just yet.

1. How do I set the scroll bar's position manually, via code.
2. How do I call a method from a keystroke?

I plan on putting a method call to whatever method number 1 entails at the end of the while loop in IncomingReader's run() method, and I'd also like the program to be able to send the user's message when they press ENTER.

The code for the client class:

chat-screenshot.JPG
[Thumbnail for chat-screenshot.JPG]
Two client GUI's, two client CLI's, and the server CLI.
 
Ranch Hand
Posts: 4632
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
> 1. How do I set the scroll bar's position manually, via code.

get the scrollbar from the scrollPane and
scrollBar.setValue(someIntValue);

but this has always been a bit wonky
a lot depends on what the maximum value is (and, if I remember correctly, this 'maximum' can change)

an alternative is to get the scrollPane's viewport and
viewport.scrollRectToVisible(someRectangle);

where someRectangle could be (e.g.) textfield.getBounds()


> 2. How do I call a method from a keystroke?

keyBindings

http://java.sun.com/docs/books/tutorial/uiswing/misc/keybinding.html
 
Eric Larsen
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks, Michael.

I'm still having problems, though. When I try to compile my code now, it give's me an error:



My revised code to bind the Enter key to my JTextField outgoing:

 
Bartender
Posts: 1561
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Rather than implement Action, I'd try having having your class extend AbstractAction. This way while it has to implement the actionPerformed method it only overrides the other Action methods that you think are necessary (or none at all).
 
Sheriff
Posts: 22781
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You also might want to combine the SendButtonListener and SendAction classes, removing the first. Action extends ActionListener, so you can use Action where you can use ActionListener.
 
Eric Larsen
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Awesome, thanks guys!!!

It works very well now! I took Rob and Pete's advice and it helped quite a bit. Thanks!
I also put in some other code to check for the presence of text in JTextField outgoing before sending.

Here's the final code:

 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic