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]

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();
}
});
}
}
> 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!