Results 1 to 10 of 10
Thread: setting text
- 11-11-2011, 03:08 AM #1
Senior Member
- Join Date
- Feb 2011
- Posts
- 235
- Rep Power
- 3
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:
and i have a jTextField1 that has text in it. when the button is clicked, nothing happens.Java Code:private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) { jTextField1.setText(""); }
-
Re: setting text
- 11-11-2011, 03:14 AM #3
Senior Member
- Join Date
- Feb 2011
- Posts
- 235
- Rep Power
- 3
Re: setting text
here is the rest of the code:
am i missing anything?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 }
-
Re: setting text
The code worked for me.
- 11-11-2011, 03:20 AM #5
Senior Member
- Join Date
- Feb 2011
- Posts
- 235
- Rep Power
- 3
Re: setting text
did you just click on "preview design"?
-
Re: setting text
- 11-11-2011, 03:26 AM #7
Senior Member
- Join Date
- Feb 2011
- Posts
- 235
- Rep Power
- 3
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?
-
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.
- 11-11-2011, 06:35 PM #9
Senior Member
- Join Date
- Feb 2011
- Posts
- 235
- Rep Power
- 3
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.
- 11-11-2011, 08:33 PM #10
Senior Member
- Join Date
- Feb 2011
- Posts
- 235
- Rep Power
- 3
Similar Threads
-
Text Not Setting
By Hollowsoul in forum Advanced JavaReplies: 23Last Post: 08-15-2011, 04:07 AM -
String -> variable (setting variables from parsing a text file)
By marklee42 in forum New To JavaReplies: 1Last Post: 06-29-2011, 05:46 PM -
Setting text width and height in PDFBox
By svenskmand in forum Advanced JavaReplies: 0Last Post: 12-21-2010, 12:23 AM -
Applet program to open a text file and display the content in text area
By bitse in forum Java AppletsReplies: 0Last Post: 12-09-2010, 05:56 PM -
[SOLVED] [newbie] AbstractButton.setText() not setting text within JButton
By jon80 in forum New To JavaReplies: 3Last Post: 05-25-2009, 03:33 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks