Java Swing automatically gather data from forms - java

I have a form in Swing with a lot of textfields receiving data. The idea is when a button is clicked, the app gathers all the data from the textfields at once. Do you know a good practice for this? Or is it necessary to gather the data textfield by textfield?

If you are looking for a framework, try BeansBinding from JDesktop. It is quite well integrated with Netbeans IDE's Matisse GUI builder, and should be sufficient for most needs in a simple program.
If you are using Netbeans, here's a good primer to help you get started.
However, as BeansBinding works by abstracting all the complexity away, what you lose is more granular control. If you are developing a highly complex application that requires more control and tweaks, then I would suggest that roll up your sleeves and prepare to get your hands dirty. Register a class as a listener for each of the editable fields on the form, and save the data to a bean class that stores each of the necessary values.

You will have to read text from each text field/area one at a time.
In order to make your life easier - you'd want to put all the fields you want to read inside a List or a Vector when they get created; in this way, you will be able to read them in a simple for loop. If you need to know which field is which, use a HashMap and name each JTextField with a unique name to serve as key for the HashMap. Conversely, you can use .setName("SomeName") and use a List/Vector and use getName() to see what field it is.
As far as good practices go, unless you have many-many-many fields in your form, you don't need to worry because it won't take too long to do and you can put all the reading code in your listener method. However, if we're talking about many dozens or even hundreds of text fields, you'd want to start a new Thread to do that for you and disable the button so that the user knows something is going and can't press the same button again until the reading is done.

Related

How to make JTree to only display strings and not a file structure

I'm currently working on a client-server project where the client sends a request to the server and the server runs a database statement based on the information from the client. As there's no static length for the output I add all the data to a ArrayList<String> and send it back to the client.
The data that the client requests has a certain hierarchy so that one could also display it using an XML file (however the output to a GUI is wanted here instead of file creation). There's several ways of displaying I came up with. One would be using a simple text area, however using this, I need to do applying the layout myself and the option of folding where I can expand and collapse certain items is not possible.
Another possibility of course would be using a simple JList, however I read on Stackoverflow that using a JList to display hierarchical things is way easier done using a JTree.
When I decided trying to implement JTree I also found the Genealogy.java file provided by Oracle in their documentation of JTree. As to be seen in the first examples of this documentation there is always a filestructure being displayed and hence also a filestructure kind of representation (using folder- and file-symbols) is shown. In contrast to this Genealogy.java only shows the names of the persons so I tried to adapt the implementation from Genealogy.java to my case however I'm completely clueless of how to do this as I don't even know where these symbols come from and where I could possibly disable them and also don't really get what some of the methods I need to implement do.
As I'm currently really clueless of how to possibly accomplish that I really need some detailed help (only telling me to come up with my own implementation of JTree or TreeNode doesn't really help me at this point). Is there any simpler way or is the need to implement any of the before mentioned interfaces inevitable and if so, how would it be done?
EDIT:
This is how it currently would be displayed (example taken from the Oracle documentation, showing folder- and file-symbols in front of the string):
And this is how I want it to be displayed (also from the documentation, this time only displaying a string as node):
The answer almost certainly will be found via the (icon of the) TreeCellRenderer used for the tree nodes. See the File Browser GUI for tips. It shows how to set the icons in the FileTreeCellRenderer. Admittedly the point here is almost exactly opposite what it was there, but it still comes down to the same thing - the icons.
The section of the tutorial that covers it is How to Use Trees: Customizing a Tree's Display.

Recalling information form an array in list form

I am trying to recall a list of employees from an array in Java. I can either choose to do them individually via their ID number or bring all employee info up at once. I know how to code everything else but I cannot find a clear tutorial on how to recall the data and project it onto the text box I have programmed into my GUI. I have the array already created in a different class but I need to get the information from that one and display it in the GUI. Any help would be appreciated. Thanks.
If you want more info just let me know.
[The image is a screenshot of the simple GUI I made.]
There are many ways to do this, but one simple way that may help you to understand this process is as follows:
Implement a store method that simply writes all data to a file in a format of your choice. XML is common, as is TSV or CSV. Some of those are very easy to use, as predefined classes exist to handle them partially or completely.
You will have to read the data you want to store from whereever you have it in your application, possibly in the text boxes directly.
Accordingly implement a restore method that reads the file and fills it back into your data structure and/or the text boxes.

Do I need to prepare any security procedures using a JTextField

I have a JTextField in a GUI Java App. It's not connected to a Database so I'm not worried about SQL injection or anything like that, but I wonder if I need to worry about a stack overflow or another problem.
For example if the user decides to hold his finger on "A" ad nauseum. My Java reference book offered no procedures to prevent misuse of the application like this.
Each key-press produces a KeyEvent, which (over some steps) is translated to a call to insert the text in the Document of your JTextField. If the user inputs really really much text (many megabytes) (then more probably by cut'n'paste), you could get an OutOfMemory error, but this is still nothing that damages you, only the user, since he can't work with the application.
So, nothing really Security-relevant here.
You should be fine with the text field from a security standpoint. I would suggest worrying more about what the code does with the text or in response to the text. SQL injection being one of the most common concerns.

Java document state pattern?

I am a new to programming with Java and I would like to know if there is some common practice for managing state of opened document (is current state saved or dirty) , saving document, opening, creating new document and so on. How do you approach this?
Right now I have my little Swing application and have actions for opening and closing document and creating new one, but I don't know how to manage if user has saved file or not (I need this to check if user wants to create new one or open existing while working on current.)
Is there some pattern for this? All advices are very welcome since I am still learning how to swim with Java.
As far as I know Swing does not have mechanisms for managing document state. You have to do that yourself. But then, it is not that much code that has to be written and if you have several different documents in your app you can put that stuff in an abstract base class.
The basic approach has been outlined already: just have a "dirty" flag in your document data structure. You should put some thought into writing down which of your operations like "create", "open", "save", "close" should modify and evaluate this flag. I would suggest a state chart (not necessarily the UML state machine variant) as a tool to specify this.
If you need more complex functionality, especially undo/redo, take a look at the Memento pattern. Most of the code that has to be written when you use this pattern is specific to the application and its data structures (i.e. the types you create for managing documents) so it would be hard to impossible to effectively generalize this and put it into a framework like Swing or RCP.
You have a boolean variable named isDirty which starts at false.
Every time a change is made to the document it is set to true by the code.
All other program functions (Open,save,new menus e.t.c) check the status of this boolean
before doing anything else.
This way they also present the familiar dialogs: Are you sure you want to exit, Discard
your changes e.t.c
I have used this several times on real world Swing Apps
You may think about working with temporary versions of your document (i.e. you open main document, but when you edit it then temp document is created). In this case another user who opens the same document will see original doc. As I know it's common practice..
But I'm not sure that you want to maintain so complex behavior..

Fast search in java swing applications?

I'm wandering myself what component is the best for displaying fast search results in swing. I want to create something like this, make a text field where user can enter some text, during his entering I'll improve in back end fast search on database, and I want to show data bellow the text box, and he will be able to browse the results and on pres enter result will be displayed in table. So my question is is there any component which already have this logic for displaying?
Or is it's not, what is the best way to implement that.
This search will be something what ajax gives me on web, same logic same look and feel, if it's possible on desktop application.
Are you looking for something like an AutoComplete component for Java Swing?
SwingX has such a component. See here for the JavaDoc. It has a lot of utility methods to do various things, i.e. auto-completing a text box from the contents of a JList.
I strongly, strongly recommend that you take a look at Glazed Lists - this is one of the finer open source Java libraries out there, and it makes the bulk of what you are asking about super easy.
You will have to first attach a listener to the JTextFields Document to be notified whenever the user types in the field (or changes it).
From there, you can fire off any server-side code you need. The results of that can be used to update a listbox.
A few things to keep in mind:
The code to do the search against the backend must be in another thread
The code that updates the list box should update the list box's model
You will need to manage all your backend search results so that you only update the listbox with the most recent result (e.g. user types 'A', backenf searches for that. Meanwhile, user has typed 'C', kicking off a backend search for 'AC'. You need to ensure the results from the 'A' search dont' make it to the listbox if the 'AC' search results are available).
Use Hibernate Search.
The SwingHack (http://oreilly.com/catalog/9780596009076/) book has an example of this.
In the interest of killing two birds with one stone: have a separate indexing thread. This will:
Improve the speed of searches whenever they are executed.
Improve the responsiveness of the UI since indexing is happening in a separate thread.
Of course, exactly how you perform the indexing will vary widely depending on your particular application. Here is a good place to start researching: Search Indexing. And please, ignore the reference to Web 3.0 [sic].
It is possible of course. It is simple too. For drop down list of terms just use popup menu. This is simple. The background processing of entered text is simple too. Enjoy!

Categories

Resources