I'm working on a big GUI, using NetBeans since it simplified the initial container design... but now I need to link all the components to actions and events... which I'm not very experienced with.
I made a dummy GUI that does similar, but far fewer functions like my real project. Can someone help me design the actions/events for this little dummy gui, and explain how they work, so I can use that as a guide...?
The dummy project has two java files. A Scan class (Scan.java) will be used to parse a 'patient' text file, and create a String with info from that file (Scan.patient), as well as store the original pathname (Scan.path). http://deviantone.com/javagui/Scan.java
You can create a Scan object by giving the constructor a File or String (file path) argument. You can then call the patient or path vars. The dummy 3 line patient file: http://deviantone.com/javagui/dummy.txt
The GUI is meant to parse whatever text file opened (for the purposes of this dummy gui, only dummy.txt will ever be browsed to, since the program can handle only 3 lines) into C:\output.txt (which you can create and leave blank beforehand).
What I need:
(1) The Open button (JButton openButton) needs to call a file chooser, where a user can browse to our dummy text file. dummyGui.java should then make a Scan object using this file.
(2) Once opened, the file path should appear in the JLabel pathLabel (you can use another gui component instead of a JLabel if you want, I just didn't know what would be the appropriate one for this sort of operation)
(3) You can select Yes/No or input some text into the combo box (JComboBox statusCombo) as well as input stuff into a text field, let's say a date (JTextField dateField). This information will also be written into output.txt
(4) Once you hit the Write button, Scan.patient, and the contents of statusCombo and dateField should be written into output.txt (it doesn't matter what line contains what, for simplicity maybe make statusCombo first, then dateField, then Scan.patient which is 2 lines)... I just need to see how to make a button do such an operation using another Object (Scan) as well as user input...
So far, I have looked through the API and File Chooser examples, and I've added the following to my code:
- instance variable to the dummyGui.java: File file; for the file that file chooser will get. - also added openButton.addActionListener(this); to the initComponents() method. - and another method:
But when I run the GUI, and hit Open, the button gets pressed in and then the window freezes while no file browser shows ... =/
Some of the fun in java is in how you put things together. There are many ways to do what you are wanting to do. This is one way, a suggestion, a place to start exploring possibilities.
Here I'm using another class, ButtonListener, to handle the button events. This isolates the event code from the gui code and (is an attempt to) make what we're doing easier to read, understand and maintain (in the future). Instantiate a single instance of ButtonListener and register its reference as the listener for the two buttons in dummyGui. Send back a reference to the dummyGui class in the ButtonListener constructor so we can access variables in dummyGui from within ButtonListener. To acces statusCombo and pathLabel I demoted them from their private status. You can use get methods to avoid this demotion.
Inside ButtonListener we do all the activity of opening a file dialog, updating pathLabel, gathering data and writing to the output file.
Back in dummyGui note the setActionCommand calls for each JButton. This is a way of avoiding the need to obtain a reference to the buttons within ButtonListener. Obtaining a reference would be easy enough to do:
anyway and of course would require get methods or the demotion (from private access) as before.