Solved

Linking one form to another

Posted on 2004-04-06
8
755 Views
Last Modified: 2012-08-13
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(JFrame.EXIT_ON_CLOSE);
      GridLayout layout = new GridLayout(3,1);
      Container c = getContentPane();
      c.setLayout(layout);

      //Add listeners
      stock.addActionListener(  
            
          new ActionListener() {

            
            public void actionPerformed(ActionEvent e) {
      
      StockEnq  stock1 = new StockEnq();
      
      setVisible(true);
            }
      });

      FlowLayout layout1 = new FlowLayout(FlowLayout.CENTER, 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(ActionEvent e)
      {

            t1.transferFocus();
        }
 }

      class ActionListenerT2 implements ActionListener
{
      public void actionPerformed(ActionEvent e)

      {

            t2.transferFocus();
            
       }
}

class ActionListenerT3 implements ActionListener
 {
      public void actionPerformed(ActionEvent 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);
}
}
0
Comment
Question by:bwhelanuk
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
8 Comments
 
LVL 14

Accepted Solution

by:
Tommy Braas earned 65 total points
ID: 10766980
Add your forms to a CardLayout. The CardLayout is a layout manager that handles many panels and facilitates easy switching between them.
0
 
LVL 14

Expert Comment

by:Tommy Braas
ID: 10767015
You might want to consider making all your forms subclasses of JPanel. You can add a JPanel to any other container, it is not that easy with a JFrame. Then you add the to a CardLayout:

CardLayout forms = new CardLayout();
forms.addLayoutComponent("form1", myFirstForm);
forms.addLayoutComponent("form2", mySecondForm);
 
Then to show a certain 'card', e.g. "form1":
forms.show(parentFrame, "form1");
0
 

Expert Comment

by:RobCSP
ID: 10767324
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(JFrame.EXIT_ON_CLOSE);
     GridLayout layout = new GridLayout(3,1);
     Container c = getContentPane();
     c.setLayout(layout);

   
      stock.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                stockActionPerformed(evt);
            }
        });
   

     FlowLayout layout1 = new FlowLayout(FlowLayout.CENTER, 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.ActionEvent 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().removeAll();
        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.AbsoluteLayout());

        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.AbsoluteConstraints(20, 40, 120, -1));

        add(jTextField1, new org.netbeans.lib.awtextra.AbsoluteConstraints(110, 40, 120, -1));

        jButton1.setText("run");
        add(jButton1, new org.netbeans.lib.awtextra.AbsoluteConstraints(250, 40, 60, 20));

        jLabel2.setText("Reports");
        add(jLabel2, new org.netbeans.lib.awtextra.AbsoluteConstraints(330, 40, 50, -1));

        add(jTextField2, new org.netbeans.lib.awtextra.AbsoluteConstraints(20, 90, 210, -1));

        jButton2.setText("run");
        add(jButton2, new org.netbeans.lib.awtextra.AbsoluteConstraints(250, 90, 60, 20));

        jLabel3.setText("General Query");
        add(jLabel3, new org.netbeans.lib.awtextra.AbsoluteConstraints(320, 90, 90, -1));

        add(jTextField3, new org.netbeans.lib.awtextra.AbsoluteConstraints(20, 130, 210, -1));

        jButton3.setText("run");
        add(jButton3, new org.netbeans.lib.awtextra.AbsoluteConstraints(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
   
}
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 14

Expert Comment

by:Tommy Braas
ID: 10767538
>>  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?
0
 

Author Comment

by:bwhelanuk
ID: 10767977
As much as I like the idea of a cardlayout, is there no easier way to display another form when pressing a button?
0
 
LVL 14

Expert Comment

by:Tommy Braas
ID: 10768456
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.
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
In this post we will learn how to make Android Gesture Tutorial and give different functionality whenever a user Touch or Scroll android screen.
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.

717 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question