How to display many frames in java

  • (2 Pages)
  • +
  • 1
  • 2

16 Replies - 1531 Views - Last Post: 23 November 2011 - 01:46 PM Rate Topic: -----

#1 Dreamer43  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 17
  • Joined: 20-November 11

How to display many frames in java

Posted 20 November 2011 - 01:13 PM

I just want to ask....If i have a frame with 2 buttons ( GO BACK and NEXT) and i want to display a new frame when "NEXT" is pressed and i want to display completely different design to each frame??? can i do this in 1 class??

I have to do a flight booking system, but i have created many GUI classes so i can handle each frame...Is that wrong ??

I have tried to do it in 1 class with inner classes for each frame. But my GUI class got massive and thats bad for coupling and cohesion.

Thanks in advance.

Is This A Good Question/Topic? 0
  • +

Replies To: How to display many frames in java

#2 GregBrannon  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2197
  • View blog
  • Posts: 5,224
  • Joined: 10-September 10

Re: How to display many frames in java

Posted 20 November 2011 - 01:19 PM

View PostDreamer43, on 20 November 2011 - 04:13 PM, said:

I just want to ask....If i have a frame with 2 buttons ( GO BACK and NEXT) and i want to display a new frame when "NEXT" is pressed and i want to display completely different design to each frame??? can i do this in 1 class??

Can you? Yes, with qualifications. Should you? I wouldn't. Why would you want to?

As for the qualifications, please be specific when you discuss the containers and components. You probably want one main JFrame for your program that spawns child modal and non-modal JDialogs, as needed.
Was This Post Helpful? 1
  • +
  • -

#3 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8325
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: How to display many frames in java

Posted 20 November 2011 - 01:24 PM

Yes, simply have to JFrame and setVisible(true/false) the one you want
The actionPerformed() method can also be in your class

class MyClass implements ActionListener {

     JButton button1, button2;
     JFrame frame1, frame2;

     MyClass() {
        frame1 = new JFrame("1");
        frame2 = new JFrame("2");

        button1 = new JButton("Show other");
        frame1.add(button1);
        button1.addActionListener(this);
        button2 = new JButton("Show other");
        frame1.add(button2);
        button2.addActionListener(this);

        frame1.setVisible(true);
     }

     public void actionPerformed(ActionEvent e) {
         Object o = e.getSource();
         if(o == button1) {
             frame1.setVisible(false);
             frame2.setVisible(true);
          } else {
             frame2.setVisible(false);
             frame1.setVisible(true);
          }
     }
}


This post has been edited by pbl: 20 November 2011 - 07:38 PM

Was This Post Helpful? 1
  • +
  • -

#4 Dreamer43  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 17
  • Joined: 20-November 11

Re: How to display many frames in java

Posted 20 November 2011 - 03:30 PM

1 more thing... When i change to the next frame my arraylist gets reseted...if i create a customer and save it in an arraylist in my main frame its fine, but when i press next and go to the next frame the customer is being deleted. Is any way so i can use the same arraylist in every frame i have? probably the reason is because i have a separate class for each frame.

And i call this in every class so it does make a new arraylist ?
customers = new ArrayCustList();

Was This Post Helpful? 0
  • +
  • -

#5 GregBrannon  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2197
  • View blog
  • Posts: 5,224
  • Joined: 10-September 10

Re: How to display many frames in java

Posted 20 November 2011 - 03:34 PM

Yes, that line in every class would create a new array of customers. I'm still not a fan of this approach, but as you've chosen to pursue it, you could pass the existing customer array to the constructor of the new frame:

Frame newFrame = new Frame( customerArray );
Was This Post Helpful? 1
  • +
  • -

#6 Dreamer43  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 17
  • Joined: 20-November 11

Re: How to display many frames in java

Posted 20 November 2011 - 06:18 PM

Sorry about my stupid question but i am a bit confused after 10 hours in front of a computer...where shall i put this ?

Frame newFrame = new Frame( customerArray );


I have an arraylist class called : arrayCustList

and my arraylist's name is : customers
Was This Post Helpful? 0
  • +
  • -

#7 GregBrannon  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2197
  • View blog
  • Posts: 5,224
  • Joined: 10-September 10

Re: How to display many frames in java

Posted 20 November 2011 - 06:30 PM

You haven't shown us any code, so I don't know how we can begin to discuss the specifics of where theoretical statements might go. Theoretically, you said you were creating new frames that created new customer arrays or lists. Instead of creating new frames that create new customer lists, create new frames with a variation of the constructor I suggested that accepts an existing customer list.
Was This Post Helpful? 0
  • +
  • -

#8 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8325
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: How to display many frames in java

Posted 20 November 2011 - 07:37 PM

You will have to extend JFrame

class MyFrame extends JFrame {
 
    ArrayList<Whatever> al;

    MyFrame(ArrayList<Whatever> al, String title) {
       super(title);
       this.al = al;
       ...



the in your MyClass

frame1 = new MyFrame(al, "Frame One");
frame2 = new MyFrame(al, "Frame two");
Was This Post Helpful? 0
  • +
  • -

#9 Dreamer43  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 17
  • Joined: 20-November 11

Re: How to display many frames in java

Posted 21 November 2011 - 11:15 AM

Right...thats a part of my code in one of my classes....I want to pass this arraylist to SearchCustomerGUI class.
You can see it in the actionPerformed on the bottom.

...
public class CreateCustomerGUI extends JFrame implements ActionListener
{
    private JLabel lblHeading, lblInstruct,lblSurname, lblFirstName, lblPostCode, lblID, lblHouseNumber, lblStreet, lblTown, lblCounty, lblDOB, lblAsterisk; 
    private JTextField txtSurname, txtFirstName, txtPostCode, txtID, txtHouseNumber, txtStreet, txtTown, txtCounty, txtDOB;
    private JButton bCreate,bConfirm,bCancel,bGoBack,bQuit;
    private Container container;
    private BorderLayout layout;
    private ArrayCustList customers;
    private boolean check = false;
    /**
     *  set up CreateCustomerGUI
     */
    public CreateCustomerGUI()
    {
        super("SimpleFlights Booking System");
        customers = new ArrayCustList();
        makeFrame();
        addActionLIsteners();
        showFrame();
    }

...

 public void actionPerformed( ActionEvent evt )
    {
        String source = evt.getActionCommand();
        if (source.equals("Create"))
        {             
            bCreate.setVisible(false);
            lblHeading.setText("Confirm the details of the customer to proceed.");
            lblHeading.setToolTipText("Confirm the details of the customer to proceed");
            lblInstruct.setText("Confirm the following details : ");
            lblInstruct.setToolTipText("Confirm the following details to proceed!");
            bConfirm.setVisible(true); 
            check = true;
        }
        else if (source.equals("Confirm"))  
        {
            createCustomer();
            printAllAccounts();
            getSizeCust();            
        }
        else if (source.equals("Go Back") && check == false)  
        {            
            this.setVisible(false);
            new SearchCustomerGUI();    // I WANT TO PASS customers TO THIS FRAME
        }
        else if (source.equals("Go Back") && check == true)  
        {
            bConfirm.setVisible(false); 
            bCreate.setVisible(true);
            lblHeading.setText("Message : The System cannot find an existing customer");
            lblHeading.setToolTipText("System cannot find an existing customer");
            lblInstruct.setText("Please enter the details below to create a new customer : ");
            lblInstruct.setToolTipText("Enter all the details below to proceed");

            check = false;

        }
        else if (source.equals("Cancel"))  
        {
            cancel();
        }
        else if ( source.equals("Quit"))
            quit();
    } 

...



Was This Post Helpful? 0
  • +
  • -

#10 Fuzzyness  Icon User is offline

  • Comp Sci Student
  • member icon

Reputation: 669
  • View blog
  • Posts: 2,438
  • Joined: 06-March 09

Re: How to display many frames in java

Posted 21 November 2011 - 11:25 AM

If you want to pass the array just have the constructor take an arraylist as a parameter
Was This Post Helpful? 0
  • +
  • -

#11 Dreamer43  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 17
  • Joined: 20-November 11

Re: How to display many frames in java

Posted 23 November 2011 - 01:00 PM

Sorry for being annoying...but can you give me a simple example ?
Was This Post Helpful? 0
  • +
  • -

#12 macosxnerd101  Icon User is offline

  • Self-Trained Economist
  • member icon




Reputation: 10397
  • View blog
  • Posts: 38,479
  • Joined: 27-December 08

Re: How to display many frames in java

Posted 23 November 2011 - 01:08 PM

Notice how I use the constructor to initialize the ArrayList based on a parameter. Constructors work like regular methods in this manner.
class Test{

   private ArrayList<String> list;

   public Test(ArrayList<String> list){ this.list = list; }
}



Also, rather than using multiple JFrames, why not just use multiple JPanels and a CardLayout to swap them out? It is much simpler and cleaner than having to hide/show a bunch of JFrames.
Was This Post Helpful? 1
  • +
  • -

#13 Dreamer43  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 17
  • Joined: 20-November 11

Re: How to display many frames in java

Posted 23 November 2011 - 01:16 PM

I jave to use a seperate class for each frame...its an assignment and this is what they ask for....and i have many frames, so mane panels so if i make everything in 1 class it will be massive. Thank you for the example let me try that out! cheers
Was This Post Helpful? 0
  • +
  • -

#14 Dreamer43  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 17
  • Joined: 20-November 11

Re: How to display many frames in java

Posted 23 November 2011 - 01:28 PM

Ok, I have put the constructor in my arrayCustList :

public class ArrayCustList
{
...
    private ArrayList<Customer> customers;
    /**
     * Constructor for objects of class ArrayCustList
     */
    public ArrayCustList()
    {
        customers = new ArrayList<Customer>();
    }
    
    public ArrayCustList(ArrayList<Customer> customers)
    {
        this.customers = customers;
    }

...



Now i want to use that arraylist in this frame...I am so confused because i have to handle many classes pff..

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.util.*;
import java.io.*;
 
/** 
 * SimpleFlights Booking System v.1.0
 * CreateCustomerGUI class
 */
public class CreateCustomerGUI extends JFrame implements ActionListener
{
    private JLabel lblHeading, lblInstruct,lblSurname, lblFirstName, lblPostCode, lblID, lblHouseNumber, lblStreet, lblTown, lblCounty, lblDOB, lblAsterisk; 
    private JTextField txtSurname, txtFirstName, txtPostCode, txtID, txtHouseNumber, txtStreet, txtTown, txtCounty, txtDOB;
    private JButton bCreate,bConfirm,bCancel,bGoBack,bQuit;
    private Container container;
    private BorderLayout layout;
    private ArrayCustList customers;
    private boolean check = false;
    /**
     *  set up CreateCustomerGUI
     */
    public CreateCustomerGUI()
    {
        super("SimpleFlights Booking System");
        makeFrame();
        addActionLIsteners();
        showFrame();
    }
...


This post has been edited by macosxnerd101: 23 November 2011 - 01:32 PM
Reason for edit:: Removed personal information per OP request

Was This Post Helpful? 0
  • +
  • -

#15 macosxnerd101  Icon User is offline

  • Self-Trained Economist
  • member icon




Reputation: 10397
  • View blog
  • Posts: 38,479
  • Joined: 27-December 08

Re: How to display many frames in java

Posted 23 November 2011 - 01:36 PM

The idea is to keep your program state/data separate from your user interface. One good way to do this is to create a Singleton class to manage the state/data, with appropriate getter/setter methods. Then the GUI classes can getInstance() on the StateManager class, and get/set the appropriate information.

In other words, separate your business logic from your GUI. Then add the GUI to just interface with the business logic.
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2