So I have been messing around with Swing and am starting to grasp general functions with buttons and text boxes etc..
But what I want to do is have a main page (JFrame Form) with 3 buttons and when I click whichever button I want it to open another JFrame form with the options corresponding to that button but cant figure out how to link 2 JFrame forms together and so I can make the appropriate ones visible / invisible when the appropriate button is pressed?
Thanks,
BestJewSinceJC 700 Posting Maven
It sounds like you are using netbeans. If you "select" the button so that it is orange, then double click it (or click on 'source'), you will see something that says jButton1ActionPerformed (or whatever the name of it is). Assuming you've already created whatever other window you want to open, and it is in the same Netbeans project, code to open the window would look like this
public void jButton1ActionPerformed(ActionEvent evt){
new SecondScreen();
}
This is assuming your netbeans hierarchy looks like this:
MyProject
-FirstScreen.java
-SecondScreen.java
-ThirdScreen.java
I hope that makes sense. If you aren't using netbeans, my apologies (your use of 'JFrame forms' is netbeans terminology, I think). But I'd recommend reading the swing tutorials anyway, and coding some GUIs without netbeans. It will teach you a lot more than netbeans will.
jackiejoe 0 Light Poster
Thanks for the help, I agree about coding it myself but I make sure I understand all the code netbeans creates.
I tried that , no errors but the button doesnt do anything. Here is my code the other form is called "Couple.java"
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
/*
* Main.java
*
* Created on 20-Mar-2009, 22:10:36
*/
/**
*
* @author Jack
*/
public class Main extends javax.swing.JFrame {
/** Creates new form Main */
public Main() {
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.
*/
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {
jButton1 = new javax.swing.JButton();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
addComponentListener(new java.awt.event.ComponentAdapter() {
public void componentHidden(java.awt.event.ComponentEvent evt) {
formComponentHidden(evt);
}
});
jButton1.setText("Click Here!!!");
jButton1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton1ActionPerformed(evt);
}
});
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(167, 167, 167)
.addComponent(jButton1)
.addContainerGap(98, Short.MAX_VALUE))
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(52, 52, 52)
.addComponent(jButton1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addGap(149, 149, 149))
);
pack();
}// </editor-fold>
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
new Couple();
}
private void formComponentHidden(java.awt.event.ComponentEvent evt) {
// TODO add your handling code here:
}
/**
* @param args the command line arguments
*/
public static void main(String args[]) {
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new Main().setVisible(true);
}
});
}
// Variables declaration - do not modify
private javax.swing.JButton jButton1;
// End of variables declaration
}
JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster
Check that your code for Couple has a setVisible(true) in its constructor
weblover 0 Junior Poster
create for each new screen you want to show a new class that extends from JFrame ...and in the button that you want to click to show the second screen put the ActionListener to call the new class ..i tried it in my projet and it works very well.
Be a part of the DaniWeb community
We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.