0

this is my code which is about postfix expression evaluator, and it works great, but it just work when I click evaluate button.

I want the code works also when I click 'Enter' key

public void init(){
    setLayout(new BorderLayout());
    label1 = new JLabel("Enter a postfix arithmetic expression, then press Enter or click Evaluate. Expression");
    add("North",label1);
    Panel p = new Panel();
    p.setLayout(new FlowLayout());
    input = new JTextField(50);
    input.setSize(300, 20);
    p.add(input);
    label2 = new JLabel("Result : ");
    p.add(label2);
    label3 = new JLabel("");
    label3.setSize(30,20);
    p.add(label3);

    add("Center", p);

    Panel p2 = new Panel();
    p2.setLayout(new FlowLayout());
    b1 = new JButton("Evaluate");
    p2.add(b1);
    b2 = new JButton("Clear");
    p2.add(b2);
    add("South", p2);
    b1.addActionListener(this);
    b2.addActionListener(this);
    setSize(800,150);
}
public void actionPerformed(ActionEvent ae){
    if(ae.getActionCommand()=="Evaluate"){
        String post = input.getText();
        String ans = compute(post);
        label3.setText(ans);
        System.out.println(label3.getText());
        System.out.println(ans);
    }
    else if(ae.getActionCommand()=="Clear"){
        input.setText("");
        label3.setText("");
    }
 }
}

Thanks

MadProgrammer
  • 343,457
  • 22
  • 230
  • 366
khaled
  • 31
  • 6
  • 1
    [How do I compare strings in Java?](http://stackoverflow.com/questions/513832/how-do-i-compare-strings-in-java) – MadProgrammer Jan 24 '16 at 01:55
  • 3
    You could use [`JRootPane#setDefautButton`](https://docs.oracle.com/javase/tutorial/uiswing/components/rootpane.html) or the [Key Bindings](http://docs.oracle.com/javase/tutorial/uiswing/misc/keybinding.html) API – MadProgrammer Jan 24 '16 at 01:57
  • Should also point out, when the button is focused, it will (if the look and feel supports it) respond to the enter (and possibly the space bar) key – MadProgrammer Jan 24 '16 at 01:58
  • @camickr So maybe something like [this](http://stackoverflow.com/questions/13731710/allowing-the-enter-key-to-press-the-submit-button-as-opposed-to-only-using-mo/13731817#13731817) or [this](http://stackoverflow.com/questions/19804717/calling-actionevent-from-within-keyevent-or-any-other-listener/19804969#19804969) or [this](http://stackoverflow.com/questions/27458635/add-enter-key-as-jbutton-accelerator/27458657#27458657) might be better duplicates (but I'm sure you have few of your own examples ;)) – MadProgrammer Jan 24 '16 at 02:02
  • `Panel p = new Panel();` - Don't use Panel, that is AWT. Use JPanel with Swing. `add("Center", p);` - that method signature has been obsolete since JDK1.1. Instead use: `add(p, BorderLayout.CENTER)`. – camickr Jan 24 '16 at 02:11
  • @camickr I define AWT and Swing on my code, so I use both of 'em , but now I want solution for how to use 'Enter' key – khaled Jan 24 '16 at 02:15
  • @MadProgrammer unfortunately the links was not helpful – khaled Jan 24 '16 at 02:16
  • @khaled Which ones? `setDefaultButton` is the normal way to achieve your goal and the key bindings API is more of a specalised approach – MadProgrammer Jan 24 '16 at 02:17
  • `so I use both of 'em ,` - that was my point. Don't use both of them. For a Swing application you should ONLY use Swing compnents!!! – camickr Jan 24 '16 at 02:18
  • @camickr ok what is the problem with use both of 'em with hitting 'enter' key !! – khaled Jan 24 '16 at 02:25
  • It has nothing to do with hitting the Enter key (that is why I added it as a comment and not an answer). It has to do with how to write proper Swing code. A Swing application should use Swing components or you may have unexpected problems in the future. Learn how to use the API properly from the start. Read the [Swing tutorial](http://docs.oracle.com/javase/tutorial/uiswing/TOC.html) and learn from the examples in the tutorial. – camickr Jan 24 '16 at 02:27
  • @MadProgrammer I did public JButton getDefaultButton(){ return b1; } but doesn't work – khaled Jan 24 '16 at 02:32
  • @khaled Can you point me to a location in any of the tutorials or examples which suggested writing a `getDefaultButton` method? As to camickr's comments, [Enter] isn't always the "accept" button on all platforms – MadProgrammer Jan 24 '16 at 02:38
  • @MadProgrammer this link https://docs.oracle.com/javase/8/docs/api/javax/swing/JRootPane.html#setDefaultButton-javax.swing.JButton- – khaled Jan 24 '16 at 02:39
  • @khaled Your example code extends from a `JRootPane`, so overriding `getDefaultButton` won't do anything, if you are extending from `JRootPane`, then don't, this is not how it should be used and you're not likely apply the instance of your root pane to your `JFrame`, [this example](http://stackoverflow.com/questions/13731710/allowing-the-enter-key-to-press-the-submit-button-as-opposed-to-only-using-mo/13731817#13731817) shows one way to set the `defaultButton` property. – MadProgrammer Jan 24 '16 at 02:45
  • You should also beware that if a component which has focus consumes the [Enter] key, this still might not work and you might need to apply a `mnemonic` to your `JButton` – MadProgrammer Jan 24 '16 at 02:46
  • @MadProgrammer Was the first link a sarcasm? Or is there something useful? – dryairship Jan 24 '16 at 11:57
  • @MadProgrammer Yes we need something helpful , I tried it , and it doesn't work , do you have a simple function code that I can use it without sending links – khaled Jan 24 '16 at 16:18
  • `SwingUtilities.getRootPane(submitButton).setDefaultButton(submitButton);` – MadProgrammer Jan 24 '16 at 20:19

0 Answers0