Win a copy of Head First Android this week in the Android forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • Rob Spoor
  • Bear Bibeault
Saloon Keepers:
  • Jesse Silverman
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Piet Souris
  • Al Hobbs
  • salvin francis

How to set up not changable size of textPane?

 
Ranch Hand
Posts: 48
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

i am using windows pro builder and trying to set up size limit of text pane.



Problem is when i am inputting letters to this textPAne, this is still widening.

How to set up is a constant?

Best,
Jacek
 
Marshal
Posts: 74374
334
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Don't use a GUI builder until you are familiar with the ways to set up a GUI by hand.
The idea behind Swing®/AWT GUIs is that you sue a layout, so the components change size as the whole GUI is resized, to fit different sizes of screen or at the user's instruction. The notion of a component which cannot be resized is inconsistent with the approach. If you insist however, see whether there is a setResizable(boolean) method.

Moving to our GUIs forum.
 
Jacek jaryszek
Ranch Hand
Posts: 48
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you Campbell Ritchie.

But with this text pane i should have only place for 4 digits. (number from 1 to 100).

So the field should be resized always for 4 digits.

Best,
Jacek
 
Campbell Ritchie
Marshal
Posts: 74374
334
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
There might be something in the Java™ Tutorials.
Also, you can get the associated Document and add a Document Listener to that. Not sure how you would use that, but there is probably some way to monitor the length of the Document.
Somebody else will doubtless know more than I do.
 
Rancher
Posts: 3248
30
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

But with this text pane i should have only place for 4 digits. (number from 1 to 100).  



Well you only need room for 3 digits.

Don't use a JTextPane. A JTextPane is used for larger pieces of text.

In gener, for 3 digits use a JTextField:



The text field will then determine its own preferred size. You then use a layout manager that respects the preferred size of the text field.

Then if you want to limit the number of characters the use can type into the text field you can use a DocumentFilter. Read the Swing tutorial on How to Implement a DocumentFilter for a working example.

However, in your case because you only want numbers in the text field you can use a JFormattedText field. This will allow you specify the number of digits to be entered by using a "format mask" and the mask will edit the character as it is typed to make sure it is a digit and not a letter. Read the tutorial on How to Use Formatted Text Fields.
 
Author
Posts: 974
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Luke jaryszek wrote:But with this text pane i should have only place for 4 digits. (number from 1 to 100).


There are a couple of issues here, but IMHO the fundamental one is that setBounds() and setSize() simply do not work to set a component's size. That's because the container's LayoutManager that is automatically in place will clobber any size/bounds you might have tried to set. (If you're not using a LayoutManager at all, then those two methods will work, but it is not recommended to not use a LayoutManager except by experts who know what they are doing, and even then only rarely. The rest of this posting assumes that a LayoutManager is in place.)

A method that does work is setPreferredSize(). It's fine if you want to use setPreferredSize(), but Swing's text components generally provide simpler ways of setting size.

JTextPane doesn't, but the only reason to use JTextPane is if you want to handle text in different styles (such as some text in one font and some text in a different font). If you want text in a single style, you could use JTextArea, which has constructors which take rows and columns paramaters: new TextArea(1, 8) Or there are separate setRows() and setColumns() methods. But JTextArea only makes sense if you need to support multiple rows of text.

For a single-row text field you can use JTextField, which has constructors which take a columns parameter: new TextField(4) Or it has a setColumns() method. Or you might want to use a JSpinner instead of a JTextField.

minor footnote: With JTextField you sometimes must to set more columns than you actually need. That's because it sets the required text-width for the whole component, including its borders, but the left and right borders cut into the usable width. This is stupid, but it's been that way for decades and isn't going to change now.
 
Jacek jaryszek
Ranch Hand
Posts: 48
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you Guys,

JTextField textField = new JTextField(3);



This did work for me !
Thank you!

Jacek
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic