-
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
-
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.
-
You know what I mean
I need to change JTextField more than once!
-
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
-
Replies: 3
Last Post: 04-08-2005, 11:05 AM
-
Replies: 3
Last Post: 03-07-2005, 02:34 AM
-
Replies: 0
Last Post: 12-13-2001, 12:06 PM
-
By Tim Brooks in forum .NET
Replies: 1
Last Post: 11-15-2001, 12:48 PM
-
By Wade Balzer in forum VB Classic
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
Forum Rules
|
Top DevX Stories
Easy Web Services with SQL Server 2005 HTTP Endpoints
JavaOne 2005: Java Platform Roadmap Focuses on Ease of Development, Sun Focuses on the "Free" in F.O.S.S.
Wed Yourself to UML with the Power of Associations
Microsoft to Add AJAX Capabilities to ASP.NET
IBM's Cloudscape Versus MySQL
|
Bookmarks