Basic GUI Help


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 2 of 2

Thread: Basic GUI Help

  1. #1
    Join Date
    Jun 2004
    Posts
    1

    Basic GUI Help

    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

    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.
    http://deviantone.com/javagui/dummyGui.java

    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:
    Code:
    public void actionPerformed(ActionEvent e) {
        if(e.getSource() == openButton){
          JFileChooser chooser = new JFileChooser();
          int returnVal = chooser.showOpenDialog(dummyGui.this);
          if(returnVal == JFileChooser.APPROVE_OPTION) {
            file = chooser.getSelectedFile();
        }
      }
    }
    But when I run the GUI, and hit Open, the button gets pressed in and then the window freezes while no file browser shows ... =/

  2. #2
    Join Date
    Feb 2004
    Posts
    808
    its' not freezing, you just arent doing things correctly... go back to netbeans

    ps; the comment:

    * WARNING: Do NOT modify this code. The content of this method is
    * always regenerated by the Form Editor.

    should be abided by..

    in netbeans, you make an event do something proper, you use the Events tab in the NetBeans properties editor. choose the appropriate event you wish to hook into, and click in the editable area of the combo, type a nice name or just accept the name that is made for you (e.g. myButton1_actionPerformed()) and press return twice. NetBeans will jump to the source editor allowing you to plug the code into your app

    WARNING: DO NOT EDIT THE initComponents() METHOD. As the name suggests, initComponents is for initing components. it is NOT for putting masses of code logic into button presses. this is why actionListeners generated by netbeans ONLY EVER do one thing; call a named method.

    you should always write your handling code in the named method, NEVER in the actual body of the actionPerformed() method, inside initCOmponents()

    lastly, your class is called dummyGui
    To obey class naming conventions, it should be called DummyGUI. do not start class names with a lowercase letter - it makes them look like variables to experienced programmers like me
    The 6th edict:
    "A thing of reference thing can hold either a null thing or a thing to any thing whose thing is assignment compatible with the thing of the thing" - ArchAngel, www.dictionary.com et al.
    JAR tutorial GridBag tutorial Inherited Shapes Inheritance? String.split(); FTP?

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center