• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

How to use method here

 
adeeb alexander
Ranch Hand
Posts: 268
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 11096
13
Android Eclipse IDE Google Web Toolkit Java Mac Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
adeeb alexander
Ranch Hand
Posts: 268
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 268
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here is my whole code


>
 
Campbell Ritchie
Sheriff
Pie
Posts: 49808
69
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 268
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 20610
63
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I take it you've read http://java.sun.com/docs/books/tutorial/uiswing/events/index.html already?
 
pete stein
Bartender
Posts: 1561
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 4632
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
> 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
Posts: 268
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 4632
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
> 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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic