• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
  • Mikalai Zaikin

Basic GUI help

Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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).

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:

Now the hard part, the GUI from NetBeans... the syntax is a little bloated since it's machine generated, but shouldn't be too hard to figure out.

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 ... =/
Ranch Hand
Posts: 1535
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
    Bookmark Topic Watch Topic
  • New Topic