Solved

Linking one form to another

Posted on 2004-04-06
8
744 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
  • 4
8 Comments
 
LVL 14

Accepted Solution

by:
Tommy Braas earned 65 total points
Comment Utility
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
Comment Utility
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
Comment Utility
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
Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

 
LVL 14

Expert Comment

by:Tommy Braas
Comment Utility
>>  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
Comment Utility
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
Comment Utility
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

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

For customizing the look of your lightweight component and making it look opaque like it was made of plastic.  This tip assumes your component to be of rectangular shape and completely opaque.   (CODE)
An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
This video teaches viewers about errors in exception handling.

772 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now