16 Replies - 1100 Views - Last Post: 23 November 2011 - 01:46 PM
#1
How to display many frames in java
Posted 20 November 2011 - 01:13 PM
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.
Replies To: How to display many frames in java
#2
Re: How to display many frames in java
Posted 20 November 2011 - 01:19 PM
Dreamer43, on 20 November 2011 - 04:13 PM, said:
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.
#3
Re: How to display many frames in java
Posted 20 November 2011 - 01:24 PM
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
#4
Re: How to display many frames in java
Posted 20 November 2011 - 03:30 PM
And i call this in every class so it does make a new arraylist ?
customers = new ArrayCustList();
#5
Re: How to display many frames in java
Posted 20 November 2011 - 03:34 PM
Frame newFrame = new Frame( customerArray );
#6
Re: How to display many frames in java
Posted 20 November 2011 - 06:18 PM
Frame newFrame = new Frame( customerArray );
I have an arraylist class called : arrayCustList
and my arraylist's name is : customers
#7
Re: How to display many frames in java
Posted 20 November 2011 - 06:30 PM
#8
Re: How to display many frames in java
Posted 20 November 2011 - 07:37 PM
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");
#9
Re: How to display many frames in java
Posted 21 November 2011 - 11:15 AM
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();
}
...
#10
Re: How to display many frames in java
Posted 21 November 2011 - 11:25 AM
#11
Re: How to display many frames in java
Posted 23 November 2011 - 01:00 PM
#12
Re: How to display many frames in java
Posted 23 November 2011 - 01:08 PM
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.
#13
Re: How to display many frames in java
Posted 23 November 2011 - 01:16 PM
#14
Re: How to display many frames in java
Posted 23 November 2011 - 01:28 PM
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
#15
Re: How to display many frames in java
Posted 23 November 2011 - 01:36 PM
In other words, separate your business logic from your GUI. Then add the GUI to just interface with the business logic.
|
|

New Topic/Question
Reply



MultiQuote






|