-1

I need help in Java.

Having worked with VB.NET you can understand my predicament below. I am new to Java GUI. I have created a swing form with buttons, Add, Edit, Save. When Add is clicked, I want to set status to TRUE for some global variable (e.g isAdding) and set the same to FALSE when I click Edit button. This is to make me make a decision to call an insert sql script or an Update sql script.

The question is, I am fumbling with constructors and encapsulation and the like to do this. How can I achieve this, very easily as outlined above?

Sylvester
  • 123
  • 1
  • 1
  • 8
  • 1
    possible duplicate of [Modification of variables/other swing objects on button click](http://stackoverflow.com/questions/6473174/modification-of-variables-other-swing-objects-on-button-click) – Paco Abato Dec 08 '14 at 14:11
  • 1
    Check out [swing tutorials on event handling](https://docs.oracle.com/javase/tutorial/uiswing/events/actionlistener.html). – default locale Dec 08 '14 at 14:12

2 Answers2

1

Global state is not a good practice and may cause side effect. You should create a data model that is displayed to your screens. When the model is changed due to a user action for instance then the screen should be updated. If data is entered by the user (text input) then the model must be updated too. To keep model and screen in sync you may implement an Observer pattern since there is no data binding with Swing.

Olivier Meurice
  • 564
  • 7
  • 17
1

In fact, I don't understand what you mean by global variable. But when I do the event handles in JAVA GUI, I usually do like the following way.

class Editeur implements ActionListener{
    Frame theFrame;
    Panel theButtons;
    Button toRead;
    Button toWrite;
   //the other components


    Editeur(){
        super();

    }
    void execute(){
        // initialization
        theFrame.addWindowListener(new WindowAdapter() {
               public void windowClosing(WindowEvent e) {
                System.exit(0);
            }
        });
     }
    public void read() {
        //do somethings
    }
    public void write() {
       //do somethings

    }
    public void actionPerformed(ActionEvent e) {
        //when toRead is clicked, read();toWrite is clicked, write()
        if (e.getSource().equals(toRead)) {
            read();
        }else if (e.getSource().equals(toWrite)){
            write();
        }
        else{
           // do somethings
        }
   }
   public static void main(String[] args){
        (new Editeur()).execute();

   }
}
ysfseu
  • 626
  • 1
  • 9
  • 20
  • this looks something towards the idea, but because I am new to JAVA, I am not understanding its complexity at this stage. – Sylvester Dec 08 '14 at 15:13
  • @Sylvester It's not complex.I just add two button:'Button toRead; Button toWrite;' In the `read`function, add some codes which you want to execute when click toRead.The actionPerformed function is the funtion in the interface ActionListener. – ysfseu Dec 08 '14 at 15:25