Button to open JFrame

Hi

I’m new to Java, and I’m trying to make a jForm which has a button. When this button is pressed, I want it to open a new jForm, which is in a different class file in the same package.

I can do things with buttons within the same frame, but trying to open a new frame just results in nothing happening…

I’m using Netbeans 6.5 and Java 1.6.

Thanks for any help!
BobsyB

Would you be able to post any of your code to help us see what is happening?

I have a form with a button, in which I put some code like:

newRecord nr = new newRecord();
nr.setVisible(true);

newRecord.java is in the same package and contains the following (mainly auto generated) code:

package scheduler;

public class newRecord extends javax.swing.JPanel {

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

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

    newRec1 = new javax.swing.JTextField();
    newRec2 = new javax.swing.JTextField();
    newRec3 = new javax.swing.JTextField();
    jLabel1 = new javax.swing.JLabel();
    jLabel2 = new javax.swing.JLabel();
    jLabel3 = new javax.swing.JLabel();
    jLabel4 = new javax.swing.JLabel();
    closeNoSave = new javax.swing.JButton();
    closeSave = new javax.swing.JButton();

    setName("Form"); // NOI18N

    org.jdesktop.application.ResourceMap resourceMap = org.jdesktop.application.Application.getInstance(scheduler.SchedulerApp.class).getContext().getResourceMap(newRecord.class);
    newRec1.setText(resourceMap.getString("newRec1.text")); // NOI18N
    newRec1.setName("newRec1"); // NOI18N

    newRec2.setText(resourceMap.getString("newRec2.text")); // NOI18N
    newRec2.setName("newRec2"); // NOI18N

    newRec3.setText(resourceMap.getString("newRec3.text")); // NOI18N
    newRec3.setName("newRec3"); // NOI18N

    jLabel1.setText(resourceMap.getString("jLabel1.text")); // NOI18N
    jLabel1.setName("jLabel1"); // NOI18N

    jLabel2.setText(resourceMap.getString("jLabel2.text")); // NOI18N
    jLabel2.setName("jLabel2"); // NOI18N

    jLabel3.setText(resourceMap.getString("jLabel3.text")); // NOI18N
    jLabel3.setName("jLabel3"); // NOI18N

    jLabel4.setFont(resourceMap.getFont("jLabel4.font")); // NOI18N
    jLabel4.setText(resourceMap.getString("jLabel4.text")); // NOI18N
    jLabel4.setName("jLabel4"); // NOI18N

    closeNoSave.setText(resourceMap.getString("closeNoSave.text")); // NOI18N
    closeNoSave.setName("closeNoSave"); // NOI18N
    closeNoSave.addMouseListener(new java.awt.event.MouseAdapter() {
        public void mouseClicked(java.awt.event.MouseEvent evt) {
            closeNoSaveMouseClicked(evt);
        }
    });

    closeSave.setText(resourceMap.getString("closeSave.text")); // NOI18N
    closeSave.setName("closeSave"); // NOI18N
    closeSave.addMouseListener(new java.awt.event.MouseAdapter() {
        public void mouseClicked(java.awt.event.MouseEvent evt) {
            closeSaveMouseClicked(evt);
        }
    });

    javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
    this.setLayout(layout);
    layout.setHorizontalGroup(
        layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
        .addGroup(layout.createSequentialGroup()
            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(layout.createSequentialGroup()
                    .addGap(151, 151, 151)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                        .addComponent(jLabel2)
                        .addComponent(jLabel3)
                        .addComponent(jLabel1))
                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addComponent(newRec1, javax.swing.GroupLayout.DEFAULT_SIZE, 208, Short.MAX_VALUE)
                        .addComponent(newRec2, javax.swing.GroupLayout.DEFAULT_SIZE, 208, Short.MAX_VALUE)
                        .addComponent(newRec3, javax.swing.GroupLayout.DEFAULT_SIZE, 208, Short.MAX_VALUE)
                        .addGroup(layout.createSequentialGroup()
                            .addComponent(closeNoSave)
                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 60, Short.MAX_VALUE)
                            .addComponent(closeSave))))
                .addGroup(layout.createSequentialGroup()
                    .addGap(22, 22, 22)
                    .addComponent(jLabel4)))
            .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
    );
    layout.setVerticalGroup(
        layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
        .addGroup(layout.createSequentialGroup()
            .addContainerGap()
            .addComponent(jLabel4)
            .addGap(29, 29, 29)
            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                .addComponent(newRec1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addComponent(jLabel1))
            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                .addComponent(newRec2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addComponent(jLabel2))
            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                .addComponent(newRec3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addComponent(jLabel3))
            .addGap(53, 53, 53)
            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                .addComponent(closeNoSave)
                .addComponent(closeSave))
            .addContainerGap(30, Short.MAX_VALUE))
    );
}// </editor-fold>

private void closeNoSaveMouseClicked(java.awt.event.MouseEvent evt) {
    // TODO add your handling code here:

}

private void closeSaveMouseClicked(java.awt.event.MouseEvent evt) {
    // create new record
    PageSheet newPage = new PageSheet();
    //create input array
    String[] newPageText;
    newPageText = new String[3];
    //populate array with data
    newPageText[0] = newRec1.getText();
    newPageText[1] = newRec2.getText();
    newPageText[2] = newRec3.getText();
    //populate the newPage object with the data array
}
// Variables declaration - do not modify
private javax.swing.JButton closeNoSave;
private javax.swing.JButton closeSave;
private javax.swing.JLabel jLabel1;
private javax.swing.JLabel jLabel2;
private javax.swing.JLabel jLabel3;
private javax.swing.JLabel jLabel4;
private javax.swing.JTextField newRec1;
private javax.swing.JTextField newRec2;
private javax.swing.JTextField newRec3;
// End of variables declaration

}

And so what is the problem?

The new Record set isn’t reacting to the other frame? Or is something else not happening correctly?

Btw, that code is quite hard to read.

The problem is that clicking the button to open a new frame does nothing.

Apologies for the difficult to read code, it’s why I didn’t post it originally…

So the two methods that your Anonymus MouseAdapters call aren’t really doing anything.


private void closeNoSaveMouseClicked(java.awt.event.MouseEvent evt) {
  // TODO add your handling code here:
}

private void closeSaveMouseClicked(java.awt.event.MouseEvent evt) {
  // create new record
  PageSheet newPage = new PageSheet();
  //create input array
  String[] newPageText;
  newPageText = new String[3];
  //populate array with data
  newPageText[0] = newRec1.getText();
  newPageText[1] = newRec2.getText();
  newPageText[2] = newRec3.getText();
  //populate the newPage object with the data array
}

As you can see, nothing interesting is happening above.

I would change your code to the following just to see if your two MouseAdapters are even receiving the events you’re hoping they are…


private void closeNoSaveMouseClicked(java.awt.event.MouseEvent evt) {
  System.out.println("closeNoSaveMouseClicked: " + evt);
}

private void closeSaveMouseClicked(java.awt.event.MouseEvent evt) {
  System.out.println("closeSaveMouseClicked: " + evt);
}

Then see if either of the above messages are printed in your console when they are clicked.

The code I posted is actually the panel I’m trying to open from a different form. The buttons on the form work when I do something like output text to the console, or make some change on that form. The problem is, I don’t know how to open a new form from those buttons. I’ve tried doing something like:

private void buttonOpenFormMouseClicked(java.awt.event.MouseEvent evt) {
System.out.println(“Open Form Mouse Clicked”);
newRecord nr = new newRecord();
System.out.println(“Button Clicked”);
}

where newRecord is the new form I’m trying to open. This gives output:

Open Form Mouse Clicked
Button Clicked

but no newRecord form and no error messages.

It looks like your NewRecord class is actually a JPanel instead of a JFrame. That might have something to do with your problems.