Results 1 to 10 of 10

Thread: setting text

  1. #1
    droidus is offline Senior Member
    Join Date
    Feb 2011
    Posts
    244
    Rep Power
    4

    Default setting text

    I am new to GUI, and am trying to set the text of a text box when the button is clicked. here is part of my code:

    Java Code:
    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
    jTextField1.setText("");
    }
    and i have a jTextField1 that has text in it. when the button is clicked, nothing happens.

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

    Default Re: setting text

    Quote Originally Posted by droidus View Post
    I am new to GUI, and am trying to set the text of a text box when the button is clicked. here is part of my code:

    Java Code:
    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
    jTextField1.setText("");
    }
    and i have a jTextField1 that has text in it. when the button is clicked, nothing happens.
    Then you've got a bug in your program and it's in code that you're not showing us, possibly a shadowed variable, ... but really, who knows?

  3. #3
    droidus is offline Senior Member
    Join Date
    Feb 2011
    Posts
    244
    Rep Power
    4

    Default Re: setting text

    here is the rest of the code:

    Java Code:
    /*
     * To change this template, choose Tools | Templates
     * and open the template in the editor.
     */
    
    /*
     * TEST.java
     *
     * Created on Nov 10, 2011, 8:58:37 PM
     */
    package gui;
    public class TEST extends javax.swing.JFrame {
    
        /** Creates new form TEST */
        public TEST() {
            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();
            jTextField1 = new javax.swing.JTextField();
            jSeparator1 = new javax.swing.JSeparator();
            jProgressBar1 = new javax.swing.JProgressBar();
    
            setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
    
            jButton1.setText("jButton1");
            jButton1.addActionListener(new java.awt.event.ActionListener() {
                public void actionPerformed(java.awt.event.ActionEvent evt) {
                    jButton1ActionPerformed(evt);
                }
            });
    
            jTextField1.setText("Hi there!");
    
            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()
                            .addContainerGap()
                            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                .addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, 146, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addComponent(jButton1)))
                        .addGroup(layout.createSequentialGroup()
                            .addGap(23, 23, 23)
                            .addComponent(jSeparator1, javax.swing.GroupLayout.PREFERRED_SIZE, 50, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGroup(layout.createSequentialGroup()
                            .addContainerGap()
                            .addComponent(jProgressBar1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                    .addContainerGap(244, Short.MAX_VALUE))
            );
            layout.setVerticalGroup(
                layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(layout.createSequentialGroup()
                    .addContainerGap()
                    .addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                    .addComponent(jSeparator1, javax.swing.GroupLayout.PREFERRED_SIZE, 10, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                    .addComponent(jButton1)
                    .addGap(18, 18, 18)
                    .addComponent(jProgressBar1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addContainerGap(192, Short.MAX_VALUE))
            );
    
            pack();
        }// </editor-fold>
    
    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
    jTextField1.setText("");
    }
    
        /**
         * @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 http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
             */
            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(TEST.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
            } catch (InstantiationException ex) {
                java.util.logging.Logger.getLogger(TEST.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
            } catch (IllegalAccessException ex) {
                java.util.logging.Logger.getLogger(TEST.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
            } catch (javax.swing.UnsupportedLookAndFeelException ex) {
                java.util.logging.Logger.getLogger(TEST.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 TEST().setVisible(true);
                }
            });
        }
        // Variables declaration - do not modify
        private javax.swing.JButton jButton1;
        private javax.swing.JProgressBar jProgressBar1;
        private javax.swing.JSeparator jSeparator1;
        private javax.swing.JTextField jTextField1;
        // End of variables declaration
    }
    am i missing anything?

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

  5. #5
    droidus is offline Senior Member
    Join Date
    Feb 2011
    Posts
    244
    Rep Power
    4

    Default Re: setting text

    did you just click on "preview design"?

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

    Default Re: setting text

    Quote Originally Posted by droidus View Post
    did you just click on "preview design"?
    This does not compute. What do you mean by 'click on "preview design"'?

  7. #7
    droidus is offline Senior Member
    Join Date
    Feb 2011
    Posts
    244
    Rep Power
    4

    Default Re: setting text

    i am working in netbeans, and there is a eye icon. doesn't that usually bring up a preview of the applet?

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

    Default Re: setting text

    I don't use NetBeans, but that's irrelevant as it has nothing to do with the code and the program's behavior when running which is the only thing we're interested in here. So you'll want to get the NetBean's design stuff out of your mind and deal with the logic of your program instead.

  9. #9
    droidus is offline Senior Member
    Join Date
    Feb 2011
    Posts
    244
    Rep Power
    4

    Default Re: setting text

    how is it not relevant? when the button is clicked, the text in the text field is changed. when running in the gui, i am not concerned about the design. i am trying to figure out (or diagnose) why it doesn't change. i am only checking to see if my code works. there shouldn't be that much logic just to press a button. i looked on the netbeans tutorial, and didn't see anything different from my code.

  10. #10
    droidus is offline Senior Member
    Join Date
    Feb 2011
    Posts
    244
    Rep Power
    4

    Default Re: setting text

    well now that is weird... it works in jgrasp, but not netbeans.. why would that be?

Similar Threads

  1. Text Not Setting
    By Hollowsoul in forum Advanced Java
    Replies: 23
    Last Post: 08-15-2011, 04:07 AM
  2. Replies: 1
    Last Post: 06-29-2011, 05:46 PM
  3. Setting text width and height in PDFBox
    By svenskmand in forum Advanced Java
    Replies: 0
    Last Post: 12-21-2010, 12:23 AM
  4. Replies: 0
    Last Post: 12-09-2010, 05:56 PM
  5. Replies: 3
    Last Post: 05-25-2009, 03:33 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
  •