Results 1 to 11 of 11
  1. #1
    TheV909's Avatar
    TheV909 is offline TwinkleToes
    Join Date
    May 2011
    Location
    Pennsylvania
    Posts
    7
    Rep Power
    0

    Exclamation JFrame button question!

    I'm working on a simple artificial intelligence program as my final project for my Java class. I am currently having trouble locating example code both in my textbook and on the internet. I am trying to make an existing window open when I click on a JButton in another window. Nothing I have tried has worked, so if someone could simply walk me through it or just post a code snippet i'd be much obliged! Thanks for your time and hopefully speedy response :)

  2. #2
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    Is the original window open and minimized? Or closed?

  3. #3
    razmyasdfg is offline Member
    Join Date
    Jul 2008
    Location
    London (Kingsbury)
    Posts
    41
    Rep Power
    0

    Default

    if you could post your code. it would be easy to assist you

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by TheV909 View Post
    I'm working on a simple artificial intelligence program as my final project for my Java class. I am currently having trouble locating example code both in my textbook and on the internet. I am trying to make an existing window open when I click on a JButton in another window. Nothing I have tried has worked, so if someone could simply walk me through it or just post a code snippet i'd be much obliged! Thanks for your time and hopefully speedy response :)
    What do you mean by "make an existing window open"? Another JFrame? A JPanel on an existing JFrame? A JDialog?

    Also, you will learn much more if we work from your "snippet of code" rather than our simply providing code. So let's see what you've tried so far.

  5. #5
    TheV909's Avatar
    TheV909 is offline TwinkleToes
    Join Date
    May 2011
    Location
    Pennsylvania
    Posts
    7
    Rep Power
    0

    Default

    I'll post the code in a second, but the original window is minimized, we will have the user input his/her name and they will be stored in a text file. After they input their name we want the user to be able to click on a button that will open the next window we have. I'm compiling both mine and my partner's code thus far so....
    Why you mad ;)?

  6. #6
    TheV909's Avatar
    TheV909 is offline TwinkleToes
    Join Date
    May 2011
    Location
    Pennsylvania
    Posts
    7
    Rep Power
    0

    Default

    Oh and it's another JFrame we're trying to open, we've only worked at it for 2 days so our code is kinda dismal this far.
    Why you mad ;)?

  7. #7
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    If you have a series of windows that should change as the user makes choices you may want to look into card layout.

    Also, looking up "iconify java" may be helpful.
    Last edited by sunde887; 05-18-2011 at 02:18 PM.

  8. #8
    TheV909's Avatar
    TheV909 is offline TwinkleToes
    Join Date
    May 2011
    Location
    Pennsylvania
    Posts
    7
    Rep Power
    0

    Default

    import java.io.FileWriter;
    import java.io.IOException;
    import java.io.PrintWriter;
    import java.util.logging.Level;
    import java.util.logging.Logger;
    /**
    *
    * @author 655788
    */
    public class NamePrompt extends javax.swing.JFrame
    {
    private String firstname;



    FileWriter NameOutput = new FileWriter("UserNames.txt", true);
    PrintWriter NamesOutput = new PrintWriter(NameOutput);
    /** Creates new form NamePrompt */
    public NamePrompt() throws IOException
    {
    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() {

    jLabel2 = new javax.swing.JLabel();
    first = new javax.swing.JTextField();
    jLabel3 = new javax.swing.JLabel();
    jTextField2 = new javax.swing.JTextField();
    jButton1 = new javax.swing.JButton();
    jButton2 = new javax.swing.JButton();
    jLabel1 = new javax.swing.JLabel();
    jProgressBar1 = new javax.swing.JProgressBar();
    jMenuBar1 = new javax.swing.JMenuBar();
    jMenu1 = new javax.swing.JMenu();
    jMenuItem1 = new javax.swing.JMenuItem();
    jMenu2 = new javax.swing.JMenu();

    setDefaultCloseOperation(javax.swing.WindowConstan ts.EXIT_ON_CLOSE);

    jLabel2.setText("First:");

    first.addActionListener(new java.awt.event.ActionListener() {
    public void actionPerformed(java.awt.event.ActionEvent evt) {
    firstnametextfieldactionevent(evt);
    }
    });

    jLabel3.setText("Last:");

    jTextField2.addActionListener(new java.awt.event.ActionListener() {
    public void actionPerformed(java.awt.event.ActionEvent evt) {
    lastnametextfieldactionevent(evt);
    }
    });

    jButton1.setText("Enter");
    jButton1.addActionListener(new java.awt.event.ActionListener() {
    public void actionPerformed(java.awt.event.ActionEvent evt) {
    enterbuttonActionPerformed(evt);
    }
    });
    jButton1.addMouseListener(new java.awt.event.MouseAdapter() {
    public void mouseClicked(java.awt.event.MouseEvent evt) {
    jButton1MouseClicked(evt);
    }
    });

    jButton2.setText("Quit");
    jButton2.addActionListener(new java.awt.event.ActionListener() {
    public void actionPerformed(java.awt.event.ActionEvent evt) {
    jButton2ActionPerformed(evt);
    }
    });

    jLabel1.setFont(new java.awt.Font("Tahoma", 0, 14));
    jLabel1.setText("Name");

    jMenu1.setText("File");

    jMenuItem1.setAccelerator(javax.swing.KeyStroke.ge tKeyStroke(java.awt.event.KeyEvent.VK_Q, java.awt.event.InputEvent.CTRL_MASK));
    jMenuItem1.setText("Quit");
    jMenuItem1.addActionListener(new java.awt.event.ActionListener() {
    public void actionPerformed(java.awt.event.ActionEvent evt) {
    quitmenuoptionActionEvent(evt);
    }
    });
    jMenu1.add(jMenuItem1);

    jMenuBar1.add(jMenu1);

    jMenu2.setText("Edit");
    jMenuBar1.add(jMenu2);

    setJMenuBar(jMenuBar1);

    org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(getContentPane());
    getContentPane().setLayout(layout);
    layout.setHorizontalGroup(
    layout.createParallelGroup(org.jdesktop.layout.Gro upLayout.LEADING)
    .add(layout.createSequentialGroup()
    .add(10, 10, 10)
    .add(jLabel1))
    .add(layout.createSequentialGroup()
    .add(10, 10, 10)
    .add(jLabel2)
    .add(4, 4, 4)
    .add(first, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 121, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
    .add(layout.createSequentialGroup()
    .add(10, 10, 10)
    .add(jLabel3)
    .add(4, 4, 4)
    .add(jTextField2, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 122, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
    .add(layout.createSequentialGroup()
    .add(10, 10, 10)
    .add(jButton1)
    .add(68, 68, 68)
    .add(jButton2))
    .add(jProgressBar1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 200, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
    );
    layout.setVerticalGroup(
    layout.createParallelGroup(org.jdesktop.layout.Gro upLayout.LEADING)
    .add(layout.createSequentialGroup()
    .add(11, 11, 11)
    .add(jLabel1)
    .add(11, 11, 11)
    .add(layout.createParallelGroup(org.jdesktop.layou t.GroupLayout.LEADING)
    .add(layout.createSequentialGroup()
    .add(2, 2, 2)
    .add(jLabel2))
    .add(first, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
    .add(15, 15, 15)
    .add(layout.createParallelGroup(org.jdesktop.layou t.GroupLayout.LEADING)
    .add(layout.createSequentialGroup()
    .add(2, 2, 2)
    .add(jLabel3))
    .add(jTextField2, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
    .add(22, 22, 22)
    .add(layout.createParallelGroup(org.jdesktop.layou t.GroupLayout.LEADING)
    .add(jButton1)
    .add(jButton2))
    .add(11, 11, 11)
    .add(jProgressBar1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
    );

    pack();
    }// </editor-fold>

    private void firstnametextfieldactionevent(java.awt.event.Actio nEvent evt) {

    }

    private void enterbuttonActionPerformed(java.awt.event.ActionEv ent evt) {
    //MainGUI = new MainGUI();

    String lastname = jTextField2.getText();
    NamesOutput.printf("%s" + " %s" + "%n", firstname, lastname);
    }

    private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {
    System.exit(0); // TODO add your handling code here:
    }

    private void lastnametextfieldactionevent(java.awt.event.Action Event evt) {
    // TODO add your handling code here:
    }

    private void quitmenuoptionActionEvent(java.awt.event.ActionEve nt evt) {
    System.exit(0);
    }

    private void jButton1MouseClicked(java.awt.event.MouseEvent evt) {
    String firstname = first.getText();
    String lastname = jTextField2.getText();
    NamesOutput.printf("%s" + " %s" + "%n", firstname, lastname);

    }

    /**
    * @param args the command line arguments
    */
    public static void main(String args[])
    {
    java.awt.EventQueue.invokeLater(new Runnable() {
    public void run()
    {
    try {
    new NamePrompt().setVisible(true);
    } catch (IOException ex) {
    Logger.getLogger(NamePrompt.class.getName()).log(L evel.SEVERE, null, ex);
    }
    }
    });
    }

    public javax.swing.JTextField first;
    public javax.swing.JButton jButton1;
    private javax.swing.JButton jButton2;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JMenu jMenu1;
    private javax.swing.JMenu jMenu2;
    private javax.swing.JMenuBar jMenuBar1;
    private javax.swing.JMenuItem jMenuItem1;
    private javax.swing.JProgressBar jProgressBar1;
    public javax.swing.JTextField jTextField2;

    private Object MainGUI() {
    throw new UnsupportedOperationException("Not yet implemented");
    }

    }

    This is the code for our first JFrame Form, we have a similar window called MainGUI but that one is for the user inputting his questions or answer the questions the program will ask it
    Why you mad ;)?

  9. #9
    TheV909's Avatar
    TheV909 is offline TwinkleToes
    Join Date
    May 2011
    Location
    Pennsylvania
    Posts
    7
    Rep Power
    0

    Default

    oh and because we are Java nubs atm we're using the GUI Builder to save time xP
    Why you mad ;)?

  10. #10
    TheV909's Avatar
    TheV909 is offline TwinkleToes
    Join Date
    May 2011
    Location
    Pennsylvania
    Posts
    7
    Rep Power
    0

    Default

    It appears we've worked this kink out.... however, if you see any errors, which there probably are..... let me know <3
    Why you mad ;)?

  11. #11
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,242
    Rep Power
    19

    Default

    Quote Originally Posted by TheV909 View Post
    oh and because we are Java nubs atm we're using the GUI Builder to save time xP
    Good luck with that </sarcasm>

    Seriously, the visual designer is not a beginners' tool. You're liable to spend much more time learning its quirks and learning how tio implement needed functionality than if you learn to hand-code the GUI instead.

    A seasoned Swing developer can and does improve his productivity with a builder, but that's only because he already knows and understands where all the code snippets go. And many professionals prefer to continue to hand-code their GUIs for the sake of flexibility, readability and ease of maintenance.

    db

Similar Threads

  1. how do i use jframe button to send input selection
    By earnest in forum New To Java
    Replies: 5
    Last Post: 04-04-2011, 08:46 PM
  2. Disable Maximize button in JFrame?
    By Manish87 in forum AWT / Swing
    Replies: 0
    Last Post: 02-01-2011, 03:39 PM
  3. How to open new JFrame with clicking on button?
    By phearless in forum AWT / Swing
    Replies: 1
    Last Post: 01-14-2011, 10:36 AM
  4. Replies: 6
    Last Post: 11-22-2010, 11:55 AM
  5. JFrame question
    By Try2Live4God in forum New To Java
    Replies: 2
    Last Post: 10-15-2007, 01:52 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •