Title itself sounds strange but i wanted to see if there's actual way of improving performances of "setText" method of regular Java JTextField.
I have app that's used for calculating some scientific data. Results of the calculation are being presented in Swing window that contains tabs with total of 1815 different JTextField controls. I do need JTextField because results should be editable.
I noticed that a lot of "processing results" time goes to internal workings of "setText" method. Looking at JTextComponent source code i assume it's because of underlying "AbstractDocument".
As i would like to improve speed of presenting calculated results to my user, is there a way to make JTextField perform faster (most notably it's "setText" method)?
Those fields are grouped into separate frames in order to be more visual appealing and distinguishable.
Seriously. A user will not be able to view 1815 components at a single time an observe there behaviour especially if the value of all of them are changing at once.
Maybe a single table can't be used for all the text fields, but I'm sure you can structure some of the data into tables.
I do need JTextField because results should be editable.
I suspect some processing is related to the text field generating events whenever the text is changed. For example DocumentEvents and UndoableEditEvents.
So, maybe use a JLabel to display the data and then to edit you double click and use a popup JTextField to enter the data into the label.
creating a custom JTextField that's not using AbstractDocument but plain String?
Instead of using a PlainDocument as the implemtation of the AbstractDocument you could create your own simpler Document implementation without the full overhead of the PlainDocument.
Related
So I've been developing web applications for 10 years, but have fond memories of working on small swing applications that related to an introductory programming curriculum I was paid to help with before that. I generally like building swing/java UI especially with a decent UI builder. However, in the intervening time I've become very accustomed to providing clear, red error messages of variable length and number when the data on a form is invalid. Sometimes with icons, sometimes with markers on the field etc. Very common stuff that's easy to do in the web world, and is really helpful and improves usability.
Today I was trying to find a way to display a nice list of error messages at the top or bottom of my swing form, icons and field markers can come later, but for now I just want a nice variable sized list of things that need to be corrected, in a red color.
At first blush this seems like it would be easy, but..
JLabel doesn't wrap, so long text is hidden, or you have to make the form ultra wide
JText Area has the same issue unless you define a number of columns, and I want to fill the space available. A set number of columns won't resize.
JTextPane wraps but as far as I can tell it won't turn red unless you get into the whole way over complicated StyledDocument bit. I don't want to do anything more complicated than change the text color so this seems like overkill.
There's also the minor issue of pushing outside the window space, and one can call pack(), but Id like to only increase the height of the form and wrap the text not change the dialog to the width of the message (creating lots of strained layout and dead space if a single long message doubles the width)
I also don't really want to have scroll bars either for the message area or the form as a whole.
This surely is a wheel that's already been invented, so what's the right way to show a list of red (but otherwise unformatted) error messages in a swing form?
Label doesn't wrap, so long text is hidden, or you have to make the
form ultra wide
use HTML
JText Area has the same issue unless you define a number of columns,
and I want to fill the space available. A set number of columns won't
resize.
use word and line wrap
A set number of columns won't resize. and together with "I also don't really want to have scroll bars"
put JTextArea to JScrollPane, to disable JScrollBars, set null for Borders from JScrollPane
JTextPane wraps but as far as I can tell it won't turn red unless you
get into the whole way over complicated StyledDocument bit. I don't
want to do anything more complicated than change the text color so
this seems like overkill.
use HighLighter, but nothing clear from posted description about your issue with StyledDocument
I'm writing a Java Desktop utility in Java swing and have a minimal GUI part in it, most of work is done on server side i.e. backend. So, I don't want to spend a lot of time on GUI part, learning different controls and widgets. The problem is that Swing has two controls for (to me)same tasks i.e. dropdown menu and they are JComboBox and JSpinner I don't know the difference and I don't want any limitation that would hinder me from completing my task after I've selected one.
I've to use dropdown to display List<String> returned from DataBase and it can have as many as thousands of values. To keep user from scrolling I'll be taking starting alphabet as input or some category limitation will be there so, it may be that I'll be using specific values to be displayed from List<String>. i want my program to be as efficient as it can be and spend least time on front end as there are a lot of operations on backend.
Any Help will be highly appreciated
you issue talking about AutoComplete JComboBox / JTextField
with some effort could be aplied (AutoComplete JTextField) to JSpinner too
I've to use dropdown to display List returned from DataBase and it can have as many as thousands of values.
all above mentioned JComponents are based on premature array, maybe need to convert java.util.List to String[] or Vector (depends of your code logics)
none of GUI are designated to helt the thousands of values, have look at Paginations for Databases engine
above mentioned AutoComplete JComboBox / JTextField works without any issue up-to 2k rows on todays PCs
for searching or selections from largiest arrays you have look at Stepped JComboBox (about two or more JComboBoxes)
1.st for reduced selection from [0-9, A-Z]
2.nd for searching in records started with A (for example)
redirect Database events to the background tasks and to use SwingWorker or Runnable#Thread
The key difference lies in the model: a SpinnerModel implementation creates a sequence of values, while a ComboBoxModel does not. If objects in a SpinnerModel do not have a suitable natural order, you'll need to impose one.
As practical matter, "thousands of values" will benefit from an auxiliary approach, as suggested in #mKorbel's answer.
JComboBox suits your requirement. JComboBox is suitable for displaying a list of values. JSpinner is used when you want to do some functionality like incerement/decrement on the Spinner's text field.
This Oracle tutorial explains about JSpinner and its similarities to JComboBox. There is a demo app also.
I have a large set of data from which the user has to select one. I'm thinking of a way to implement it (of course, in a GUI). I have a few ideas. But just thought of posting here as there may be better alternatives..
Say, user has to select a name from a large set of user base. If I simply put a text field for user to enter the name, then there can be issues like entering same name in different formats, misspelling etc...
I see two options here
Using a combo box
Using a list (Actually i'm thinking of something like a tool tip. As I cant show the whole list always due to space issues)
But combo box won't be much user friendly i guess. As the user will have to scroll around the whole list to select an entry. If the number of entries are too large, this will be
Which means, now I'm left only one option. A popping up list, which will change the content according the text user is entering in the text field. So he can type first few letters and the list will show all the entries starting from the entered text. Got my point, right?
Are there any other better to achieve this kind of need?
If I'm going to implement above, what will be the best way to follow. I'm thinking of extending the JTextField to add required functionality. Well, I'll put some method to set the popup list entries. And I'll add some actionListner to watch the text field, and control the popup list accordingly...
Autocomplete is what you are probably looking for. Google for "java swing jcombobox autocomplete" and limit results for the last couple of years to get relevant results. There will be a lot of examples and ideas on how to implement this with custom code.
I believe there is also some custom libraries like "swingx" that provide at least partial or full implementations to save time.
http://swingx.java.net/
They have released code as recently as the beginning of this years so it appears active and might have what you need.
You could take a look at SwingLab's autocomplete feature, it allows you to attach it to a JCombBox, JList or JTextComponent
use AutoComplete JComboBox/JTextField
based on Standard Java Classes
no issue with larger sets of data
no issue with Focus, BackSpace Key, Caret
for better performance to required sort the array before use
simple workaround for setStrict(true/false), restrict input to array
I have a JList with items that I want to show two values. Is there a way to have it show a string name and then have a right justified string to show a value. Looking something like this:
Title__________________120
Title2_________________135
Is it possible to pass in two string to an item and have the first string display on the left and the second one on the right?
Sure, implement a custom renderer. You might return a JPanel with BorderLayout as the rendering component, with the LHS text in the WEST, and the RHS text in the EAST.
Another way is to shove HTML into the default renderer (a JLabel), using an HTML table that stretches across 100% of the width. Though the custom renderer would be a better choice for a number of reasons (e.g. not presuming the type of the default renderer is a label).
BTW - perhaps you should consider using a JTable for this kind of functionality. No hacks or custom classes needed.
..does the jtable allow selecting items?
Of course! Here is an example taken directly from How to Use Tables in the tutorial. 'Jane' is selected.
A table is a little more effort to set up and get right, but it is well worth the effort.
Would a JTable perform just as a JList ..
No, the table ultimately provides more functionality. But the things it does which a list can also do, work (for the user) in much the same way.
I am creating a java app that needs to display multiple documents worth of plain text. I have been playing around with JEditorPane (for each document to display text) with JTabbedPane (for document selection) but I think I would prefer to use something better (if it exists).
Is there an existing class that creates a dead simple editor with document selector functionality built in?
Nothing is built-in to do this, but JDesktopPane might be a better option for selection rather than a JTabbedPane.
For plain text, I would tend to prefer JTextArea "that displays multiple lines of text and optionally allows the user to edit the text." It is described in the article How to Use Text Areas. The append() method is particularly convenient. The articles Text Area Scrolling and Message Console are also very helpful.