I am having problems clearing contents of TextField in AWT using setText() method. Apparently, setText("") does not clear the contents of the TextField on pressing the 'Reset' button. Here's my program:
import java.awt.*;
import java.awt.event.*;
public class form extends Frame
{
Label lbl = new Label("Name:");
TextField tf = new TextField();
Button btn = new Button("Reset");
public form()
{
tf.setColumns(20);
addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent e)
{
System.exit(0);
}
});
btn.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
tf.setText(""); //Problem occurs here. This does not clear the contents of the text field on pressing the 'Reset' button.
}
});
add(lbl);
add(tf);
add(btn);
setLayout(new FlowLayout());
setSize(400,100);
setVisible(true);
setTitle("Form");
}
public static void main(String[] args)
{
new form();
}
}
Can someone please tell me where I went wrong or suggest an alternative? Thanks.
I see the problem as well using Java 8u11. I seem to remember this being filed as a known bug, but I can't seem to find it now.
A solution that works for me is to add an intermediate step:
public void actionPerformed(ActionEvent e) {
tf.setText(" ");
tf.setText("");
}
I'm not sure why this is necessary, I think it's a bug with the setText() function specifically ignoring empty Strings. If somebody finds the filed bug there would be more information there.
Add space in setText(" ") in function and see if it works. But there after there will be one space.
Related
I am new to GUI based java programming and would like your help with my query.
import java.awt.*;
import java.awt.event.*;
class Panels
{
static Frame f; static TextArea t;
public static void main(String...xyxxcxcx)
{
f=new Frame();
f.setLayout(new FlowLayout());
f.setSize(400,300);
t=new TextArea(); Button b=new Button("Select All");
f.add(b); f.add(t);
t.setText("step into the ring with the game");
b.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
t.selectAll();
}
}
);
f.setVisible(true);
}
}
When I click "Select All" button, I expect that the text present in the text area would be selected. However this is not happening. Please advise.
It likely is happening, the TextAreabut the button has focus, so you never see it. If you tabbed over to the TextArea, you'd likely see all the text selected.
what if you called
public void actionPerformed(ActionEvent e) {
t.selectAll();
t.requestFocusInWindow();
}
Question: why use the AWT library? It's about 20 years out of date.
I have a problem in my program. Here's my code:
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;`
public class click_rep extends JFrame{`
public click_rep(){
super("CLICK");
final JButton btn1 = new JButton("CLICK HERE");
final JLabel label = new JLabel();
FlowLayout flo = new FlowLayout();
setLayout(flo);
add(btn1);
setSize(315,120);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);
btn1.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
try{
String command = e.getActionCommand();
if (command.equals(btn1)){
label.setText("CLICK");
setVisible(true);
}
}catch(Exception e1){
e1.printStackTrace();
}
}
});
}
public static void main(String[] a){
click_rep cp = new click_rep();
}
}
My problem is that the ActionEvent won't appear. What will I do to make the ActionEvent appear?
Hopefully someone can help me. Thanks for the help.
Take a closer look at this...
String command = e.getActionCommand();
if (command.equals(btn1)){
command is a String and btn1 is a JButton, when are they likely to ever be equal?
There are a few ways you might fix it, you could, for example, do something like this...
if ("CLICK HERE".equals(command)) {
Or something like this...
if (e.getSource() == btn1) {
But I prefer the first one...
But, because the ActionListener is an annoymouse listener registered to btn1, the source of the event can never be anything other then btn1, so you could simply do something like this instead...
btn1.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
label.setText("CLICK");
// Not sure what this is meant for, as the component
// must already be visible in order for the user to
// activate the button...
setVisible(true);
}
});
I have the following code. How would I make a textbox displaying the text on each button come up when that button is pushed? I'm taking a beginning java class online that gives little to no instruction and then asks me to do stuff like this, so any and all help is very much appreciated. I want to learn! Thank You!
import java.awt.*;
public class FinalProj2 extends Frame
{
FinalProj2()
{
setTitle("Buttons");
setSize(600,600);
show();
}
public static void main(String args[])
{
Frame objFrame;
Button objButton1;
Button objButton2;
Button objButton3;
Label objLabel2;
objFrame= new FinalProj2();
objButton1= new Button("Submit");
objButton2= new Button("Cancel");
objButton3= new Button("What Now");
objLabel2= new Label();
objButton1.setBounds(60,200,80,80);
objButton2.setBounds(150,300,80,80);
objButton3.setBounds(60,400,80,80);
objFrame.add(objButton2);
objFrame.add(objButton1);
objFrame.add(objButton3);
objFrame.add(objLabel2);
}
}
Attach an ActionListener using addActionListener() method on each of the buttons instances you need. In actionPerformed()method text in textbox
btn.addActionListener( new ActionListener() {
public void actionPerformed(ActionEvent e) {
lbl.setText(btn.getLabel());
}
});
If I understand you correctly, You want that when you press any button then the button label should be displayed in TextField
For this you have to create an object of TextField like:
final TextField box = new TextField();
Then you can add ActionListener on that TextField like:
objButton1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
box.setText(objButton1.getLabel());
}
});
Same for other buttons.
How can I call a method by pressing a JButton?
For example:
when JButton is pressed
hillClimb() is called;
I know how to display messages etc when pressing a JButton, but want to know if it is possible to do this?
Many thanks.
If you know how to display messages when pressing a button, then you already know how to call a method as opening a new window is a call to a method.
With more details, you can implement an ActionListener and then use the addActionListener method on your JButton. Here is a pretty basic tutorial on how to write an ActionListener.
You can use an anonymous class too:
yourButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
hillClimb();
}
});
Here is trivial app showing how to declare and link button and ActionListener. Hope it will make things more clear for you.
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
public class ButtonSample extends JFrame implements ActionListener {
public ButtonSample() {
setDefaultCloseOperation(EXIT_ON_CLOSE);
setSize(100, 100);
setLocation(100, 100);
JButton button1 = new JButton("button1");
button1.addActionListener(this);
add(button1);
setVisible(true);
}
public static void main(String[] args) {
new ButtonSample();
}
#Override
public void actionPerformed(ActionEvent e) {
String command = e.getActionCommand();
if (command.equals("button1")) {
myMethod();
}
}
public void myMethod() {
JOptionPane.showMessageDialog(this, "Hello, World!!!!!");
}
}
Fist you initialize the button, then add ActionListener to it
JButton btn1=new JButton();
btn1.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
hillClimb();
}
});
You need to add an event handler (ActionListener in Java) to the JButton.
This article explains how to do this.
btnMyButton.addActionListener(e->{
JOptionPane.showMessageDialog(null,"Hi Manuel ");
});
with lambda
In the code sample below, if a user changes the contents of the JFormattedTextField then presses Enter, the dialog is supposed to act as if the OK button is pressed. But it takes two presses of Enter for this to happen.
The plain vanilla JTextField always acts as I would expect - changing the text then pressing Enter activates the OK button straight away.
This in on Mac OS X 10.6 with the current Mac Java update 1.6.0_20.
Is this a work-around? Is this a Mac specific problem?
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.text.NumberFormat;
import java.text.ParseException;
public class ScratchSpace {
public static void main(final String[] args) throws ParseException {
final JDialog dialog = new JDialog((Frame) null, "Test", true);
dialog.setLayout(new FlowLayout());
dialog.add(new JLabel("text field: "));
dialog.add(new JTextField(20));
dialog.add(new JLabel("formatted text field: "));
final JFormattedTextField formattedTextField = new JFormattedTextField(NumberFormat.getIntegerInstance());
formattedTextField.setValue(42);
formattedTextField.setColumns(20);
dialog.add(formattedTextField);
final JButton okButton = new JButton(new AbstractAction("OK") {
public void actionPerformed(ActionEvent e) {
dialog.dispose();
}
});
dialog.add(okButton);
dialog.getRootPane().setDefaultButton(okButton);
dialog.pack();
dialog.setLocationRelativeTo(null);
dialog.setVisible(true);
}
}
Adding this code solved the problem,
formattedTextField.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
dialog.dispose();
}
});
This did not solve my problem. However, it seemed that the problem solution was much simpler for me:
private void jTextField1ActionPerformed(java.awt.event.ActionEvent evt) {
add(jTextField1); //this reacts after the "ENTER" gets pressed
jButton1.doClick(); //this activates the button
jTextField1.setText(""); //this removes the text from a text-field
jTextField1.grabFocus(); //this sets a cursor within a text-field
}