0

Is it possible to have JTextField scrollable without use of JScrollBar. For example, I would define field as max size 120, but only 40 to be visible, the rest is scrollable (left/right). Similar, that HTML INPUT TEXT works.

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
  • 1
    The plain JTextField works like this – BackSlash Dec 28 '14 at 12:10
  • But it can be scrolled forever ? new JTextField(40) will show 40 chars while the others will be scrolled. But I would like to limit them to stop scrolling after 120th is filled. – WonderMouse Dec 28 '14 at 12:45
  • Then you need to [limit the length of the JTextField](http://stackoverflow.com/questions/10136794/limiting-the-number-of-characters-in-a-jtextfield) – BackSlash Dec 28 '14 at 13:08
  • Thats what I'm asking here. There is no such method, except setColumns which is the same as calling the constructor with size. – WonderMouse Dec 28 '14 at 13:31
  • Did you open the link in the previous comment? There is the answer. – BackSlash Dec 28 '14 at 21:53
  • Yes, but still missing (see below comment) lack of basic functionality :) So I wrote own class :P – WonderMouse Dec 30 '14 at 07:39

1 Answers1

1

If you want to limit the user to entering 120 characters into the text field then you would use a DocumentFilter. Read the section from the Swing tutorial on Implementing a DocumentFilter for a working example.

camickr
  • 321,443
  • 19
  • 166
  • 288
  • I would use this in conjunction with a `DocumentListener` (same tutorial) to alert the user when the max number of characters has been reached. For example, you can make a warning `JLabel` visible when the number of characters has reached the max, and make it invisible when it is less. – hfontanez Dec 28 '14 at 16:24
  • Could do this with action listener as well I guess. I don't know why such generic methods are not expanded in functionality with higher Java versions, as this is a very common need when making GUI's. – WonderMouse Dec 28 '14 at 18:10