Meaningless Drivel is fun!*
The moose likes Swing / AWT / SWT and the fly likes call another form/frame(?) show method Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "call another form/frame(?) show method" Watch "call another form/frame(?) show method" New topic
Author

call another form/frame(?) show method

faisal henry
Greenhorn

Joined: Feb 15, 2004
Posts: 3
hi,
i'm very nu to java so please bear with me on this one.
I am trying to develop a windows Swing app but unfortunately almost all of the examples that i found works on a single window.
I need to create a multi window (not necessarily a MDI) application.
suppose i got a mainWindow and secondWindow.
I place a button on mainWindow, if i click on it, how do i call the secondWindow to appear?
this is very easy to do in delphi, just type secondWindow.show and that's it.
I'm sure this is also easy to do in java, just do not know how.
Gregg Bolinger
GenRocket Founder
Ranch Hand

Joined: Jul 11, 2001
Posts: 15299
    
    6

Hi nu toJava. Don't forget about the request in your other thread.
Opening another JFrame from 1 JFrame is pretty simple. You do it the same way you opened your initial JFrame. So I will show you the code for your ActionEvent in your JButton click event.

There are dozens of ways to do this. This is only 1 of them. You could also have another class that subclasses JFrame and so you would do

Also, you could include all the other method calls in the constructor of your 2nd frame. setVisible(), pack(), setBounds(), etc can all be in the constructor of your 2nd frame. That makes it easier so you can just use 1 statement to open your new frame.
If you have some more problems, post your code and tell us where you are mixed up and we will be glad to help.
More so even if you do that little name change. Hint hint.


GenRocket - Experts at Building Test Data
ndut cantik
Greenhorn

Joined: Feb 16, 2004
Posts: 2
Greg,
I have tried your code, but i do not think that this is what he wants.
Actually i have the same problem.
I am just learning java, my background is using delphi and VB.
Your code will create an empty frame.
I have created 2 forms. frmMain and frmData.
Suppose i place a button on frmMain, how do i call the frmData show method or make it visible when i click the button?
Gregg Bolinger
GenRocket Founder
Ranch Hand

Joined: Jul 11, 2001
Posts: 15299
    
    6

Originally posted by ndut cantik:
Greg,
I have tried your code, but i do not think that this is what he wants.
Actually i have the same problem.
I am just learning java, my background is using delphi and VB.
Your code will create an empty frame.
I have created 2 forms. frmMain and frmData.
Suppose i place a button on frmMain, how do i call the frmData show method or make it visible when i click the button?

It depends. What type of parent component are we talking about for frmData? Is it a JFrame? Do you have it in a class of it's own? Does the class sublcass JFrame?
Like I said in my previous post, if you have created another Class that subclasses JFrame and that has your form data, you need to instantiate that class as in:
MyFrame frame = new MyFrame();
And all your form components should be part of the MyFrame class.
I know that
JFrame frame = new JFrame()
only opens and Empty JFrame. But faisal only asked how to open a second Window. He said nothing about it needing to be populated with any form components.
Neither of you has really provided enough information to make more than an educated guess about how your problems. So that is what I did. I guessed. Both my solutions work depending on your own implementation.
ndut cantik
Greenhorn

Joined: Feb 16, 2004
Posts: 2
Hi Greg,
below is my code, i created it using Forte 4.
Basically what i need is a way of calling a predefined form.
frmMain has a button that if it is clicked will call frmData.
A pretty straightforward problem that should have a straightforward solution, problem is i'm pretty much clueless on java.
package UI;
/**
*
*
*/
public class frmMain extends javax.swing.JFrame {

/** Creates new form frmMain */
public frmMain() {
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() {
jButton1 = new javax.swing.JButton();
jScrollPane1 = new javax.swing.JScrollPane();
jTable1 = new javax.swing.JTable();
getContentPane().setLayout(new org.netbeans.lib.awtextra.AbsoluteLayout());
addWindowListener(new java.awt.event.WindowAdapter() {
public void windowClosing(java.awt.event.WindowEvent evt) {
exitForm(evt);
}
});
jButton1.setText("jButton1");
jButton1.addMouseListener(new java.awt.event.MouseAdapter() {
public void mouseClicked(java.awt.event.MouseEvent evt) {
jButton1MouseClicked(evt);
}
});
getContentPane().add(jButton1, new org.netbeans.lib.awtextra.AbsoluteConstraints(280, 230, -1, -1));
jTable1.setModel(new javax.swing.table.DefaultTableModel(
new Object [][] {
{null, null, null, null},
{null, null, null, null},
{null, null, null, null},
{null, null, null, null}
},
new String [] {
"Title 1", "Title 2", "Title 3", "Title 4"
}
));
jScrollPane1.setViewportView(jTable1);
getContentPane().add(jScrollPane1, new org.netbeans.lib.awtextra.AbsoluteConstraints(80, 60, 270, 110));
pack();
}
private void jButton1MouseClicked(java.awt.event.MouseEvent evt) {
UI.
}

/** Exit the Application */
private void exitForm(java.awt.event.WindowEvent evt) {
System.exit(0);
}

/**
* @param args the command line arguments
*/
public static void main(String args[]) {
new frmMain().show();
}


// Variables declaration - do not modify
private javax.swing.JButton jButton1;
private javax.swing.JScrollPane jScrollPane1;
private javax.swing.JTable jTable1;
// End of variables declaration

}

package UI;
/**
*
* */
public class frmData extends javax.swing.JDialog {

/** Creates new form frmData */
public frmData(java.awt.Frame parent, boolean modal) {
super(parent, modal);
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() {
jButton1 = new javax.swing.JButton();
getContentPane().setLayout(new org.netbeans.lib.awtextra.AbsoluteLayout());
addWindowListener(new java.awt.event.WindowAdapter() {
public void windowClosing(java.awt.event.WindowEvent evt) {
closeDialog(evt);
}
});
jButton1.setText("jButton1");
getContentPane().add(jButton1, new org.netbeans.lib.awtextra.AbsoluteConstraints(40, 250, -1, -1));
pack();
}

/** Closes the dialog */
private void closeDialog(java.awt.event.WindowEvent evt) {
setVisible(false);
dispose();
}

/**
* @param args the command line arguments
*/
public static void main(String args[]) {
new frmData(new javax.swing.JFrame(), true).show();
}


// Variables declaration - do not modify
private javax.swing.JButton jButton1;
// End of variables declaration

}
Gregg Bolinger
GenRocket Founder
Ranch Hand

Joined: Jul 11, 2001
Posts: 15299
    
    6

Ok, your first problem is that you are using Forte 4. The code it generates is absolutly horrid. Why one earth would you use a MouseAdapter to capture Button click events instead of using an ActionListener? That's insane. If you ever read Sun's SWING Tutorial, which I highly recommend for you, nowhere in there do they do code like Forte. Oddly, Forte was made by Sun...
Anyway, I have already answered this question. But I will do it again.
Here is the code for our initial JFrame. It is very simple. It only contains a single Button right now. There could be typos because I typed it in WordPad and haven't compiled. But the point of all this is there:

Now we will do the DataForm class

And that's it. Notice the JButton's action Listener in the MainForm class. That is how you call your Data form and get it to show up.
Now, here is what I recommend.
  • Lose Forte. Use something like JCreator or even just notepad if you are in windows. If you are in Linux use VI.
  • Go through the SWING tutorial I linked to above


  • If you are going to grasp all of this you need to have an understanding of what Forte is doing. And the only way to do that is to not use Forte. Once you understand what it happenening, well, you won't want to use Forte by then, but you could move on to a real editor like IDEA, Eclipse, or Netbeans. If you have any more questions, let me know.
    [ February 17, 2004: Message edited by: Gregg Bolinger ]
     
    Don't get me started about those stupid light bulbs.
     
    subject: call another form/frame(?) show method