-2

I have an application that is developed in Java swing and the NetBens 7 IDE

Steps:

  1. I want to use a Jbutton to perform two different functions depending on the user mode. for example I want to label a single button with the following text "New Record" and "Exit New Record"

  2. The default text is the "New Record". This will enable the user enter new record.

  3. Whiles in the new record mode, the text on the jButton changes to "Exit New Record". To exit the new record mode the user clicks on the same button to exit.

  4. This will then change the text on button to the default enter "Enter New Record"

Is there any suggestion on how to do this with the netbeans IDE or do I manually override a method?

psubsee2003
  • 8,563
  • 8
  • 61
  • 79
faisal abdulai
  • 3,739
  • 8
  • 44
  • 66

2 Answers2

1

Implement Action Listener on JButton (code not tested, just for your hint):

public class MyButton extends JButton implements ActionListener{
boolean pressed = false;
public MyButton(String name){
    super(name);
    this.addActionListener(this);
    }
@Override
public void actionPerformed(ActionEvent e){
    if(pressed){
    pressed = !pressed;
    _change_text_on_button_
    _do_job_
    }
}

Than use customized MyButton.

1ac0
  • 2,875
  • 3
  • 33
  • 47
  • 1
    no: a) don't subclass JSomething b) overriding the actionPerformed will have severe sid-effects. Instead, configure the button with a custom action and change the name property of that action when appropriate – kleopatra Mar 23 '13 at 10:28
  • @kleopatra not subclassing JButton (or any other swing components?)? can you post some code where subclassing JButton and override actionPerformed() has some side efects? – 1ac0 Mar 23 '13 at 10:51
  • my first comment isn't quite right: had assumed the actionPerfomed to be a method in AbstractButton when in fact its the actionListener implemented in the myButton - so no side-effects, just plain crap :-) Think about this: what do you expect to happen when you click the button? And what happens? The moral: use the components as they are supposed to be used ... – kleopatra Mar 23 '13 at 11:16
  • @kleopatra can you post some code where code should has some side-efect? up to now no problem with overriding JButton and reimplementing actionPerformed(). – 1ac0 Mar 23 '13 at 11:22
  • @kleopatra yes, this is why some code should be usefull to see some side efect of reimplementing actionPerformed() – 1ac0 Mar 23 '13 at 11:31
  • repeating: _no side-effects, just plain crap_ ;-) and that's the last I'll say on this: it's up to you to do some research. Hint: write a small example app which uses it (_and_ behaves as you expect, which you still didn't specify) and compare that to button's normal usage – kleopatra Mar 23 '13 at 11:37
  • @kleopatra now and in the past working on more than ten apps (bank and telco industry) where this aproach is used without any problem, it is used with any programmer i see so that is why asking code. can you post your answer to question please or edit my answer please? – 1ac0 Mar 23 '13 at 11:43
  • strongly doubt that - anyway, it's _your_ turn to show how to use _your_ abnormal code, not mine to show you how to normally use buttons (there are enough tutorials :-) – kleopatra Mar 23 '13 at 11:46
  • @kleopatra do you mean [this tutorial](http://docs.oracle.com/javase/tutorial/uiswing/components/button.html) and [this demo code](http://docs.oracle.com/javase/tutorial/displayCode.html?code=http://docs.oracle.com/javase/tutorial/uiswing/examples/components/ButtonDemoProject/src/components/ButtonDemo.java)? – 1ac0 Mar 23 '13 at 11:54
1

As shown in examples here and here, a button's text can be changed in its ActionListener. The NetBeans GUI editor generates the code to invoke the ActionListener, but it lets you edit the code in the method that is called. The method name will be something like nameActionPerformed().

See also How to Use Buttons, Check Boxes, and Radio Buttons and this suggestion.

Community
  • 1
  • 1
trashgod
  • 203,806
  • 29
  • 246
  • 1,045