bwhelanuk
asked on
Linking one form to another
I have set up a main screen, MainScreen1, and I want to call another screen StockEnq, when the Stock button is pressed. I would appreciate any help at all.
Here are the 2 screens:
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
public class MainScreen1 extends JFrame {
JPanel row1 = new JPanel();
JLabel abacus = new JLabel("ABACUS STOCK CONTROL", SwingConstants.CENTER );
JPanel row2 = new JPanel();
JButton stock = new JButton("STOCK");
JButton suppliers = new JButton("SUPPLIERS");
JButton orders = new JButton("ORDERS");
JButton employees = new JButton("EMPLOYEES");
JButton category = new JButton("CATEGORY");
JButton prices = new JButton("PRICES");
public MainScreen1() {
super("Abacus");
setSize(400, 300);
setDefaultCloseOperation(J Frame.EXIT _ON_CLOSE) ;
GridLayout layout = new GridLayout(3,1);
Container c = getContentPane();
c.setLayout(layout);
//Add listeners
stock.addActionListener(
new ActionListener() {
public void actionPerformed(ActionEven t e) {
StockEnq stock1 = new StockEnq();
setVisible(true);
}
});
FlowLayout layout1 = new FlowLayout(FlowLayout.CENT ER, 10, 10 );
row1.setLayout(layout1);
row1.add(abacus);
c.add(row1);
GridLayout layout2 = new GridLayout(3,2,10, 10 );
row2.setLayout(layout2);
row2.add(stock);
row2.add(suppliers);
row2.add(orders);
row2.add(employees);
row2.add(category);
row2.add(prices);
c.add(row2);
setVisible(true);
}
public Insets getInsets() {
Insets i = new Insets(50,40,15,40);
return i;
}
public static void main (String args[]) {
MainScreen1 frame = new MainScreen1();
}
}
************************** ********** ********** *****
This is the screen i want to be called
************************** ********** ********** *****
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
class StockEnq extends JFrame{
JLabel l1= new JLabel("Stock Query ");
JLabel l2= new JLabel("Reports ");
JLabel l3= new JLabel("General Query");
JButton b1 = new JButton ("run");
JButton b2 = new JButton ("run");
JButton b3 = new JButton ("run");
JTextField t1 = new JTextField(15);
JTextField t2 = new JTextField(15);
JTextField t3 = new JTextField(15);
public StockEnq() {
Container c = getContentPane ();
c.setLayout (new FlowLayout ());
//add text fields to frame
c.add (l1);
c.add (t1);
c.add (b1);
c.add (l2);
c.add (t2);
c.add (b2);
c.add (l3);
c.add (t3);
c.add (b3);
//set focus to t1
t1.requestFocus();
//add action listeners
t1.addActionListener(new ActionListenerT1());
t2.addActionListener(new ActionListenerT2());
t3.addActionListener(new ActionListenerT3());
addWindowListener (new WindowAdapter () {
public void windowClosing (WindowEvent e )
{dispose () ;}
});
}
//ActionListener classes for textfields
class ActionListenerT1 implements ActionListener
{
public void actionPerformed(ActionEven t e)
{
t1.transferFocus();
}
}
class ActionListenerT2 implements ActionListener
{
public void actionPerformed(ActionEven t e)
{
t2.transferFocus();
}
}
class ActionListenerT3 implements ActionListener
{
public void actionPerformed(ActionEven t e)
{
t3.transferFocus();
}
}
public static void main (String args[])
{
StockEnq mFrame = new StockEnq ();
mFrame.setLocation(100,100 );
mFrame.setSize(450,200);
mFrame.setTitle("STOCK ENQUIRIES");
mFrame.setVisible(true);
}
}
Here are the 2 screens:
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
public class MainScreen1 extends JFrame {
JPanel row1 = new JPanel();
JLabel abacus = new JLabel("ABACUS STOCK CONTROL", SwingConstants.CENTER );
JPanel row2 = new JPanel();
JButton stock = new JButton("STOCK");
JButton suppliers = new JButton("SUPPLIERS");
JButton orders = new JButton("ORDERS");
JButton employees = new JButton("EMPLOYEES");
JButton category = new JButton("CATEGORY");
JButton prices = new JButton("PRICES");
public MainScreen1() {
super("Abacus");
setSize(400, 300);
setDefaultCloseOperation(J
GridLayout layout = new GridLayout(3,1);
Container c = getContentPane();
c.setLayout(layout);
//Add listeners
stock.addActionListener(
new ActionListener() {
public void actionPerformed(ActionEven
StockEnq stock1 = new StockEnq();
setVisible(true);
}
});
FlowLayout layout1 = new FlowLayout(FlowLayout.CENT
row1.setLayout(layout1);
row1.add(abacus);
c.add(row1);
GridLayout layout2 = new GridLayout(3,2,10, 10 );
row2.setLayout(layout2);
row2.add(stock);
row2.add(suppliers);
row2.add(orders);
row2.add(employees);
row2.add(category);
row2.add(prices);
c.add(row2);
setVisible(true);
}
public Insets getInsets() {
Insets i = new Insets(50,40,15,40);
return i;
}
public static void main (String args[]) {
MainScreen1 frame = new MainScreen1();
}
}
**************************
This is the screen i want to be called
**************************
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
class StockEnq extends JFrame{
JLabel l1= new JLabel("Stock Query ");
JLabel l2= new JLabel("Reports ");
JLabel l3= new JLabel("General Query");
JButton b1 = new JButton ("run");
JButton b2 = new JButton ("run");
JButton b3 = new JButton ("run");
JTextField t1 = new JTextField(15);
JTextField t2 = new JTextField(15);
JTextField t3 = new JTextField(15);
public StockEnq() {
Container c = getContentPane ();
c.setLayout (new FlowLayout ());
//add text fields to frame
c.add (l1);
c.add (t1);
c.add (b1);
c.add (l2);
c.add (t2);
c.add (b2);
c.add (l3);
c.add (t3);
c.add (b3);
//set focus to t1
t1.requestFocus();
//add action listeners
t1.addActionListener(new ActionListenerT1());
t2.addActionListener(new ActionListenerT2());
t3.addActionListener(new ActionListenerT3());
addWindowListener (new WindowAdapter () {
public void windowClosing (WindowEvent e )
{dispose () ;}
});
}
//ActionListener classes for textfields
class ActionListenerT1 implements ActionListener
{
public void actionPerformed(ActionEven
{
t1.transferFocus();
}
}
class ActionListenerT2 implements ActionListener
{
public void actionPerformed(ActionEven
{
t2.transferFocus();
}
}
class ActionListenerT3 implements ActionListener
{
public void actionPerformed(ActionEven
{
t3.transferFocus();
}
}
public static void main (String args[])
{
StockEnq mFrame = new StockEnq ();
mFrame.setLocation(100,100
mFrame.setSize(450,200);
mFrame.setTitle("STOCK ENQUIRIES");
mFrame.setVisible(true);
}
}
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
I recommend 1 main Frame and the rest of components must be containers like JPanels, try with this code:
/******* the main frame ************/
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.text.*;
public class MainScreen1 extends JFrame {
JPanel row1 = new JPanel();
JLabel abacus = new JLabel("ABACUS STOCK CONTROL", SwingConstants.CENTER );
JPanel row2 = new JPanel();
JButton stock = new JButton("STOCK");
JButton suppliers = new JButton("SUPPLIERS");
JButton orders = new JButton("ORDERS");
JButton employees = new JButton("EMPLOYEES");
JButton category = new JButton("CATEGORY");
JButton prices = new JButton("PRICES");
public MainScreen1() {
super("Abacus");
setSize(400, 300);
setDefaultCloseOperation(J Frame.EXIT _ON_CLOSE) ;
GridLayout layout = new GridLayout(3,1);
Container c = getContentPane();
c.setLayout(layout);
stock.addActionListener(ne w java.awt.event.ActionListe ner() {
public void actionPerformed(java.awt.e vent.Actio nEvent evt) {
stockActionPerformed(evt);
}
});
FlowLayout layout1 = new FlowLayout(FlowLayout.CENT ER, 10, 10 );
row1.setLayout(layout1);
row1.add(abacus);
c.add(row1);
GridLayout layout2 = new GridLayout(3,2,10, 10 );
row2.setLayout(layout2);
row2.add(stock);
row2.add(suppliers);
row2.add(orders);
row2.add(employees);
row2.add(category);
row2.add(prices);
c.add(row2);
setVisible(true);
}
public Insets getInsets() {
Insets i = new Insets(50,40,15,40);
return i;
}
private void stockActionPerformed(java. awt.event. ActionEven t evt) {
change_1();
}
public static void main (String args[]) {
MainScreen1 frame = new MainScreen1();
}
/** Carga un panel de la clase jP_Divisas.
*/
public void change_1() {
Stock am = new Stock();
this.getContentPane().remo veAll();
this.getContentPane().add( am);
pack();
this.repaint();
}
}
/*************** one panel***************/
public class Stock extends javax.swing.JPanel {
/** Creates new form Stock */
public Stock() {
initComponents();
}
/** This method is called from within the constructor to
* initialize the form.
* WARNING: Do NOT modify this code. The content of this method is
* always regenerated by the Form Editor.
*/
private void initComponents() {
jLabel1 = new javax.swing.JLabel();
jTextField1 = new javax.swing.JTextField();
jButton1 = new javax.swing.JButton();
jLabel2 = new javax.swing.JLabel();
jTextField2 = new javax.swing.JTextField();
jButton2 = new javax.swing.JButton();
jLabel3 = new javax.swing.JLabel();
jTextField3 = new javax.swing.JTextField();
jButton3 = new javax.swing.JButton();
setLayout(new org.netbeans.lib.awtextra. AbsoluteLa yout());
setMaximumSize(new java.awt.Dimension(400, 200));
setMinimumSize(new java.awt.Dimension(400, 200));
jLabel1.setText("Stock query");
add(jLabel1, new org.netbeans.lib.awtextra. AbsoluteCo nstraints( 20, 40, 120, -1));
add(jTextField1, new org.netbeans.lib.awtextra. AbsoluteCo nstraints( 110, 40, 120, -1));
jButton1.setText("run");
add(jButton1, new org.netbeans.lib.awtextra. AbsoluteCo nstraints( 250, 40, 60, 20));
jLabel2.setText("Reports") ;
add(jLabel2, new org.netbeans.lib.awtextra. AbsoluteCo nstraints( 330, 40, 50, -1));
add(jTextField2, new org.netbeans.lib.awtextra. AbsoluteCo nstraints( 20, 90, 210, -1));
jButton2.setText("run");
add(jButton2, new org.netbeans.lib.awtextra. AbsoluteCo nstraints( 250, 90, 60, 20));
jLabel3.setText("General Query");
add(jLabel3, new org.netbeans.lib.awtextra. AbsoluteCo nstraints( 320, 90, 90, -1));
add(jTextField3, new org.netbeans.lib.awtextra. AbsoluteCo nstraints( 20, 130, 210, -1));
jButton3.setText("run");
add(jButton3, new org.netbeans.lib.awtextra. AbsoluteCo nstraints( 250, 130, 60, 20));
}
// Variables declaration - do not modify
private javax.swing.JButton jButton1;
private javax.swing.JButton jButton2;
private javax.swing.JButton jButton3;
private javax.swing.JLabel jLabel1;
private javax.swing.JLabel jLabel2;
private javax.swing.JLabel jLabel3;
private javax.swing.JTextField jTextField1;
private javax.swing.JTextField jTextField2;
private javax.swing.JTextField jTextField3;
// End of variables declaration
}
/******* the main frame ************/
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.text.*;
public class MainScreen1 extends JFrame {
JPanel row1 = new JPanel();
JLabel abacus = new JLabel("ABACUS STOCK CONTROL", SwingConstants.CENTER );
JPanel row2 = new JPanel();
JButton stock = new JButton("STOCK");
JButton suppliers = new JButton("SUPPLIERS");
JButton orders = new JButton("ORDERS");
JButton employees = new JButton("EMPLOYEES");
JButton category = new JButton("CATEGORY");
JButton prices = new JButton("PRICES");
public MainScreen1() {
super("Abacus");
setSize(400, 300);
setDefaultCloseOperation(J
GridLayout layout = new GridLayout(3,1);
Container c = getContentPane();
c.setLayout(layout);
stock.addActionListener(ne
public void actionPerformed(java.awt.e
stockActionPerformed(evt);
}
});
FlowLayout layout1 = new FlowLayout(FlowLayout.CENT
row1.setLayout(layout1);
row1.add(abacus);
c.add(row1);
GridLayout layout2 = new GridLayout(3,2,10, 10 );
row2.setLayout(layout2);
row2.add(stock);
row2.add(suppliers);
row2.add(orders);
row2.add(employees);
row2.add(category);
row2.add(prices);
c.add(row2);
setVisible(true);
}
public Insets getInsets() {
Insets i = new Insets(50,40,15,40);
return i;
}
private void stockActionPerformed(java.
change_1();
}
public static void main (String args[]) {
MainScreen1 frame = new MainScreen1();
}
/** Carga un panel de la clase jP_Divisas.
*/
public void change_1() {
Stock am = new Stock();
this.getContentPane().remo
this.getContentPane().add(
pack();
this.repaint();
}
}
/*************** one panel***************/
public class Stock extends javax.swing.JPanel {
/** Creates new form Stock */
public Stock() {
initComponents();
}
/** This method is called from within the constructor to
* initialize the form.
* WARNING: Do NOT modify this code. The content of this method is
* always regenerated by the Form Editor.
*/
private void initComponents() {
jLabel1 = new javax.swing.JLabel();
jTextField1 = new javax.swing.JTextField();
jButton1 = new javax.swing.JButton();
jLabel2 = new javax.swing.JLabel();
jTextField2 = new javax.swing.JTextField();
jButton2 = new javax.swing.JButton();
jLabel3 = new javax.swing.JLabel();
jTextField3 = new javax.swing.JTextField();
jButton3 = new javax.swing.JButton();
setLayout(new org.netbeans.lib.awtextra.
setMaximumSize(new java.awt.Dimension(400, 200));
setMinimumSize(new java.awt.Dimension(400, 200));
jLabel1.setText("Stock query");
add(jLabel1, new org.netbeans.lib.awtextra.
add(jTextField1, new org.netbeans.lib.awtextra.
jButton1.setText("run");
add(jButton1, new org.netbeans.lib.awtextra.
jLabel2.setText("Reports")
add(jLabel2, new org.netbeans.lib.awtextra.
add(jTextField2, new org.netbeans.lib.awtextra.
jButton2.setText("run");
add(jButton2, new org.netbeans.lib.awtextra.
jLabel3.setText("General Query");
add(jLabel3, new org.netbeans.lib.awtextra.
add(jTextField3, new org.netbeans.lib.awtextra.
jButton3.setText("run");
add(jButton3, new org.netbeans.lib.awtextra.
}
// Variables declaration - do not modify
private javax.swing.JButton jButton1;
private javax.swing.JButton jButton2;
private javax.swing.JButton jButton3;
private javax.swing.JLabel jLabel1;
private javax.swing.JLabel jLabel2;
private javax.swing.JLabel jLabel3;
private javax.swing.JTextField jTextField1;
private javax.swing.JTextField jTextField2;
private javax.swing.JTextField jTextField3;
// End of variables declaration
}
>> I recommend 1 main Frame and the rest of components must be containers like JPanels, try with this code:
That is different from my statement how?
That is different from my statement how?
ASKER
As much as I like the idea of a cardlayout, is there no easier way to display another form when pressing a button?
Sure, you can remove and add panels to a frame during runtime, the problem with that though is that the panel will be layed out again, which might or might not problem. Re-laying out panels, or any container for that matter, will make the components move about on the screen. I personally find that very unsightly.
CardLayout forms = new CardLayout();
forms.addLayoutComponent("
forms.addLayoutComponent("
Then to show a certain 'card', e.g. "form1":
forms.show(parentFrame, "form1");