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.
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?
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
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 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:
> 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.
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.
> 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.
He got surgery to replace his foot with a pig. He said it was because of this tiny ad:
a bit of art, as a gift, the permaculture playing cards