简体   繁体   中英

How do JButtons in advanced applications work?

How do buttons in software written in Java work?

http://i.stack.imgur.com/fCbRQ.png

For example the above screenshot: when the user clicks different buttons, different algorithms are run on user-inputted data (it's a data analysis application) and the output is displayed. Just getting started writing Java GUI's though, it all seems like magic to me -- is there one ActionListener for every pane? Does it listen for different ActionCommands of the different buttons and execute the algorithm right within the actionPerformed() method (it seems a little nonintuitive to me to execute an algorithm in a method independent of data...ie the button doesn't know what data it's dealing with?). So far, all the action listener tutorials I've read online have merely printed something when the button is pressed...

What's the general structure for connecting button, actionlisteners, and actual actions performed in the background?

Thanks in advance.

The usual way is to have one action listener per button. The Statistics panel has access (via one of its fields), to the data it needs to read and modify). So, the handling of the first button in this panel could look like:

private void initButtonListeners() {
    this.averageDegreeButton.addActionListener(new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent e) {
            computeAverageDegree();
        }
    }

    // other buttons...
}

And the computeAverageDegree() method could look like

private void computeAverageDegree() {
    double result = this.statistics.computeAverageDegree();
    this.averageDegreeLabel.setText(formatDoubleToString(result));
}

My personal preference is to do almost nothing in the UI, but move it all to the model/controller side (not sure what the best name is as it is seldom pure MVC).

I think that everything you do in the UI should be doable through the API as well. Benefits are easier testing, redesign of the UI is possible without messing up your logic, easy to perform the heavy work on background threads, ... .

A good read describing this is the Humble Dialog article. Not really Swing specific, but applicable to all sort of UI's.

To answer your questions:

is there one ActionListener for every pane?

No, typically you have an Action (or ActionListener ) for each button. I prefer to use Action instances as they are far more reusable then the typical anonymous ActionListener (and easier to test as well)

Does it listen for different ActionCommands of the different buttons and execute the algorithm right within the actionPerformed() method

Certainly not. Doing heavy calculations in that method will block the Swing UI thread (the Event Dispatch Thread), which results in a non-responsive UI while the calculations are ongoing. Showing progress becomes also impossible. Calculations are typically done on a worker thread, launched when your Action is triggered (for example using a SwingWorker ). This is explained in the Concurrency in Swing tutorial.

it seems a little nonintuitive to me to execute an algorithm in a method independent of data...ie the button doesn't know what data it's dealing with?

The button should not know about the data. The data is typically stored in the model. The UI is only displaying it, but does not contain it (unless it is input just provided by the user). The button should just know what to call on the model. The model does whatever it has to do and fires an event. The UI picks up that event and updates itself.

At least, that is how Swing is designed (for example JTable and its TableModel ). I so no good reason to not follow that model when making your own Swing UI's

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM