wood burning stoves 2.0*
The moose likes Swing / AWT / SWT and the fly likes How to use method here Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "How to use method here" Watch "How to use method here" New topic
Author

How to use method here

adeeb alexander
Ranch Hand

Joined: May 29, 2008
Posts: 268
Hi all.
i think this is a simple question for you guyz. I am unable get into my mind.

my program structure is given below can any one help me out. I have to use the same code in actionPerformed and KeyReleased so how can i create a new method and call it in ctionPerformed and KeyReleased. Please some one just suggest me.



Thanks and Regards
alexander
Maneesh Godbole
Saloon Keeper

Joined: Jul 26, 2007
Posts: 10451
    
    8

Just create a new method called say doStuff() and call it from inside your actionPerformed() and keyReleased().
However, you need to make sure, that you do not invoke the method twice. Hitting enter on a button (on some OS) will generate the action events as well as the key events if the button is registered as a source for both the events.

What is your requirement? Why are you having both type of listeners? Are both the listeners registered for the same source?


[How to ask questions] [Donate a pint, save a life!] [Onff-turn it on!]
adeeb alexander
Ranch Hand

Joined: May 29, 2008
Posts: 268
Hi
I have a gui with 4 text fields and a single button. My requirement is that when ever the user press the enter key the button should be pressed. At present the user should set the focus on the button using TAB then press enter i want the button to perform action when the enter key is pressed regardless of the focus. I have tried this. But does not satisfy me.


KeyboardFocusManager.getCurrentKeyboardFocusManager()
.addKeyEventDispatcher ( new KeyEventDispatcher() {
public boolean dispatchKeyEvent(KeyEvent e){
int key = e.getKeyCode();
if(key == 13) System.out.println("Done:");
return false;}


and the other one i tried is this. It works but only after getting the focus. Can i have the JButton permanently focused.


public void keyTyped(KeyEvent e)
{

}

// Invoked when a key has been pressed.
public void keyPressed(KeyEvent e)
{

}

// Invoked when a key has been released.
public void keyReleased(KeyEvent e)
{
if(e.getKeyCode() == KeyEvent.VK_ENTER)
{
System.out.println("Done 2:");
}
}


I am using actionPerformed also. Is it necessary to use actionPerformed when we use the KeyListeners

I have also tried using getRootPane().setDefaultButton(connect);. Here the problem is that only a single button could be used but how shall i do when there are many buttons which are on other panels.

I think this keylisteners are the most tough and i am unable to get any clue to solve this problem. Please help


Thanks and Regards
alexander
adeeb alexander
Ranch Hand

Joined: May 29, 2008
Posts: 268
Here is my whole code


>
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39436
    
  28
Far better to learn how to add a Listener class to each control. The more controls (buttons, etc) you add the bigger the actionPerformed method becomes and the harder it is to maintain.
adeeb alexander
Ranch Hand

Joined: May 29, 2008
Posts: 268
Hi.
My problem is not yet solved. Please suggest me with the easiest way in which i can use the key listeners





Thanks and Regards
alexander
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19723
    
  20

I take it you've read http://java.sun.com/docs/books/tutorial/uiswing/events/index.html already?


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
pete stein
Bartender

Joined: Feb 23, 2007
Posts: 1561
adeeb alexander wrote:Hi.
My problem is not yet solved. Please suggest me with the easiest way in which i can use the key listeners

Don't. As I believe has been mentioned, the issue is not solved this way. Add the same actionlistener that you use in your Connect button to each JTextField.
For example:

Michael Dunn
Ranch Hand

Joined: Jun 09, 2003
Posts: 4632
> I have also tried using getRootPane().setDefaultButton(connect);.
> Here the problem is that only a single button could be used but how shall i do when there are many buttons which are on other panels.

I see you're using a CardLayout, and, presumably you want a particular button on each panel to do things when ENTER is pressed,
one of those things being to switch panels in the cardlayout.

if this is so, simple use the getRootPane().setDefaultButton(connect) in each of the actionListeners, after cl.show(...)

add this at the end of the constructor, for its initial setting
getRootPane().setDefaultButton(connect);

then in actionPerformed


so, basically, you set the default button to the one in the panel being shown.
adeeb alexander
Ranch Hand

Joined: May 29, 2008
Posts: 268
Hi all.
Thanks a lot guyz especial thanks to Michael. Thank you Michael you really got my problem. Actually i should have done it but i dint get this simple idea. Thanks again.
I have used this program to display 18,000 rows but it took nearly 5-7 minutes. Is there any other way, so that i could complete this operation in 2 mins.



Thanks and Regards
alexander
Michael Dunn
Ranch Hand

Joined: Jun 09, 2003
Posts: 4632
> I have used this program to display 18,000 rows but it took nearly 5-7 minutes. Is there any other way, so that i could complete this operation in 2 mins.

your problem is now a JDBC one, so you're probably better off posting a separate question in the JDBC forum.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: How to use method here