|
-
October 22nd, 2005, 12:44 AM
#1
get window state with multiple frames
Hello again... ;-)
Can someone point me to a good tutorial on using multiple JFrames in a Java app?
I am trying to write an app that opens multiple JFrames (depending on which button the user clicks from the main JFrame). So far everything works just fine, however, I want to prevent users from being able to open multiple instances of the same frame. Right now its not a bother, as the frames don't do anything, but eventually, this will access a database, and I dont want a user to be able to open 100 different frames accessing the same database...
I have looked at various listeners, and have implemented a few of them, but the problem I am having is determining WHAT frame is sending the action. doing an event.getSource() in the windowEvent handler gives me something like this:
myproject.formCheckAvailability[lots of configuration stuff here]
I set a name for each form using form.setName(), but I cant seem to pull that out.
Here is what I have so far. Please ignore most of it as it does not really do anything yet, and I just started on it this evening:
Code:
package airvendettaseatingsystem;
import javax.swing.*;
import javax.swing.event.*;
import java.awt.event.*;
import java.awt.*;
public class mainForm extends javax.swing.JFrame implements java.awt.event.ActionListener, java.awt.event.WindowListener {
public int checkState = 0;
public int viewState = 0;
public int reserveState = 0;
public int cancelState = 0;
frmCheck newCheck;
frmView newView;
/** Creates new form mainForm */
public mainForm() {
initComponents();
}
public void windowActivated(WindowEvent es) {
System.out.println("Activated "+es.getSource());
System.out.println(es.getID());
System.out.println(es.getComponent());
}
public void windowDeactivated(WindowEvent eh) {
System.out.println("Deactivated "+eh.getSource());
System.out.println(eh.getID());
System.out.println(eh.getComponent());
}
public void windowIconified(WindowEvent em) {
System.out.println("Iconified "+em.getSource());
}
public void windowDeiconified(WindowEvent er) {
System.out.println("Deiconified "+er.getSource());
}
public void windowClosed(WindowEvent wc) {
System.out.println(wc.getSource());
}
public void windowClosing(WindowEvent wi) {
System.out.println(wi.getSource());
}
public void windowOpened(WindowEvent wo) {
if (wo.getSource() == newView)
System.out.println("GOTCHA");
}
public void actionPerformed(ActionEvent event){
System.out.println(event.getSource());
if (event.getSource() == btnCheck){
// if (checkState == 0){
System.out.println("Check Button");
frmCheck newCheck = new frmCheck();
newCheck.setName("myCheck");
newCheck.addWindowListener(this);
newCheck.setLocationRelativeTo(this);
newCheck.setVisible(true);
checkState = 1;
// }
}
if (event.getSource() == btnView){
System.out.println("View Button");
frmView newView = new frmView();
newView.addWindowListener(this);
newView.setVisible(true);}
if (event.getSource() == btnReserve)
System.out.println("Reserve Button");
if (event.getSource() == btnCancel)
System.out.println("Cancel Button");
if (event.getSource() == btnExit)
dispose();
}
/** 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() {//GEN-BEGIN:initComponents
pnlMain = new javax.swing.JPanel();
btnCheck = new javax.swing.JButton();
lblCheck = new javax.swing.JLabel();
lblView = new javax.swing.JLabel();
btnView = new javax.swing.JButton();
btnReserve = new javax.swing.JButton();
lblReserve = new javax.swing.JLabel();
btnCancel = new javax.swing.JButton();
btnExit = new javax.swing.JButton();
lblCancel = new javax.swing.JLabel();
lblExit = new javax.swing.JLabel();
getContentPane().setLayout(new org.netbeans.lib.awtextra.AbsoluteLayout());
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
setTitle("Air Vendetta Seating Reservation System");
setName("mainForm");
setResizable(false);
pnlMain.setLayout(new org.netbeans.lib.awtextra.AbsoluteLayout());
pnlMain.setBorder(new javax.swing.border.CompoundBorder(new javax.swing.border.SoftBevelBorder(javax.swing.border.BevelBorder.RAISED, new java.awt.Color(255, 102, 255), new java.awt.Color(204, 0, 255), new java.awt.Color(204, 0, 204), new java.awt.Color(102, 0, 102)), new javax.swing.border.LineBorder(new java.awt.Color(102, 153, 255), 2, true)));
pnlMain.setMinimumSize(new java.awt.Dimension(300, 190));
pnlMain.setOpaque(false);
pnlMain.setPreferredSize(new java.awt.Dimension(275, 220));
btnCheck.setBackground(new java.awt.Color(255, 255, 255));
btnCheck.setText("Check");
btnCheck.setToolTipText("Click here to check availability");
btnCheck.setActionCommand("btnCheck");
btnCheck.setBorder(new javax.swing.border.SoftBevelBorder(javax.swing.border.BevelBorder.RAISED, new java.awt.Color(153, 255, 255), new java.awt.Color(102, 153, 255), new java.awt.Color(153, 0, 204), new java.awt.Color(255, 0, 255)));
btnCheck.setDefaultCapable(false);
btnCheck.setNextFocusableComponent(btnView);
btnCheck.setVerifyInputWhenFocusTarget(false);
btnCheck.addActionListener(this);
pnlMain.add(btnCheck, new org.netbeans.lib.awtextra.AbsoluteConstraints(20, 30, 70, 20));
lblCheck.setLabelFor(btnCheck);
lblCheck.setText("Check seat availability");
lblCheck.setFocusable(false);
lblCheck.setRequestFocusEnabled(false);
lblCheck.setVerifyInputWhenFocusTarget(false);
pnlMain.add(lblCheck, new org.netbeans.lib.awtextra.AbsoluteConstraints(100, 30, 150, 20));
lblView.setLabelFor(btnView);
lblView.setText("View seating chart");
lblView.setFocusable(false);
lblView.setRequestFocusEnabled(false);
lblView.setVerifyInputWhenFocusTarget(false);
pnlMain.add(lblView, new org.netbeans.lib.awtextra.AbsoluteConstraints(100, 60, 120, 20));
btnView.setBackground(new java.awt.Color(255, 255, 255));
btnView.setText("View");
btnView.setToolTipText("Click here to view a seating chart");
btnView.setActionCommand("btnView");
btnView.setBorder(new javax.swing.border.SoftBevelBorder(javax.swing.border.BevelBorder.RAISED, new java.awt.Color(153, 255, 255), new java.awt.Color(102, 153, 255), new java.awt.Color(153, 0, 204), new java.awt.Color(255, 0, 255)));
btnView.setDefaultCapable(false);
btnView.setNextFocusableComponent(btnReserve);
btnView.setVerifyInputWhenFocusTarget(false);
btnView.addActionListener(this);
pnlMain.add(btnView, new org.netbeans.lib.awtextra.AbsoluteConstraints(20, 60, 70, 20));
btnReserve.setBackground(new java.awt.Color(255, 255, 255));
btnReserve.setText("Reserve");
btnReserve.setToolTipText("Click here to set up a reservation");
btnReserve.setActionCommand("btnReserve");
btnReserve.setBorder(new javax.swing.border.SoftBevelBorder(javax.swing.border.BevelBorder.RAISED, new java.awt.Color(153, 255, 255), new java.awt.Color(102, 153, 255), new java.awt.Color(153, 0, 204), new java.awt.Color(255, 0, 255)));
btnReserve.setDefaultCapable(false);
btnReserve.setNextFocusableComponent(btnCancel);
btnReserve.setVerifyInputWhenFocusTarget(false);
btnReserve.addActionListener(this);
pnlMain.add(btnReserve, new org.netbeans.lib.awtextra.AbsoluteConstraints(20, 90, 70, 20));
lblReserve.setLabelFor(btnReserve);
lblReserve.setText("Reserve a seat");
lblReserve.setFocusable(false);
lblReserve.setRequestFocusEnabled(false);
lblReserve.setVerifyInputWhenFocusTarget(false);
pnlMain.add(lblReserve, new org.netbeans.lib.awtextra.AbsoluteConstraints(100, 90, 120, 20));
btnCancel.setBackground(new java.awt.Color(255, 255, 255));
btnCancel.setText("Cancel");
btnCancel.setToolTipText("Click here to cancel a reservation");
btnCancel.setActionCommand("btnCancel");
btnCancel.setBorder(new javax.swing.border.SoftBevelBorder(javax.swing.border.BevelBorder.RAISED, new java.awt.Color(153, 255, 255), new java.awt.Color(102, 153, 255), new java.awt.Color(153, 0, 204), new java.awt.Color(255, 0, 255)));
btnCancel.setDefaultCapable(false);
btnCancel.setNextFocusableComponent(btnExit);
btnCancel.setVerifyInputWhenFocusTarget(false);
btnCancel.addActionListener(this);
pnlMain.add(btnCancel, new org.netbeans.lib.awtextra.AbsoluteConstraints(20, 120, 70, 20));
btnExit.setBackground(new java.awt.Color(255, 255, 255));
btnExit.setText("Exit");
btnExit.setToolTipText("Click here to exit application");
btnExit.setActionCommand("btnExit");
btnExit.setBorder(new javax.swing.border.SoftBevelBorder(javax.swing.border.BevelBorder.RAISED, new java.awt.Color(153, 255, 255), new java.awt.Color(102, 153, 255), new java.awt.Color(153, 0, 204), new java.awt.Color(255, 0, 255)));
btnExit.setDefaultCapable(false);
btnExit.setNextFocusableComponent(btnCheck);
btnExit.setVerifyInputWhenFocusTarget(false);
btnExit.addActionListener(this);
pnlMain.add(btnExit, new org.netbeans.lib.awtextra.AbsoluteConstraints(20, 170, 70, 20));
lblCancel.setLabelFor(btnCancel);
lblCancel.setText("Cancel a reservation");
lblCancel.setFocusable(false);
lblCancel.setRequestFocusEnabled(false);
lblCancel.setVerifyInputWhenFocusTarget(false);
pnlMain.add(lblCancel, new org.netbeans.lib.awtextra.AbsoluteConstraints(100, 120, 120, 20));
lblExit.setLabelFor(btnExit);
lblExit.setText("Exit Application");
lblExit.setFocusable(false);
lblExit.setRequestFocusEnabled(false);
lblExit.setVerifyInputWhenFocusTarget(false);
pnlMain.add(lblExit, new org.netbeans.lib.awtextra.AbsoluteConstraints(100, 170, 120, 20));
getContentPane().add(pnlMain, new org.netbeans.lib.awtextra.AbsoluteConstraints(0, 0, -1, -1));
pack();
}//GEN-END:initComponents
/**
* @param args the command line arguments
*/
public static void main(String args[]) {
// java.awt.EventQueue.invokeLater(new Runnable() {
// public void run() {
// new mainForm().setVisible(true);
// }
// });
}
// Variables declaration - do not modify//GEN-BEGIN:variables
private javax.swing.JButton btnCancel;
private javax.swing.JButton btnCheck;
private javax.swing.JButton btnExit;
private javax.swing.JButton btnReserve;
private javax.swing.JButton btnView;
private javax.swing.JLabel lblCancel;
private javax.swing.JLabel lblCheck;
private javax.swing.JLabel lblExit;
private javax.swing.JLabel lblReserve;
private javax.swing.JLabel lblView;
private javax.swing.JPanel pnlMain;
// End of variables declaration//GEN-END:variables
}
Basically, there is a main class that does nothign but start a thread that runs frmMain. frmMain contains buttons that launch other JFrames to accomplish certain things. The code, as it stands now, DOES work, but I simply cant figure out how to implement the listner, or more specifically, I cant figure out how to find a usable identifier from the window that passes the event (such as hide, close, minimize, and so on).
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|