gui questions

hi all,

i wanted to know if there is a way to reset a JTextField after im entering a word so it will b blank again automaticly,and how can i limit the number of rows that my Textarea save when im using JScrollPane?

for example in q1 if im writing bla bla and press enter i want the line to b blank automaticly after the word goes to the action listner

what should i add in here?

public void actionPerformed(ActionEvent event) {

}

and q2 means snce im using JScrollPane i have noticed it is keeping all the line i feed into it (Jtextarea.append) how can i limit it to save the last 20 ?

thx and pls forgive my poor english

[672 byte] By [barakyesha] at [2007-11-27 5:36:03]
# 1

q1

public void actionPerformed(ActionEvent event) {

...

...

textfield.setText("");//last line in actionPerformed()

}

q2

http://forum.java.sun.com/thread.jspa?forumID=57&threadID=689486

Michael_Dunna at 2007-7-12 15:06:05 > top of Java-index,Java Essentials,Java Programming...
# 2

To catch an "enter" keypress in a JTextField, you may need to add a KeyListener and watch for KeyCode 10. Here is a small application that will extract the jtextfield's text and put into a string variable if button pressed or if enter typed:

import java.awt.*;

import java.awt.event.*;

import javax.swing.*;

class Foo extends JFrame

{

JPanel pane = new JPanel();

JButton btn1 = new JButton("button1");

JTextField myTextField = new JTextField(20);

String myString = ""; // this will hold the extracted myTextField text.

public Foo() // app constructor

{

super("Extract Text App");

addWidgets();

getContentPane().add(pane);

setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

}

private void addWidgets() // add jpanels with button and jtextfield

{

pane.setLayout(new BorderLayout());

pane.add(makeButtonPanel(), BorderLayout.NORTH);

pane.add(makeTextPanel(), BorderLayout.SOUTH);

}

// create jtextfield and jpanel to hold it

private JPanel makeTextPanel()

{

JPanel textPanel = new JPanel();

textPanel.add(myTextField);

myTextField.addKeyListener(new KeyListener()

{

public void keyPressed(KeyEvent ke)

{

if (ke.getKeyCode() == 10) // enter key pressed

{

doStuffToTextField();

}

}

public void keyReleased(KeyEvent arg0){}

public void keyTyped(KeyEvent arg0){}

});

return textPanel;

}

// create jbutton and panel to hold it

private JPanel makeButtonPanel()

{

JPanel buttonPanel = new JPanel();

buttonPanel.add(btn1);

btn1.addActionListener(new ActionListener()

{

public void actionPerformed(ActionEvent arg0)

{

doStuffToTextField();

}

});

return buttonPanel;

}

// code common to both ActionListener and KeyListener

private void doStuffToTextField()

{

// put myTextField's text into the myString variable

myString = myTextField.getText();

//clear the textfield

myTextField.setText("");

// and display the myString variable contents

JOptionPane.showMessageDialog(null, "myString is now: " + myString);

}

// initialize our app's class and show it

private static void createAndShowGUI()

{

Foo fu = new Foo();

fu.pack();

fu.setVisible(true);

}

public static void main(String[] args)

{

// let's call our frame in a thread-safe manner

javax.swing.SwingUtilities.invokeLater(new Runnable()

{

public void run()

{

createAndShowGUI();

}

});

}

}

petes1234a at 2007-7-12 15:06:05 > top of Java-index,Java Essentials,Java Programming...
# 3

> To catch an "enter" keypress in a JTextField, you may need to add a KeyListener and watch for KeyCode 10.

No, don't use a KeyListener. Using a higher level of Abstraction is always a better choice when you have the option.

The addActionListener() method is to be used explicitly for fhis purpose so you can handle this common requirement as easily as you can by adding an ActionListener to a buton.

camickra at 2007-7-12 15:06:05 > top of Java-index,Java Essentials,Java Programming...
# 4

> No, don't use a KeyListener. Using a higher level of

> Abstraction is always a better choice when you have

> the option.

>

> The addActionListener() method is to be used

> explicitly for fhis purpose so you can handle this

> common requirement as easily as you can by adding an

> ActionListener to a buton.

To camickr, thanks for the correction.

To the OP... listen to camickr!

petes1234a at 2007-7-12 15:06:05 > top of Java-index,Java Essentials,Java Programming...
# 5

> The addActionListener() method is to be used

> explicitly for fhis purpose so you can handle this

> common requirement as easily as you can by adding an

> ActionListener to a buton.

Dumb question, but despite a google search, I couldn't find out how to trap the "enter" key in a JTextField by using an ActionListener. What am I missing?

Thanks in advance.

/Pete

petes1234a at 2007-7-12 15:06:05 > top of Java-index,Java Essentials,Java Programming...
# 6

You don't trap the enter key. The LAF does this for you and generates an ActionEvent. So all you do is add an ActionListenerr to the text field.

Its the same as a button click. You don't trap a MouseClick. The LAF will generate an ActionEvent so you just write your ActionListener and let the LAF generate the correct event.

Take a look at the ListDemo example from the Swing tutorial on [url http://java.sun.com/docs/books/tutorial/uiswing/components/list.html]How to Use Lists[/url]. The same ActionListener is added to the "Hire" button and the "Employee" text field so whether you click on the button or use the enter key on the text field the same code is executed.

camickra at 2007-7-12 15:06:05 > top of Java-index,Java Essentials,Java Programming...
# 7
thank u all 4 the reply it was very usefull
barakyesha at 2007-7-12 15:06:05 > top of Java-index,Java Essentials,Java Programming...