Problem - inner classes


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 4 of 4

Thread: Problem - inner classes

  1. #1
    Join Date
    Dec 2005
    Posts
    2

    Problem - inner classes

    Problem – Inner Classes

    I have to collect values from JTextField(numbers) on a press of a button. Because JButtons addActionListener is a local inner class I would have to declare variables in a outer class “final”. This would not be OK because I want to change the value of JtextField. Is there any way to solve this problem – maybe with different design of a program or something else – I don't know – anything? Here is the code.



    import javax.swing.*;
    import javax.swing.border.*;
    import java.awt.*;
    import java.awt.event.*;


    public class NewEmpty extends JDialog{
    public NewEmpty( JFrame frame ) {
    super( frame, true );
    setSize( 700, 700 );

    int Z1;

    // Creates a panel to hold all components
    JPanel panel = new JPanel( new BorderLayout() );
    panel.setLayout( new GridBagLayout() );

    // give the panel a border gap of 5 pixels
    panel.setBorder( new EmptyBorder( new Insets( 5, 5, 5, 5 ) ) );
    getContentPane().add( BorderLayout.CENTER, panel );
    GridBagConstraints c = new GridBagConstraints();

    // Define preferred sizes for input fields
    Dimension shortField = new Dimension( 40, 20 );

    // Spacing between label and field
    EmptyBorder border = new EmptyBorder( new Insets( 0, 0, 0, 10 ) );

    // add space around all components to avoid clutter
    c.insets = new Insets( 2, 2, 2, 2 );

    // anchor all components WEST
    c.anchor = GridBagConstraints.WEST;

    JLabel lbl1 = new JLabel( "Enter Number:" );
    lbl1.setBorder( border ); // add some space to the right
    panel.add( lbl1, c );
    JTextField txt1 = new JTextField();
    txt1.setPreferredSize( shortField );
    c.gridx = 1;
    c.weightx = 1.0; // use all available horizontal space
    c.gridwidth = 3; // spans across 3 columns
    c.fill = GridBagConstraints.HORIZONTAL; // fills the 3 columns
    panel.add( txt1, c );



    JButton submitBtn = new JButton( "Submit" );
    c.gridx = 4;
    c.gridy = 0;
    c.gridwidth = 1;
    c.fill = GridBagConstraints.HORIZONTAL;
    panel.add( submitBtn, c );

    submitBtn.addActionListener(new ActionListener() {
    public void actionPerformed(ActionEvent ev) {

    Z1 = Integer.parseInt(txt1.getText());
    }

    });
    WindowListener wndCloser = new WindowAdapter() {
    public void windowClosing(WindowEvent e) {
    System.exit(0);
    }
    };
    addWindowListener( wndCloser );

    setVisible( true );
    }
    public static void main( String[] args ) {
    new NewEmpty( new JFrame() );
    }
    }


    TIA

  2. #2
    Join Date
    Oct 2005
    Posts
    107
    You can make JTextField final...and still change it's contents. All the final modifier does is not let you do this more than once...

    jtField = new JTextField()

    you can still change the text and stuff. It just has to be final to be used in an anonymous class.

  3. #3
    Join Date
    Dec 2005
    Posts
    2

    You know what I mean

    I need to change JTextField more than once!

  4. #4
    Join Date
    Oct 2005
    Posts
    107
    I don't see where you are changing txt1 reference but if you really need to then don't use an anonymous class.

Similar Threads

  1. classes and arrays problem
    By hiya in forum C++
    Replies: 3
    Last Post: 04-08-2005, 11:05 AM
  2. Java Applet Compiler problem?
    By mdl in forum Java
    Replies: 3
    Last Post: 03-07-2005, 02:34 AM
  3. Replies: 0
    Last Post: 12-13-2001, 12:06 PM
  4. Problem w/C# Classes
    By Tim Brooks in forum .NET
    Replies: 1
    Last Post: 11-15-2001, 12:48 PM
  5. Replies: 0
    Last Post: 06-23-2000, 02:17 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center