Results 1 to 7 of 7
- 03-20-2013, 09:58 PM #1
Member
- Join Date
- Oct 2012
- Posts
- 15
- Rep Power
- 0
Textfields not accepting length arguements
First thing is that this portion is part of a much larger program, as such a main method is needed to run.
The problem is that the following bit of code doesn't accept any length argument passed for JTextField. the buttons will perform no actions however in this snipet, i have another program that is using the same thing shown here and is accepting all arguments, so i'm really confused. what am i doing wrong?
Java Code:import javax.swing.*; import java.awt.*; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; public class SplashGUI extends JFrame implements ActionListener { private JLabel userEmail, password, forgotPrompt; private JTextField emailInput, passwordInput; private JButton loginBtn, registerBtn, forgotBtn; private String tempStr1; private String tempStr2; public SplashGUI() { makeGUI(); } private void makeGUI() { this.setTitle("SNS - Log In"); this.setSize(500,500); this.setDefaultCloseOperation(EXIT_ON_CLOSE); Container pane = getContentPane(); userEmail = new JLabel("Email: "); emailInput = new JTextField(50); password = new JLabel("Password: "); passwordInput = new JTextField(50); loginBtn = new JButton("Log In"); registerBtn = new JButton("Register"); forgotBtn = new JButton ("Forgot Password?"); loginBtn.addActionListener(this); registerBtn.addActionListener(this); forgotBtn.addActionListener(this); pane.setLayout(new FlowLayout()); pane.add(userEmail); pane.add(emailInput); pane.add(password); pane.add(passwordInput); pane.add(loginBtn); pane.add(registerBtn); pane.add(forgotBtn); this.setVisible(true); } }
I am trying to get it smaller, that size expands past the current window size, so you'll need to resize it to see.
- 03-20-2013, 11:15 PM #2
Re: Textfields not accepting length arguements
Moved from New to Java
dbIf you're forever cleaning cobwebs, it's time to get rid of the spiders.
- 03-20-2013, 11:20 PM #3
Re: Textfields not accepting length arguements
The length argument of JTextfield indicates the size of the box, not the maximum amount of characters that are allowed. If you want to limit the amount of characters, you need a DocumentFilter, as described in this tutorial: Text Component Features (The Java™ Tutorials > Creating a GUI With JFC/Swing > Using Swing Components)
- 03-21-2013, 04:38 AM #4
Member
- Join Date
- Oct 2012
- Posts
- 15
- Rep Power
- 0
Re: Textfields not accepting length arguements
Yea, I am actually trying to limit the size of the field, not the amount of characters in it. I was using the characters to show how far beyond 50 it is. I have another program that is using the same type of jtextfield with an argument of (2), and although you can enter x amount of character in this field, only 2 are shown which tells me that one has the correct width for the field. yet in this program it's not adjusting the jtextfield size. just wondering what the issue is and how to fix it.
- 03-21-2013, 07:01 AM #5
Senior Member
- Join Date
- Jul 2009
- Posts
- 1,236
- Rep Power
- 12
Re: Textfields not accepting length arguements
The "50" creates a text field that will hold 50 characters the width of "W". Since numbers are narrower than "W" it appears like there is a lot of extra space. When you only have 2 characters you don't notice this as much. The solution:
a) use a number less than 50
b) change the font to be a monospaced font then all characters will take up the same space.
- 03-21-2013, 11:21 AM #6
Re: Textfields not accepting length arguements
Ah, sorry, I misunderstood your question. Follow camickr's directions.
Alternatively, use a layout that sizes the components for you, like GridBagLayout, or if you can use external libraries, MigLayout or JGoodies FormLayout.
- 03-21-2013, 01:39 PM #7
Member
- Join Date
- Oct 2012
- Posts
- 15
- Rep Power
- 0
Similar Threads
-
Inserting a method with arguements into another method with no arguements?
By bbfunk in forum New To JavaReplies: 2Last Post: 01-22-2013, 06:21 AM -
Accepting value for phone number field
By sgiri37 in forum New To JavaReplies: 2Last Post: 03-22-2012, 10:20 AM -
The PrintRequestAttributeSet is not accepting the given attributes.
By Iyyam in forum New To JavaReplies: 0Last Post: 09-10-2009, 01:35 PM -
[SOLVED] commandline arguements
By raghu9198 in forum New To JavaReplies: 3Last Post: 12-23-2008, 10:47 AM -
Socket programming - accepting files
By ravian in forum NetworkingReplies: 2Last Post: 11-29-2007, 11:40 AM
Bookmarks