Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 16 of 16

Thread: Swing (Problems)

  1. #1
    Member
    Join Date
    Sep 2012
    Posts
    98
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Swing (Problems)

    I am trying to get this program to output where they put their name in a different area on the box, I have bolded the area that I feel is the issue.

    /*
     * To change this template, choose Tools | Templates
     * and open the template in the editor.
     */
     
    /**
     *
     * @author Logan
     */
    public class NameGUI extends javax.swing.JFrame {
     
        /**
         * Creates new form NameGUI
         */
        public NameGUI() {
            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">//GEN-BEGIN:initComponents
        private void initComponents() {
     
            NameLabel = new javax.swing.JLabel();
            TheirAnswer = new javax.swing.JTextField();
            NameChecker = new javax.swing.JButton();
            ResultLabel = new javax.swing.JLabel();
            NameAfterCalc = new javax.swing.JLabel();
     
            setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
     
            NameLabel.setText("What Is Your Name?");
     
            TheirAnswer.addActionListener(new java.awt.event.ActionListener() {
                public void actionPerformed(java.awt.event.ActionEvent evt) {
                    TheirAnswerActionPerformed(evt);
                }
            });
     
            NameChecker.setText("Check My Name!");
            NameChecker.addActionListener(new java.awt.event.ActionListener() {
                public void actionPerformed(java.awt.event.ActionEvent evt) {
                    NameCheckerActionPerformed(evt);
                }
            });
     
            ResultLabel.setText("Name Result:");
     
            javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
            getContentPane().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(28, 28, 28)
                            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                .addComponent(NameChecker)
                                .addComponent(TheirAnswer, javax.swing.GroupLayout.PREFERRED_SIZE, 151, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addComponent(NameLabel)))
                        .addGroup(layout.createSequentialGroup()
                            .addContainerGap()
                            .addComponent(ResultLabel)
                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                            .addComponent(NameAfterCalc, javax.swing.GroupLayout.PREFERRED_SIZE, 0, javax.swing.GroupLayout.PREFERRED_SIZE)))
                    .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
            );
            layout.setVerticalGroup(
                layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(layout.createSequentialGroup()
                    .addContainerGap()
                    .addComponent(NameLabel, javax.swing.GroupLayout.PREFERRED_SIZE, 26, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                    .addComponent(TheirAnswer, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGap(18, 18, 18)
                    .addComponent(NameChecker)
                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 29, Short.MAX_VALUE)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(ResultLabel)
                        .addComponent(NameAfterCalc, javax.swing.GroupLayout.PREFERRED_SIZE, 14, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGap(27, 27, 27))
            );
     
            pack();
        }// </editor-fold>//GEN-END:initComponents
     
        private void TheirAnswerActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_TheirAnswerActionPerformed
            // TODO add your handling code here:
        }//GEN-LAST:event_TheirAnswerActionPerformed
     
        private void NameCheckerActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_NameCheckerActionPerformed
            String TheirName = String.valueOf(TheirAnswer);
            [B]NameAfterCalc.setText(TheirName);[/B]
     
        }//GEN-LAST:event_NameCheckerActionPerformed
     
        /**
         * @param args the command line arguments
         */
        public static void main(String args[]) {
            /* Set the Nimbus look and feel */
            //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
            /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
             * For details see [url=http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html]How to Set the Look and Feel (The Java™ Tutorials > Creating a GUI With JFC/Swing > Modifying the Look and Feel)[/url] 
             */
            try {
                for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                    if ("Nimbus".equals(info.getName())) {
                        javax.swing.UIManager.setLookAndFeel(info.getClassName());
                        break;
                    }
                }
            } catch (ClassNotFoundException ex) {
                java.util.logging.Logger.getLogger(NameGUI.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
            } catch (InstantiationException ex) {
                java.util.logging.Logger.getLogger(NameGUI.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
            } catch (IllegalAccessException ex) {
                java.util.logging.Logger.getLogger(NameGUI.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
            } catch (javax.swing.UnsupportedLookAndFeelException ex) {
                java.util.logging.Logger.getLogger(NameGUI.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
            }
            //</editor-fold>
     
            /* Create and display the form */
            java.awt.EventQueue.invokeLater(new Runnable() {
                public void run() {
                    new NameGUI().setVisible(true);
                }
            });
        }
        // Variables declaration - do not modify//GEN-BEGIN:variables
        private javax.swing.JLabel NameAfterCalc;
        private javax.swing.JButton NameChecker;
        private javax.swing.JLabel NameLabel;
        private javax.swing.JLabel ResultLabel;
        private javax.swing.JTextField TheirAnswer;
        // End of variables declaration//GEN-END:variables
    }
    Last edited by LoganC; October 21st, 2012 at 08:55 PM.


  2. #2
    Super Moderator curmudgeon's Avatar
    Join Date
    Aug 2012
    Posts
    1,130
    My Mood
    Cynical
    Thanks
    64
    Thanked 139 Times in 134 Posts

    Default Re: Swing (Problems)

    Quote Originally Posted by LoganC View Post
    I am trying to get this program to output where they put their name in a different area on the box, I have bolded the area that I feel is the issue.
    ??
    Can you give us more information? Please assume that we have no idea to start with what you're trying to accomplish with your program or what it might currently be doing wrong. We're dependent on you for all these details.

  3. #3
    Member
    Join Date
    Sep 2012
    Posts
    98
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Swing (Problems)

    All I am trying to do is take a string that they are inputting in the GUI[TheirLabel], and just move it to a different part on the GUI. [NameAfterCalc]

  4. #4
    Super Moderator curmudgeon's Avatar
    Join Date
    Aug 2012
    Posts
    1,130
    My Mood
    Cynical
    Thanks
    64
    Thanked 139 Times in 134 Posts

    Default Re: Swing (Problems)

    Please do go on... How does the user enter the String? What does the user do to tell the GUI to move the String? Where is the String initially? Where is supposed to be moved? Where exactly are you stuck? what misbehaviors are you seeing? etc... Again, explain things to us as if we don't understand your code or problem, as if we're completely ignorant.

    Note that your code uses NetBeans-generated code making it hard to tell by glancing at it how its structured. We're counting on you to provide these details. A good rule of thumb here is that you should strive to put in as much effort asking your question as you'd like a volunteer to put in answering it.
    Last edited by curmudgeon; October 21st, 2012 at 09:36 PM.

  5. #5
    Member
    Join Date
    Sep 2012
    Posts
    98
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Swing (Problems)

    The user enters string in a textbox I have assigned, The string is initially where they input it in the GUI. It is supposed to be moved to a label in the bottom of the GUI. I am stuck getting the button to work. I have used the actionPerform and it still is not working. It is almost as if code is not right for me moving the code. I am trying to move what they input to a different part on the GUI

  6. #6
    Super Moderator curmudgeon's Avatar
    Join Date
    Aug 2012
    Posts
    1,130
    My Mood
    Cynical
    Thanks
    64
    Thanked 139 Times in 134 Posts

    Default Re: Swing (Problems)

    Sorry if I'm dragging this out, but I'm still a bit unclear. First off, what is a textbox? I don't know of a Swing component with this name. Do you mean JTextField? If so, please let us know and also give us the name of the variable. Next what is in your ActionListener code? Does its actionPerformed method in fact get called? Have you added println(...) statements to test this out? Next, is the text supposed to be placed in a JLabel that is already displayed in the GUI or are you trying to create a new one and then add it to the GUI? Which label variable is it? Are you sure that it is being displayed or that it is big enough to hold your String (you can check by putting a line border around it)? Is any text at all being displayed in the JLabel?

    Also, you mentioned that you've "bolded" your code to highlight a region of it, but please check this out. At least on my system, this doesn't work within code blocks. You may wish to post code snippets along with your entire program to show us in isolation regions that you think we need to concentrate on.
    Last edited by curmudgeon; October 21st, 2012 at 10:03 PM.

  7. #7
    Member
    Join Date
    Sep 2012
    Posts
    98
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Swing (Problems)

    Below is my action listener code. No text is being display at all. Text will be displayed in a jLabel that is already made.



    private void NameCheckerActionPerformed(java.awt.event.ActionEvent evt) {                                            
            String TheirName = String.valueOf(TheirAnswer);
            NameAfterCalc.setText(TheirName);
     
        }

  8. #8
    Super Moderator curmudgeon's Avatar
    Join Date
    Aug 2012
    Posts
    1,130
    My Mood
    Cynical
    Thanks
    64
    Thanked 139 Times in 134 Posts

    Default Re: Swing (Problems)

    What does TheirAnswer represent?

    And again, have you added some debug code to your program? Something like:

    private void NameCheckerActionPerformed(java.awt.event.ActionEvent evt) {                                            
      String TheirName = String.valueOf(TheirAnswer);
     
      // add this:
      System.out.println("inside of NameCheckerActionPerformed. String TheirName is: " + TheirName);
     
      // add this:
      NameAfterCalc.setBorder(BorderFactory.createLineBorder(Color.red));
      NameAfterCalc.setText(TheirName);
     
    }

    This code will be deleted later, but it's just there to test the state of your program and to see if this method is ever actually called.
    Last edited by curmudgeon; October 21st, 2012 at 10:14 PM.

  9. #9
    Member
    Join Date
    Sep 2012
    Posts
    98
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Swing (Problems)

    TheirAnswer is the JTextField where they input their name.

  10. #10
    Super Moderator curmudgeon's Avatar
    Join Date
    Aug 2012
    Posts
    1,130
    My Mood
    Cynical
    Thanks
    64
    Thanked 139 Times in 134 Posts

    Default Re: Swing (Problems)

    Quote Originally Posted by LoganC View Post
    TheirAnswer is the JTextField where they input their name.
    Please see the edit to my post above.

    So TheirAnswer is the JTextField itself? then String.valueOf(TheirAnswer) doesn't make much sense since this will return the toString() value of the JTextField which is not very useful for your purposes. Wouldn't you rather get the text held in the JTextField? Calling getText() on it here seems like a better idea, I think.

  11. #11
    Member
    Join Date
    Sep 2012
    Posts
    98
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Swing (Problems)

    I added the code you said for debugging, and I got this in the IDE:
    Inside of NameCheckerActionPerformed. String TheirName is: javax.swing.JTextField[,28,38,151x28,layout=javax.swing.plaf.basic.BasicTextUI$UpdateHandler,alignmentX=0.0,alignmentY=0.0,border=javax.swing.plaf.synth.SynthBorder@8497904,flags=288,maximumSize=,minimumSize=,preferredSize=,caretColor=,disabledTextColor=DerivedColor(color=142,143,145 parent=nimbusDisabledText offsets=0.0,0.0,0.0,0 pColor=142,143,145,editable=true,margin=javax.swing.plaf.InsetsUIResource[top=0,left=0,bottom=0,right=0],selectedTextColor=DerivedColor(color=255,255,255 parent=nimbusSelectedText offsets=0.0,0.0,0.0,0 pColor=255,255,255,selectionColor=DerivedColor(color=57,105,138 parent=nimbusSelectionBackground offsets=0.0,0.0,0.0,0 pColor=57,105,138,columns=0,columnWidth=0,command=,horizontalAlignment=LEADING]

  12. #12
    Super Moderator curmudgeon's Avatar
    Join Date
    Aug 2012
    Posts
    1,130
    My Mood
    Cynical
    Thanks
    64
    Thanked 139 Times in 134 Posts

    Default Re: Swing (Problems)

    Yep, as I thought, that's the toString() returned from your JTextField variable which is certainly not what you want. Get the JTextField's text instead by calling getText() on it.

    Also, did your JLabel get a red border after the button press?

  13. #13
    Member
    Join Date
    Sep 2012
    Posts
    98
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Swing (Problems)

    TheirAnswer is the JTextField, correct. We didn't learn much about get text, is this the correct application? I am getting an error.

    String TheirName = String.getText(TheirAnswer);
            NameAfterCalc.setText(TheirName);

  14. #14
    Super Moderator curmudgeon's Avatar
    Join Date
    Aug 2012
    Posts
    1,130
    My Mood
    Cynical
    Thanks
    64
    Thanked 139 Times in 134 Posts

    Default Re: Swing (Problems)

    You call the method on the JTextField itself. To learn how to use JTextFields, please check the tutorials, Using JTextFields and the JTextField API

  15. #15
    Member
    Join Date
    Sep 2012
    Posts
    98
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Swing (Problems)

    Hmm all areas are gone, but it's still not printing out for some reason.
    private void NameCheckerActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_NameCheckerActionPerformed
            String TheirName = TheirAnswer.getText();
            NameAfterCalc.setText(TheirName);

  16. #16
    Super Moderator jps's Avatar
    Join Date
    Jul 2012
    Posts
    2,642
    My Mood
    Daring
    Thanks
    90
    Thanked 262 Times in 231 Posts

    Default Re: Swing (Problems)

    If you use [code=java] before the code and [/code] at the end of your code, instead of [code] at the start and [/code] at the end, look how pretty.
    Plus you can decorate with comments and all those pretty colors make it so easy to see.
    Quote Originally Posted by LoganC View Post
    I am trying to get this program to output where they put their name in a different area on the box, I have bolded the area that I feel is the issue.

    /*
     * To change this template, choose Tools | Templates
     * and open the template in the editor.
     */
     
    /**
     *
     * @author Logan
     */
    public class NameGUI extends javax.swing.JFrame {
     
        /**
         * Creates new form NameGUI
         */
        public NameGUI() {
            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">//GEN-BEGIN:initComponents
        private void initComponents() {
     
            NameLabel = new javax.swing.JLabel();
            TheirAnswer = new javax.swing.JTextField();
            NameChecker = new javax.swing.JButton();
            ResultLabel = new javax.swing.JLabel();
            NameAfterCalc = new javax.swing.JLabel();
     
            setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
     
            NameLabel.setText("What Is Your Name?");
     
            TheirAnswer.addActionListener(new java.awt.event.ActionListener() {
                public void actionPerformed(java.awt.event.ActionEvent evt) {
                    TheirAnswerActionPerformed(evt);
                }
            });
     
            NameChecker.setText("Check My Name!");
            NameChecker.addActionListener(new java.awt.event.ActionListener() {
                public void actionPerformed(java.awt.event.ActionEvent evt) {
                    NameCheckerActionPerformed(evt);
                }
            });
     
            ResultLabel.setText("Name Result:");
     
            javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
            getContentPane().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(28, 28, 28)
                            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                .addComponent(NameChecker)
                                .addComponent(TheirAnswer, javax.swing.GroupLayout.PREFERRED_SIZE, 151, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addComponent(NameLabel)))
                        .addGroup(layout.createSequentialGroup()
                            .addContainerGap()
                            .addComponent(ResultLabel)
                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                            .addComponent(NameAfterCalc, javax.swing.GroupLayout.PREFERRED_SIZE, 0, javax.swing.GroupLayout.PREFERRED_SIZE)))
                    .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
            );
            layout.setVerticalGroup(
                layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(layout.createSequentialGroup()
                    .addContainerGap()
                    .addComponent(NameLabel, javax.swing.GroupLayout.PREFERRED_SIZE, 26, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                    .addComponent(TheirAnswer, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGap(18, 18, 18)
                    .addComponent(NameChecker)
                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 29, Short.MAX_VALUE)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(ResultLabel)
                        .addComponent(NameAfterCalc, javax.swing.GroupLayout.PREFERRED_SIZE, 14, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGap(27, 27, 27))
            );
     
            pack();
        }// </editor-fold>//GEN-END:initComponents
     
        private void TheirAnswerActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_TheirAnswerActionPerformed
            // TODO add your handling code here:
        }//GEN-LAST:event_TheirAnswerActionPerformed
     
        private void NameCheckerActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_NameCheckerActionPerformed
            String TheirName = String.valueOf(TheirAnswer);
    /**000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000*/
            [B]NameAfterCalc.setText(TheirName);[/B]
    /**000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000*/
     
        }//GEN-LAST:event_NameCheckerActionPerformed
     
        /**
         * @param args the command line arguments
         */
        public static void main(String args[]) {
            /* Set the Nimbus look and feel */
            //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
            /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
             * For details see [url=http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html]How to Set the Look and Feel (The Java™ Tutorials > Creating a GUI With JFC/Swing > Modifying the Look and Feel)[/url] 
             */
            try {
                for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                    if ("Nimbus".equals(info.getName())) {
                        javax.swing.UIManager.setLookAndFeel(info.getClassName());
                        break;
                    }
                }
            } catch (ClassNotFoundException ex) {
                java.util.logging.Logger.getLogger(NameGUI.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
            } catch (InstantiationException ex) {
                java.util.logging.Logger.getLogger(NameGUI.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
            } catch (IllegalAccessException ex) {
                java.util.logging.Logger.getLogger(NameGUI.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
            } catch (javax.swing.UnsupportedLookAndFeelException ex) {
                java.util.logging.Logger.getLogger(NameGUI.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
            }
            //</editor-fold>
     
            /* Create and display the form */
            java.awt.EventQueue.invokeLater(new Runnable() {
                public void run() {
                    new NameGUI().setVisible(true);
                }
            });
        }
        // Variables declaration - do not modify//GEN-BEGIN:variables
        private javax.swing.JLabel NameAfterCalc;
        private javax.swing.JButton NameChecker;
        private javax.swing.JLabel NameLabel;
        private javax.swing.JLabel ResultLabel;
        private javax.swing.JTextField TheirAnswer;
        // End of variables declaration//GEN-END:variables
    }

Similar Threads

  1. Swing Help
    By ByteSized in forum What's Wrong With My Code?
    Replies: 6
    Last Post: June 20th, 2012, 05:58 PM
  2. Help with Swing
    By mwr76 in forum AWT / Java Swing
    Replies: 1
    Last Post: October 9th, 2011, 10:28 AM
  3. Help with swing GUI
    By mwr76 in forum AWT / Java Swing
    Replies: 1
    Last Post: October 5th, 2011, 03:54 AM
  4. Could need some help with Swing
    By Kerr in forum AWT / Java Swing
    Replies: 2
    Last Post: March 10th, 2011, 09:11 AM
  5. Replies: 0
    Last Post: November 27th, 2010, 09:47 PM