1

now i have 3 Java files which are . i have 2 buttons on TrainAndCOnductor. my purpose is , when i click on 1st button it will open my The Conductor panel.

public class JavaApplication1 {

    public static void main(String[] args) {
        TheConductor form = new TheConductor();
        form.setVisible(true);
    }
}
//Train And Conductor
    public class TrainAndConductor extends javax.swing.JFrame {

    /** Creates new form TrainAndConductor */
    public TrainAndConductor() {
        initComponents();
    }

    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">
    private void initComponents() {

        jButton1 = new javax.swing.JButton();
        jButton2 = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        jButton1.setText("Train Driver");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        jButton2.setText("Conductor");
        jButton2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton2ActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jButton1, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 282, Short.MAX_VALUE)
            .addComponent(jButton2, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 282, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(jButton1, javax.swing.GroupLayout.PREFERRED_SIZE, 23, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jButton2))
        );

        pack();
    }// </editor-fold>

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
        add(TheConductor);
        TheConductor.setVisible(true);

//The Conductor
public class TheConductor extends javax.swing.JFrame {

    /** Creates new form TheConductor */
    public TheConductor() {
        initComponents();
    }
Boro
  • 7,913
  • 4
  • 43
  • 85
George Lim
  • 145
  • 1
  • 2
  • 8

2 Answers2

3

It depends on how your layout is organised. You can use a JTabbedPane or CardLayout to achieve this.

Jonas
  • 121,568
  • 97
  • 310
  • 388
  • 1
    See also this `CardLayout` [example](http://stackoverflow.com/questions/5654926/implementing-back-forward-buttons-in-swing/5655843#5655843). – trashgod May 20 '11 at 05:53
  • 1
    @Andrew Thompson: I had since edited the link; and thank you for noticing. :-) – trashgod May 20 '11 at 06:25
2

Try this:

private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
   secondPanel.setVisible(true);
}

and if you are not adding it before then you have to also add it like:

private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
   add(secondPanel);
   secondPanel.setVisible(true);
}

Try this:

private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
    TheConductor conduct = new TheConductor();
    add(conduct);
    conduct.setVisible(true);
}
Harry Joy
  • 58,650
  • 30
  • 162
  • 207
  • sorry, it cannot work , it says non-static method setVisible(boolean) cannot be referenced from a static context – George Lim May 20 '11 at 05:54
  • @George Lim: is your `TrainAndConductor` variable global and accessible by this method? – Harry Joy May 20 '11 at 05:56
  • @George: You must create the `TrainAndConductor` panel before, and then call the method setVisible on the object: `myTrainAndConductorObject.setVisible(true);` – Jonas May 20 '11 at 05:56
  • @George: We know that. It would be helpful if you could provide more code. – Jonas May 20 '11 at 06:02
  • i have edited the code, some codes are for other purpose just ignore it. – George Lim May 20 '11 at 06:21
  • @George: okay. Now just create constructor of the TheConductor class in actionPerformed and in the constructor of TheConductor use `setVisible(true);`. Note that you also have to provide size of TheConductor may be you are providing in initComponents() of TheConductor. – Harry Joy May 20 '11 at 06:43
  • thanks man . it works now . and also last question. what method should i implement to close the TrainAndConductor Panel. public TheConductor() { initComponents(); setVisible(true); //close action – George Lim May 20 '11 at 06:55
  • @George: it depends on meaning of close. If you just wanna hide and preserve data then use `setVisible(false);` – Harry Joy May 20 '11 at 06:56
  • it works after i remove a bunch of code in TheConductor which are below. public static void main(String args[]) { java.awt.EventQueue.invokeLater(new Runnable() { public void run() { new TheConductor().setVisible(true); } }); } – George Lim May 20 '11 at 06:58
  • what i meant was , let say there are 2 panels there, once i click the button on the 1st panel and it brings me to the second panel in the mean time, it will also close my first panel. – George Lim May 20 '11 at 07:03
  • @George: add `setVisible(false);` after you create TheConstructor object in first frame. This will hide first frame. – Harry Joy May 20 '11 at 07:04
  • sorry sir, i dont get it, where should i add setVisible(false); ? – George Lim May 20 '11 at 07:13
  • @George: `private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) { TheConductor conduct = new TheConductor(); setVisible(false); }` – Harry Joy May 20 '11 at 07:16
  • @Harry hi, but my current setVisible is true , how can i set it to false again ? `private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) { TheConductor conduct = new TheConductor(); add(conduct); conduct.setVisible(false);` – George Lim May 20 '11 at 07:30
  • `private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) { TheConductor conduct = new TheConductor(); conduct.setVisible(true); setVisible(false); }` – George Lim May 20 '11 at 07:36