Connecting JFrames together in Netbeans

Get help on programming - C++, Java, Delphi, etc.
Post Reply
Rajiv
Registered User
Posts: 2178
Joined: 03 Dec 2010, 20:26

Connecting JFrames together in Netbeans

Post by Rajiv »

Hi guys, I am currently working on a project that is based on a meal order scenario. The program must be able to have a welcome screen (where the user can choose whether he/she wants to select the first option by entering the nr '1' and pressing a 'Go' button or whether he/she wants to exit the program), and then if the user chose the number '1' at the welcome screen, an input screen must be displayed where the user can enter all the various order details (this data must be sent to a database) and then an output screen must be displayed showing the relative order placed.

I am new to Netbeans and would like to know how I would be able to code the connection between the welcome screen and the input GUI and from there to the output GUI. Basically, what code would I need to so that if the user enters '1' in the welcome screen and clicks on the 'Go' button, the input GUI would appear.

Any help would be appreciated.
"Because I don't say it...don't mean I ain't thinking it!"
User avatar
rustypup
Registered User
Posts: 8872
Joined: 13 Dec 2004, 02:00
Location: nullus pixius demonica
Contact:

Re: Connecting JFrames together in Netbeans

Post by rustypup »

any JComponent can be swapped into and out of a JFrame content pane using the derived add() and remove() accessors...

the ContentPane's default layout manager is BorderLayout... again can be modified via derived accessors..

Code: Select all

myFrame.getContentPane().add(myPanel, BorderLayout.CENTER);
myFrame.getContentPane().remove(myPanel);
this example assumes you're maintaining an instance of the various JPanel's along with content and connectors.. this only makes sense in excessive reuse applications otherwise it's unnecessary overhead..

focus first on MVC, (model, view, controller)...

also be sure to acquaint yourself with the API... it's your friend...
Most people would sooner die than think; in fact, they do so - Bertrand Russel
Rajiv
Registered User
Posts: 2178
Joined: 03 Dec 2010, 20:26

Re: Connecting JFrames together in Netbeans

Post by Rajiv »

Ok, so this my 'Go' button in the WelcomeGUI code section:

Code: Select all

private void GoBtnActionPerformed(java.awt.event.ActionEvent evt) {                                      
      if (userChoice == 1 && selectedoptionlbl.getText()!=null)
        {
            new InputGUI().setVisible(true);
        }
    }
This doesn't work, but I still don't understand what I need, sorry.
"Because I don't say it...don't mean I ain't thinking it!"
User avatar
rustypup
Registered User
Posts: 8872
Joined: 13 Dec 2004, 02:00
Location: nullus pixius demonica
Contact:

Re: Connecting JFrames together in Netbeans

Post by rustypup »

ok...

you can't connect JFrames because they're heavyweight containers... single instance only...

setVisible() tells the rendering stack to iterate that container and its components on the next rendering call... nothing else... if the panel/container isn't included in any visible component this will have zero impact..

steps are:
1) present JFrame with content pane's root content set to the welcome JPanel instance.
2) Consume the selection event and determine which JPanel to construct in response
3) Construct that JPanel* and swap it in... if this is a one-way process you can discard the "welcome" JPanel instance...

Code: Select all

private void GoBtnActionPerformed(java.awt.event.ActionEvent evt) {                                     
      if (userChoice == 1 && selectedoptionlbl.getText()!=null)
        {
                myFrame.getContentPane().remove(myWelcomeScreen);
                myFrame.getContentPane().add(new InputGUI(), BorderLayout.CENTER);
        }
    }
NB! all the above segment of code will do is introduce a new instance of the InputGUI class into the container's rendering stack. you will have ZERO reference to it or it's events unless it's controller is established elsewhere in the InputGUI's constructor. this is important... if you require a reference in order to bind event handlers, pass a reference not an instance...

*you could always have the two options constructed at execution and discard the superfluous one post selection... this will "appear" to improve performance...

<edit>
unless InputGUI is a JFrame extension?... in which case.. .does it pack() itself?
</edit>
Most people would sooner die than think; in fact, they do so - Bertrand Russel
Rajiv
Registered User
Posts: 2178
Joined: 03 Dec 2010, 20:26

Re: Connecting JFrames together in Netbeans

Post by Rajiv »

Ok thanks Rusty, but I managed to figure the problem out with my dad, but we've run into another problem (I should have created a thread to incorporate everything about his project). One of my classes is a 'ConnectDB' class which contains the code necessary to connect to the database and the other class is my jFrame class, 'InputGUI'. I created an array of type String and made it public in the ConnectDB class, we created a counter that is used to count the records and use it as the size of the array. In that array, we stored the contents of the table (MENUDESCRIPTION). We then want to use that same array, with its contents, in the InputGUI class to populate a combo box. Here is the code we've used so far:

In the ConnectDB class - the declaration of the String array:

Code: Select all

public String [] menuItems;
In the ConnectDB class - storing the contents of the database into the array with the use of a counter:

Code: Select all

public void showMenuItems ()
{
     int menuCount = 0;
     String sql = "SELECT * FROM MENUITEMS";
    System.out.println ("\nMenu items in the database : \n");
    
    try 
    {
        Statement statement = con.createStatement ();
        ResultSet rs = statement.executeQuery (sql);
        
        if (rs != null)
        {
            while (rs.next())
            {
                int menuID = rs.getInt("MENUID");
                String menuDescription = rs.getString ("MENUDESCRIPTION");
                System.out.println (menuID + " " + menuDescription);
                menuCount++;
                menuItems[menuCount] = menuDescription;
                
            }
        }
        rs.close ();
        statement.close();
    }
    catch (Exception ex)
    {
        System.out.println ("Error reading database information!");
        System.out.println (ex);
    }
}
In the InputGUI class - where we want to populate the combo box:

Code: Select all

public void populateMenuListlbl()
    {
      MenuListlbl.setModel(new javax.swing.DefaultComboBoxModel(menuItems));  
    }
How do I parse an array from one class to another class? The error is: cannot find symbol - 'menuItems'.
"Because I don't say it...don't mean I ain't thinking it!"
Post Reply