Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 10 of 10

Thread: A program with a GUI

  1. #1
    Junior Member
    Join Date
    Jan 2010
    Posts
    13
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default A program with a GUI

    I have an (a subclass to) AbstractTableModel which contains an ArrayList of type Item (a home-made class) and some basic methods such as add, remove,getColumnCount, getRowCount and getValueAt. This extension of AbstractTableModel has been put in a JTable, which has been put in a JScrollPane and in a JFrame for viewing.

    What I am trying to create now in the GUI is a function for the user to add Items to the ArrayList so that they will appear shortly after in the JTable.

    In overly simplified pseudocode, this function would look something like:

    1.User presses button1 which is labeled "create new.."
    2.Dialog box with text fields pops up
    3.the input is used to create a new instance
    4.the add-method is called with that instance.

    How do I make this happened?
    I have created a class for handling events.(in param: ActionEvent event). So far that class does nothing. I guess I need some kind of conditional statement like: if(button1 == event.getSource()) and then something that makes a dialog pop up.

    I've read tons of info on Jdialog and JOptionPane, the classes suggested to use for the dialog. However, I am unable to make anything happened. Please dont just post links to some page at sun microsystems (it feels like I've read them all.)

    For example, how would a dialog box even be able to appear in the first place? Shouldn't the method that makes it pop up be in the main method that actually starts the program?

    What I would like as a response to this is some code that easily just could be put in after my if statement and makes a simple dialog box appear. Some clarifying advice would also be nice.



    PHP Code:
    public class EventHandler implements ActionListener{
            
            public 
    void actionPerformed(ActionEvent event){
                
                
                if (
    event.getSource()== button1){
                    


                }
                
                
            }
        } 


  2. #2
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,262
    Thanks
    176
    Thanked 821 Times in 764 Posts
    Blog Entries
    5

    Default Re: A program with a GUI

    You just need to create a JOptionPane and retrieve the output...the JOptionPane handles all the intricacies. Grab the returning string, then create a new item object based upon that input, and add the item to your list

    String s = (String)JOptionPane.showInputDialog("Please enter new item a value:");//create an option pane
    if ( s != null ){//s will be null if cancel is pressed, otherwise use it to create a new item
    //validate the user input if necessary
    //create your new item based upon s
    //add your new item
    tableModel.fireTableDataChanged();//assuming tableModel is the name of your model - necessary to let the table know its data has changed
    }

  3. The Following User Says Thank You to copeg For This Useful Post:

    joachim89 (January 15th, 2010)

  4. #3
    Junior Member
    Join Date
    Jan 2010
    Posts
    13
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: A program with a GUI

    Is it possible for me to create my own JOptionPane? I need more than one string to pass on to the constructor.(The constructor takes like 6 parameters of 3 different types). Of course I could have the program read it like one string and extract different substrings and parse them, but I would really prefer to have it customized like "enter title:___, enter year:____" etc.
    Also, the code you posted did not compile, I got the error message "Cannot make a static reference to the non-static method fireTableDataChanged() from the type
    AbstractTableModel" How do I get rid of that?

  5. #4
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,262
    Thanks
    176
    Thanked 821 Times in 764 Posts
    Blog Entries
    5

    Default Re: A program with a GUI

    Quote Originally Posted by joachim89 View Post
    Also, the code you posted did not compile, I got the error message "Cannot make a static reference to the non-static method fireTableDataChanged() from the type
    AbstractTableModel" How do I get rid of that?
    This is because you need to call fireTableDataChanged on a reference to your abstract table model...I'm guessing you are calling it in a static context (eg AbstractTableModel. fireTableDataChanged() is a static context and won't compile).

    Quote Originally Posted by joachim89 View Post
    Is it possible for me to create my own JOptionPane? I need more than one string to pass on to the constructor.(The constructor takes like 6 parameters of 3 different types). Of course I could have the program read it like one string and extract different substrings and parse them, but I would really prefer to have it customized like "enter title:___, enter year:____" etc.
    It sounds like you need a much more comprehensive interface than JOptionPane supplies. You can create a new JFrame to do so, sending it a reference to the JFrame with the table in it so you can update the table from that reference. Another way which may be easier is - when the add item button is fired - add an empty item to your list, and prompt the user to enter the information directly into the new row in the table. You can do so by:
    Item item = new Item();//empty item
    //add the item to the list
    //call fireTableDataChanged on your TableModel
    //call editCellAt specifying the new entry where you wish users to enter info
    Of course your table needs to be editable for this to be an option

  6. The Following User Says Thank You to copeg For This Useful Post:

    joachim89 (January 15th, 2010)

  7. #5
    Junior Member
    Join Date
    Jan 2010
    Posts
    13
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Unhappy Re: A program with a GUI

    Ok, I decided to make my own JFrame instead.
    But still I can't get it to run just for a quick test.

    This code gives me the error message that the method setVisible is undefined for the type PopupWindow. WHAT THE H*** IS WRONG HELP ME PLEASE
    What's strange is that in the actual PopupWindow class I get no such errors.

    PHP Code:
    public class EventHandler implements ActionListener{
                    
    PopupWindow window = new PopupWindow();
            public 
    void actionPerformed(ActionEvent event){
                
                
                
    window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                
    window.setSize(400,80);
                
                
                
                if(
    event.getSource() == button1){
                    
    window.setVisible(true);
                    
                }
            

                }
                
                
            } 
    Last edited by joachim89; January 15th, 2010 at 06:06 PM.

  8. #6
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,262
    Thanks
    176
    Thanked 821 Times in 764 Posts
    Blog Entries
    5

    Default Re: A program with a GUI

    Quote Originally Posted by joachim89 View Post
    This code gives me the error message that the method setVisible is undefined for the type PopupWindow.
    Make sure your PopupWindow extends JFrame. If it does you will probably have to post the code to that object because its tough to debug without knowing that info

  9. #7
    Junior Member
    Join Date
    Jan 2010
    Posts
    13
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: A program with a GUI

    I decided to create a JFrame with data fields.

    Here's an EventHandler class that reads input from the JFrame dialog box.
    (createNew is a JButton)
    PHP Code:
    public class EventHandler implements ActionListener{
            public 
    String titleproducerpubYeargenre;
            public 
    int lengthageLimit;
            
    Film addFilm;
            public 
    void actionPerformed(ActionEvent event){
                
                if(
    event.getSource() == createNew)
                  
    addFilm = new Film(field1.getText(),field2.getText(),false,field3.getText(),Integer.parseInt(field4.getText()),Integer.parseInt(field6.getText()),field5.getText());
                    
                        }
        } 
    So this is supposed to take enough info to create a Film object. Now i want to add this to my Items ArrayList (Film is a subclass to Item). The arraylist is inside a subclass(called LendingModel) to AbstractTableModel, which is in a JTable in a JFrame.
    But as I try to add the new object to my LendingModel object (lm) in the EventHandler class posted above, it does not recognize lm. (Error message: lm cannot be resolved to a type).
    So where do I add it? Or is it the right place to add it?

    I really need some help with this, thanks in advance.

  10. #8
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,262
    Thanks
    176
    Thanked 821 Times in 764 Posts
    Blog Entries
    5

    Default Re: A program with a GUI

    The compiler needs a reference to your ArrayList object. I don't know how your project is organized, but however you do it your EventHandler class must know of your list through a reference. You can pass it the list via the constructor, have your previous JFrame window act as the actionListener, or any number of other designs

  11. #9
    Junior Member
    Join Date
    Jan 2010
    Posts
    13
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Unhappy Re: A program with a GUI

    I made a method called addToList that takes an ArrayList and an object as parameters. Evidently it it supposed to add the object to the list. My class for the dialog box looks like this now:


    PHP Code:
    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.util.ArrayList;
    import javax.swing.table.AbstractTableModel;
    public class 
    NewFilmDialog extends JFrame{
        protected 
    JLabel label1;
        protected 
    JTextField  field1;
        protected 
    JLabel label2;
        protected 
    JTextField  field2;
        protected 
    JLabel label3;
        protected 
    JTextField  field3;
        protected 
    JLabel label4;
        protected 
    JTextField field4;
        protected 
    JLabel label5;
        protected 
    JTextField field5;
        protected 
    JLabel label6;
        protected 
    JTextField field6;
        
        public 
    JButton createNew;
        
        public 
    NewFilmDialog(){
            
    super("Ny Film");
            
    setLayout(new FlowLayout());
            
    label1 = new JLabel("    Title:        ");
            
    field1 = new JTextField(10);
            
    label2 = new JLabel("    Producer:     ");
            
    field2 = new JTextField(10);
            
    label3 = new JLabel(   " PublicationYear: ");
            
    field3 = new JTextField(10);
            
    label4 = new JLabel("    length:        ");
            
    field4 = new JTextField(10);
            
    label5 = new JLabel("     Genre:      ");
            
    field5 = new JTextField(10);
            
    label6 = new JLabel("     Age limit:  ");
            
    field6 = new JTextField(10);
            
    createNew = new JButton("Create new");
            
            
    add(label1);
            
    add(field1);
            
    add(label2);
            
    add(field2);
            
    add(label3);
            
    add(field3);
            
    add(label4);
            
    add(field4);
            
    add(label5);
            
    add(field5);
            
    add(label6);
            
    add(field6);
            
    add(createNew);
        
    EventHandler handler = new EventHandler(); // Here I get compilation error,"The constructor NewFilmDialog.EventHandler() is 
        // undefined"
        
    createNew.addActionListener(handler);
        
        }

        public class 
    EventHandler implements ActionListener{
            public 
    String titleproducerpubYeargenre;
            public 
    int lengthageLimit;
            
    Film addFilm;
            
            public 
    EventHandler(AbstractTableModel table){
                
            }
            
            
            public 
    void actionPerformed(ActionEvent event){
                
                if(
    event.getSource() == createNew)
                  
    addFilm = new Film(field1.getText(),field2.getText(),false,field3.getText(),Integer.parseInt(field4.getText()),Integer.parseInt(field6.getText()),field5.getText());
                    
    addToList(lm,addFilm); // Here I get the error" lm cannot be resolved"
            
    }
        
        public 
    void addToList(ArrayList list, Object o){
            list.
    add(o);
            }
        
        }

        
            
        
        
        

    But the problem remains, lm cannot be resolved according to Eclipse. I understand that it needs a reference, I just can't see how I could change the code ao it does that.

    One more time: lm is an object of the type LendingModel, which is an AbstractTableModel.

    Thanks copeg and anyone who may help me.
    Last edited by joachim89; January 17th, 2010 at 04:30 PM.

  12. #10
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,262
    Thanks
    176
    Thanked 821 Times in 764 Posts
    Blog Entries
    5

    Default Re: A program with a GUI

    You do not define what 'lm' is anywhere else in the object, which is why you are getting the error. As mentioned earlier you need to let the class know what is what...As an example of how to accomplish this, you can pass your LendingModel reference to the constructor of the new frame:

     
    private LendingModel lm = null;//define the reference
    //update your constructor
    public NewFilmDialog(LendingModel lm){
        //construct
        this.lm = lm;
        ///etc...
    }
    //etc...