We help IT Professionals succeed at work.

Open a second form...

Mike Eghtebas
on
In the process of designing an alarm clock, when btnSetAlarm is clicked, a second form needs to open allowing the user the enter values.
 
   public void actionPerformed(ActionEvent e)
    {
        Object source = e.getSource();
       
        if (source == btnSetAlarm) {
            System.out.println("test ok btnSetAlarm");
            AlarmClockReset restAlarm = new AlarmClockReset();
            restAlarm.setTitle("Set Alarm");
            restAlarm.setSize(200, 160);
            restAlarm.setLocationRelativeTo(null);
            restAlarm.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            restAlarm.setVisible(true);
       
        }else if (source == chkAlarm) {
            System.out.println("test ok chkAlarm");
        }
    }

The form opens but it doesn't show the components on the form.

Q1: How to make the control to be visible?
Q2: How to place that form to the right side of the first form when it opens?
Q3: If there is some bad approach, please comment on it so I can correct it.
Thank you.
import java.awt.*;
import java.awt.event.*;
import java.applet.*;
import javax.swing.*;

public class AlarmClock extends JApplet implements ActionListener{

    private JCheckBox chkAlarm = new JCheckBox("Alarm");
    private JButton btnSetAlarm = new JButton("Set Alarm");
    private JLabel lblHour = new JLabel("Hour");
    private JLabel lblMinute = new JLabel("Minute");
    private JLabel lblSecond = new JLabel("Second");
    private JTextField txtHour = new JTextField("9", 3);
    private JTextField txtMinute = new JTextField("24", 3);
    private JTextField txtSecond = new JTextField("49", 3);
    private AudioClip audioClip;
    Font f1 = new Font("Arial", 1, 36);
    Font f2 = new Font("Arial", 1, 12);
    
    public void init() {

        chkAlarm.addActionListener(this);
        btnSetAlarm.addActionListener(this);

//        setLayout(new BorderLayout());
        
        JPanel pane1 = new JPanel(new GridLayout(0, 3, 0, 0));
        lblHour.setFont(f2); lblMinute.setFont(f2); lblSecond.setFont(f2);
        lblHour.setHorizontalAlignment(0); lblMinute.setHorizontalAlignment(0); lblSecond.setHorizontalAlignment(0);
        pane1.add(lblHour);
        pane1.add(lblMinute);
        pane1.add(lblSecond);
        add(pane1, BorderLayout.NORTH);
        
        JPanel pane2 = new JPanel(new GridLayout(0, 3, 0, 0));
        txtHour.setFont(f1); txtMinute.setFont(f1); txtSecond.setFont(f1);
        txtHour.setHorizontalAlignment(0); txtMinute.setHorizontalAlignment(0); txtSecond.setHorizontalAlignment(0);
       
        pane2.add(txtHour); pane2.add(txtMinute); pane2.add(txtSecond);
        add(pane2, BorderLayout.CENTER);
        
        JPanel pane3 = new JPanel();
        pane3.add(chkAlarm);
        pane3.add(btnSetAlarm);
        add(pane3, BorderLayout.SOUTH);
        
    }

    public void actionPerformed(ActionEvent e) 
    {
        Object source = e.getSource();
        
        if (source == btnSetAlarm) {
            System.out.println("test ok btnSetAlarm");
            AlarmClockReset restAlarm = new AlarmClockReset();
            restAlarm.setTitle("Set Alarm");
            restAlarm.setSize(200, 160);
            restAlarm.setLocationRelativeTo(null);
            restAlarm.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            restAlarm.setVisible(true);
        
        }else if (source == chkAlarm) {
            System.out.println("test ok chkAlarm");
        }
    }
    
    public static void main(String[] args) {
        
        JFrame frame = new JFrame();
        frame.setTitle("Exercise 18.22: Alarm Clock");
        frame.setSize(300, 160);
        frame.setLocationRelativeTo(null);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
	AlarmClock tp = new AlarmClock();
	tp.init();
	frame.getContentPane().add(tp);
        frame.setVisible(true);
       
    }
}

class AlarmClockReset extends JFrame implements ActionListener{

    private JCheckBox btnOK = new JCheckBox("OK");
    private JButton btnCancel = new JButton("Cancel");
    private JLabel lblInstruction = new JLabel("Enter Hour, Minute, and Second");
    private JLabel lblHour = new JLabel("Hour");
    private JLabel lblMinute = new JLabel("Minute");
    private JLabel lblSecond = new JLabel("Second");
    private JTextField txtHour = new JTextField("9", 3);
    private JTextField txtMinute = new JTextField("24", 3);
    private JTextField txtSecond = new JTextField("49", 3);
    
    public void AlarmClockReset() {

        btnOK.addActionListener(this);
        btnCancel.addActionListener(this);
        
        JFrame frm = new JFrame("Set Alarm");
        
        JPanel p1 = new JPanel(new BorderLayout());

        p1.add(lblInstruction, BorderLayout.NORTH);
        JPanel p2 = new JPanel(new GridLayout(4, 2, 0, 0));
        p2.add(lblHour); p2.add(txtHour);
        p2.add(lblMinute); p2.add(txtMinute);
        p2.add(lblSecond); p2.add(txtSecond);
        p2.add(btnOK); p2.add(btnCancel);
        frm.add(p1, BorderLayout.NORTH);
        frm.add(p2, BorderLayout.SOUTH);
    }    
    
    public void actionPerformed(ActionEvent e) 
    {
        Object source = e.getSource();
        if (source == btnOK) {
                System.out.println("test ok btnOK");
        }else if (source == btnCancel) {
            System.out.println("test ok btnCancel");
        }
    }
}

Open in new window

Comment
Watch Question

Awarded 2011
Awarded 2011

Commented:
But you didn't add any element there - so it is obviously empty.

I added soem arbitraty elements there tjus to show you
- chneg them as you need.

import java.awt.*;
import java.awt.event.*;
import java.applet.*;
import javax.swing.*;

public class AlarmClock extends JApplet implements ActionListener{

    private JCheckBox chkAlarm = new JCheckBox("Alarm");
    private JButton btnSetAlarm = new JButton("Set Alarm");
    private JLabel lblHour = new JLabel("Hour");
    private JLabel lblMinute = new JLabel("Minute");
    private JLabel lblSecond = new JLabel("Second");
    private JTextField txtHour = new JTextField("9", 3);
    private JTextField txtMinute = new JTextField("24", 3);
    private JTextField txtSecond = new JTextField("49", 3);
    private AudioClip audioClip;
    Font f1 = new Font("Arial", 1, 36);
    Font f2 = new Font("Arial", 1, 12);

    public void init() {

        chkAlarm.addActionListener(this);
        btnSetAlarm.addActionListener(this);

//        setLayout(new BorderLayout());

        JPanel pane1 = new JPanel(new GridLayout(0, 3, 0, 0));
        lblHour.setFont(f2); lblMinute.setFont(f2); lblSecond.setFont(f2);
        lblHour.setHorizontalAlignment(0); lblMinute.setHorizontalAlignment(0); lblSecond.setHorizontalAlignment(0);
        pane1.add(lblHour);
        pane1.add(lblMinute);
        pane1.add(lblSecond);
        add(pane1, BorderLayout.NORTH);

        JPanel pane2 = new JPanel(new GridLayout(0, 3, 0, 0));
        txtHour.setFont(f1); txtMinute.setFont(f1); txtSecond.setFont(f1);
        txtHour.setHorizontalAlignment(0); txtMinute.setHorizontalAlignment(0); txtSecond.setHorizontalAlignment(0);

        pane2.add(txtHour); pane2.add(txtMinute); pane2.add(txtSecond);
        add(pane2, BorderLayout.CENTER);

        JPanel pane3 = new JPanel();
        pane3.add(chkAlarm);
        pane3.add(btnSetAlarm);
        add(pane3, BorderLayout.SOUTH);

    }

    public void actionPerformed(ActionEvent e)
    {
        Object source = e.getSource();

        if (source == btnSetAlarm) {
            System.out.println("test ok btnSetAlarm");
            AlarmClockReset restAlarm = new AlarmClockReset();
            JPanel jp = new JPanel();
            JTextField tf = new JTextField(10);
            JLabel lbl = new JLabel("Set time:");

            Container c1 = restAlarm.getContentPane();

            jp.setLayout(new FlowLayout());
            jp.add(lbl);
            jp.add(tf);
            c1.add(jp);

            restAlarm.setTitle("Set Alarm");
            restAlarm.setSize(200, 160);
            restAlarm.setLocationRelativeTo(null);
            restAlarm.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            restAlarm.setVisible(true);

        }else if (source == chkAlarm) {
            System.out.println("test ok chkAlarm");
        }
    }

    public static void main(String[] args) {

        JFrame frame = new JFrame();
        frame.setTitle("Exercise 18.22: Alarm Clock");
        frame.setSize(300, 160);
        frame.setLocationRelativeTo(null);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
	AlarmClock tp = new AlarmClock();
	tp.init();
	frame.getContentPane().add(tp);
        frame.setVisible(true);

    }
}

class AlarmClockReset extends JFrame implements ActionListener{

    private JCheckBox btnOK = new JCheckBox("OK");
    private JButton btnCancel = new JButton("Cancel");
    private JLabel lblInstruction = new JLabel("Enter Hour, Minute, and Second");
    private JLabel lblHour = new JLabel("Hour");
    private JLabel lblMinute = new JLabel("Minute");
    private JLabel lblSecond = new JLabel("Second");
    private JTextField txtHour = new JTextField("9", 3);
    private JTextField txtMinute = new JTextField("24", 3);
    private JTextField txtSecond = new JTextField("49", 3);

    public void AlarmClockReset() {

        btnOK.addActionListener(this);
        btnCancel.addActionListener(this);

        JFrame frm = new JFrame("Set Alarm");

        JPanel p1 = new JPanel(new BorderLayout());

        p1.add(lblInstruction, BorderLayout.NORTH);
        JPanel p2 = new JPanel(new GridLayout(4, 2, 0, 0));
        p2.add(lblHour); p2.add(txtHour);
        p2.add(lblMinute); p2.add(txtMinute);
        p2.add(lblSecond); p2.add(txtSecond);
        p2.add(btnOK); p2.add(btnCancel);
        frm.add(p1, BorderLayout.NORTH);
        frm.add(p2, BorderLayout.SOUTH);
    }

    public void actionPerformed(ActionEvent e)
    {
        Object source = e.getSource();
        if (source == btnOK) {
                System.out.println("test ok btnOK");
        }else if (source == btnCancel) {
            System.out.println("test ok btnCancel");
        }
    }
}

Open in new window

Awarded 2011
Awarded 2011

Commented:

And don't do this:

source == btnSetAlarm

I wrote you many times - do not compare objects with ==

Awarded 2011
Awarded 2011

Commented:
It is normal practice that your original winodw should indeed
extend JFrame, however these pop-up winodws, and any derivative windows should
not extend JFrame - they should extend JDialog

JFrame shouild normal be only the first root winodw of your application,
all the rest should be derived from JDialog
Mike EghtebasDatabase and Application Developer

Author

Commented:
re:> source == btnSetAlarm

I had this from some old solution from EE. Although it works but as a mater of style, I no longer will use it. Instead, I will use

source.equals(btnOK)

Which works nicely.

-------------------

re:> But you didn't add any element there - so it is obviously empty.

line AlarmClockReset restAlarm = new AlarmClockReset(); instantiates AlarmClockReset class which has all of the components added to it. see lines 82 in the original post.

BTW, I removed void from its constructor, now, it doesn't work at all. I had like:

public void AlarmClockReset() {

I changed it to:

public AlarmClockReset() {

help...

AlarmClockReset

Awarded 2011
Awarded 2011

Commented:
you cannot declare void for constructore
 adn tons of other errors


THis code below wiorks in spite of ll these weird things that you are doing
import java.awt.*;
import java.awt.event.*;
import java.applet.*;
import javax.swing.*;

public class AlarmClock extends JApplet implements ActionListener{

    private JCheckBox chkAlarm = new JCheckBox("Alarm");
    private JButton btnSetAlarm = new JButton("Set Alarm");
    private JLabel lblHour = new JLabel("Hour");
    private JLabel lblMinute = new JLabel("Minute");
    private JLabel lblSecond = new JLabel("Second");
    private JTextField txtHour = new JTextField("9", 3);
    private JTextField txtMinute = new JTextField("24", 3);
    private JTextField txtSecond = new JTextField("49", 3);
    private AudioClip audioClip;
    Font f1 = new Font("Arial", 1, 36);
    Font f2 = new Font("Arial", 1, 12);


    public void init() {

        chkAlarm.addActionListener(this);
        btnSetAlarm.addActionListener(this);

//        setLayout(new BorderLayout());

        JPanel pane1 = new JPanel(new GridLayout(0, 3, 0, 0));
        lblHour.setFont(f2); lblMinute.setFont(f2); lblSecond.setFont(f2);
        lblHour.setHorizontalAlignment(0); lblMinute.setHorizontalAlignment(0); lblSecond.setHorizontalAlignment(0);
        pane1.add(lblHour);
        pane1.add(lblMinute);
        pane1.add(lblSecond);
        add(pane1, BorderLayout.NORTH);

        JPanel pane2 = new JPanel(new GridLayout(0, 3, 0, 0));
        txtHour.setFont(f1); txtMinute.setFont(f1); txtSecond.setFont(f1);
        txtHour.setHorizontalAlignment(0); txtMinute.setHorizontalAlignment(0); txtSecond.setHorizontalAlignment(0);

        pane2.add(txtHour); pane2.add(txtMinute); pane2.add(txtSecond);
        add(pane2, BorderLayout.CENTER);

        JPanel pane3 = new JPanel();
        pane3.add(chkAlarm);
        pane3.add(btnSetAlarm);
        add(pane3, BorderLayout.SOUTH);

    }

    public void actionPerformed(ActionEvent e)
    {
        Object source = e.getSource();

        if (source == btnSetAlarm) {
            System.out.println("test ok btnSetAlarm");
            AlarmClockReset restAlarm = new AlarmClockReset();
            /*
            JPanel jp = new JPanel();
            JTextField tf = new JTextField(10);
            JLabel lbl = new JLabel("Set time:");

            Container c1 = restAlarm.getContentPane();

            jp.setLayout(new FlowLayout());
            jp.add(lbl);
            jp.add(tf);
            c1.add(jp);
                */

            restAlarm.setTitle("Set Alarm");
            restAlarm.setSize(200, 160);
        //    restAlarm.setLocation(tp.getLocation().x + tp.getWidth() + 50, tp.getLocation().y);
          restAlarm.setLocationRelativeTo(this);
            // restAlarm.setLocation(500,500);


            restAlarm.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            restAlarm.setVisible(true);

        }else if (source == chkAlarm) {
            System.out.println("test ok chkAlarm");
        }
    }

    public static void main(String[] args) {

        JFrame frame = new JFrame();
        frame.setTitle("Exercise 18.22: Alarm Clock");
        frame.setSize(300, 160);
        frame.setLocationRelativeTo(null);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
 AlarmClock tp = new AlarmClock();
	tp.init();
	frame.getContentPane().add(tp);
        frame.setVisible(true);

    }
}

class AlarmClockReset extends JFrame implements ActionListener{

    private JCheckBox btnOK;
    private JButton btnCancel;
    private JLabel lblInstruction;
    private JLabel lblHour;
    private JLabel lblMinute;
    private JLabel lblSecond;
    private JTextField txtHour;
    private JTextField txtMinute;
    private JTextField txtSecond;

    public AlarmClockReset() {
        
          btnOK = new JCheckBox("OK");
   btnCancel = new JButton("Cancel");
    lblInstruction = new JLabel("Enter Hour, Minute, and Second");
    lblHour = new JLabel("Hour");
     lblMinute = new JLabel("Minute");
     lblSecond = new JLabel("Second");
    txtHour = new JTextField("9", 3);
   txtMinute = new JTextField("24", 3);
    txtSecond = new JTextField("49", 3);

        btnOK.addActionListener(this);
        btnCancel.addActionListener(this);

       // JFrame frm = new JFrame("Set Alarm");

        JPanel p1 = new JPanel(new BorderLayout());

        p1.add(lblInstruction, BorderLayout.NORTH);
        JPanel p2 = new JPanel(new GridLayout(4, 2));
        p2.add(lblHour); p2.add(txtHour);
        p2.add(lblMinute); p2.add(txtMinute);
        p2.add(lblSecond); p2.add(txtSecond);
        p2.add(btnOK); p2.add(btnCancel);
        Container c1 =  this.getContentPane();
        c1.setLayout(new BorderLayout());
        c1.add(p1, BorderLayout.NORTH);
        c1.add(p2, BorderLayout.CENTER);
        //this.setSize(200,160);
       // this.setVisible(true);
    }


    public void actionPerformed(ActionEvent e)
    {
        Object source = e.getSource();
        if (source == btnOK) {
                System.out.println("test ok btnOK");
        }else if (source == btnCancel) {
            System.out.println("test ok btnCancel");
        }
    }
}

Open in new window

Awarded 2011
Awarded 2011

Commented:


I cannot specify location relative to the first window.
It very well may be because that fiorst window extends Applet.

when I printout System.out.println(this.getLocation().x)
it prints 0 even though it is sitting in the center of the screen
I don't think that would happen if it would be exetnding JFrame, because JFrame
is supposed to be the winow on the screen and JApplet
is sitting in the browser window so that may be the reson why it does not track the location.

There may be some trick that you may use, but I don't know how to do it in this way

Awarded 2011
Awarded 2011
Commented:
These are the main things which were wrong in your code:

class AlarmClockReset extends JFrame implements ActionListener{

    private JCheckBox btnOK = new JCheckBox("OK");
    private JButton btnCancel = new JButton("Cancel");
    private JLabel lblInstruction = new JLabel("Enter Hour, Minute, and Second");
    private JLabel lblHour = new JLabel("Hour");
    private JLabel lblMinute = new JLabel("Minute");
    private JLabel lblSecond = new JLabel("Second");
    private JTextField txtHour = new JTextField("9", 3);
    private JTextField txtMinute = new JTextField("24", 3);
    private JTextField txtSecond = new JTextField("49", 3);
    
    public void AlarmClockReset() {    // this is constructor - it cannot be void --   this was the main thing - surprised that compiler tdid not complain - it was very difficult to spot

        btnOK.addActionListener(this);
        btnCancel.addActionListener(this);
        
        JFrame frm = new JFrame("Set Alarm");      //  in constructor extending  JFrame - you do not create new JFrame - you add components to "this" JFrame
  // not to new JFrame 
        
        JPanel p1 = new JPanel(new BorderLayout()); 
 
        p1.add(lblInstruction, BorderLayout.NORTH);
        JPanel p2 = new JPanel(new GridLayout(4, 2, 0, 0));
        p2.add(lblHour); p2.add(txtHour);
        p2.add(lblMinute); p2.add(txtMinute);
        p2.add(lblSecond); p2.add(txtSecond);
        p2.add(btnOK); p2.add(btnCancel);
        frm.add(p1, BorderLayout.NORTH);  //  you should not not be adding to some external jframe
        frm.add(p2, BorderLayout.SOUTH);  //  you should be adding to this child of JFrame that you are creating right now
// so in Swing - you shoud first obtain Container from yout top-level window
 Conatiner c = this.getContentPane();
//and then you add your first panel to container

//The main thing about BorderLayout is usually its central element - main element
//and then "decorations" along the boerders
// maybe you can have BioorderLayed out element without Center but it is highly unusuall



    }    
    

Open in new window

Mike EghtebasDatabase and Application Developer

Author

Commented:
I saw a solution from you to another user where you write (screen width - form width)/2 ...

I will try to locate it and use it I will be fine in this regard.
Awarded 2011
Awarded 2011

Commented:
yes that will work if the user (I mean the user of the application) does not
move the iroginal window after it appeared - then you amy assume that it appeared in the center and you
can caclaulate where to put new window, but I'm afraid, if the user moves, then
applet will not track the new location of the original window
Mike EghtebasDatabase and Application Developer

Author

Commented:
Thank you