My target is to display an abbreviation list with two entries per line: the abbreviation and the corresponding long version. For a nice layout I used a GridPane because of the vertical alignment over all entries - it's nice to read.
But I also want to scroll to the clicked abbreviation and set the focus on it like in a ListView version of it.
For example the # on page links in good old HTML. Is there another javafx layout element I miss to achieve this?
I don't believe there is a provided control that will work for the specific scenario you are describing. However, I think one of these options might work for you...
Use the TableView control and add two columns for the information you want to show (one for the abbreviation and another for the long version). TableViews also have the scrollTo and setFocus functionality you're looking for. Here is a good resource to get you started with the Tableview control. You can also style the Tableview with CSS to look less like a table and more like a list if thats what your intention is.
The second option is to set a custom cell factory on your ListView that builds custom cells using HBoxes, VBoxes, Labels, etc. to achieve your desired look. You would also want to use the cell factory to populate each ListView cell with an object that contains both the abbreviated text and long version text. A couple good resources, 1, 2
Although I think both option will work fine, I would suggest option 1 since in option 2 you are sort of building a table type structure anyway. I hope this is helpful!
Related
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 am trying to make a properties frame just like the one in netBeans (or Visual Studio). My problem is that I don't know exactly how to design it. First I thought I'll make it with JTable (2 columns, multiple rows) but then I realised that on the second column I will have different types of values (booleans, String, color choosers, etc.), but I think that JTable allows only 1 type of data to be placed in a column.
I would like someone to tell me "JTable allows multiple data types on the same column" and show me how to do it, or tell me a different approach to the problem.
You can perfectly tell a JTable to have a column that contains Object, this way you will be able to put whatever ou want in.
BUT.
You'll then have to implement a very good TableCellRenderer/TableCellEditor pair in order to display whatever the cell contains.
Another option would be to use a Grid or GridBag layout inside of a JScrollPane, then dynamically populate the cells of the grid with different editors depending on the data type of the property.
If you can use external libraries, the JGoodies FormLayout is really suited to create such dialogs. Just take a look at the screenshots in their demo.
There is also a rather good PDF available containing with some examples and explanations.
I am currently developing an Android app that is to be a counterpart to its sister iPhone prototype.
My task is to recreate the screen from a design mockup from the iPhone app in Android, as shown here:
What would be the best layouts / views to use for replicating this screen in an activity?
Thanks.
Your question does not clear some things up. Also, I disagree with Ted Hopp's answer. I believe he is assuming that the medications will be filled statically, or something like that.
By the looks of your app, I assume you will be filling stuff dynamically, probably with many medications at once, or no medication at all. By the arrows in the iPhone mock-up, I also assume you will want to perform actions depending on the medication selected.
All that said, I would use a ListView. My general concept (the one I'd probably use) would be like this:
You have your data source, and use a Loader/LoaderManager/etc. to fill that into a Cursor. I'd feed this cursor to a CursorAdapter (perhaps a SimpleCursorAdapter, which seems likely and easy by looking at that UI concept --- won't need to customize the adapter part itself). Finally, this adapter would be used in the ListView. This is fairly easy and won't take much code (the ListView-SimpleCursorAdapter-Cursor stuff, the data logic is certainly custom).
You can then manage each of your clicks using the proper ListView listeners, IIRC. And act accordingly depending on the item selected. I'm guessing that the user would, for example, startActivity to see a detail about each medication.
However, as I said, if you have a fixed number of medications (here says an experienced former leukemia patient here, so I always assume medications vary widely!), a single TableLayout would do, but I feel that's not the case.
Ah, and about the layout for each component/med, as I said, a simple layout would do. Probably a LinearLayout with horizontal orientation. Again, very simple to implement with a SimpleCursorAdapter.
There's a nice example of how to do this with a custom row view here. This is probably the cleanest way to go.
The closest built-in widget for this is a TableLayout. Take a look at the Hello Views tutorial project for an example of this in action. You might want to wrap it in a ScrollView.
The right approach is highly dependent on the requirements. As I see it, you have (at least) a few options:
TableLayout
ListView
Something custom
As David noted, the TableLayout is most appropriate if you have a fixed set of data, but you can make it work dynamically too simply by adding child views. The benefit of using the TableLayout is mainly a built-in implementation of columns, including dynamic column sizing based on contents. The down-side is the lack of built-in dynamic support, especially when working with large data sets.
A ListView is a better fit for dynamic and large data sets, but comes with the limitation of not supporting columns. Ted's link lets you simulate columns, but unlike TableLayout these columns have a fixed width (in percentage of the parent's width). The columns are not sized based on content. You could potentially try to do something to measure all the children, but it'd be tricky. It can also be mildly annoying to try to deal with headers in a ListView, though with a little searching you'll find plenty of resources to help you with this.
The third option is to roll your own AdapterView or AbsListView. Of course this involves a significant amount more work, but you could take a look at how TableLayout works and resize the columns based on the content. This would also get you support for adapters and all the benefits that come along with that. This is probably quite a bit of work though, especially resizing the columns based on content.
Consider whether your columns must resize to fit their content or not.
I've got a blocking problem with the sorting functionality of a JTable; this made stall the development of the spare time open source project for 4 months now. Hope to be pointed into the right direction here.
Context: I'm working on extending the functionality of the ps3mediaserver to add a media library with pms-mlx. The UI of the media server has been done using swing.
Problem: When clicking on a column header in the JTable, a seemingly random column gets sorted instead of the one having been clicked.
Current implementation: Here's the description of the different components and classes being used for the implementation:
ETable: As alternate row colours aren't supported by default in the JTable, I've switched to the ETable extending the JTable. Source comes from here
FileDisplayTable: This is the class creating the table. In the init() method, the sorting is being enabled with 'table.setAutoCreateRowSorter(true);'
FileDisplayTableCellRenderer: Exists to always align cell content on the left
FileDisplayTableColumnModel: Does some mapping between internal types and column names
FileDisplayTableAdapter: This class implements com.jgoodies.binding.adapter.AbstractTableAdapter to map the objects with the table columns.
Possible solutions:
Preferably, I'd like to keep the current implementation and figure out how to correct the sorting, but I doubt someone can help me out with that!? Additionally their are some bits of code I had to add because of strange behaviours; they're commented in the code
The alternate option would be to change the JTable for another control altogether. I've made some research but didn't find the solution I was hoping for. The constraints are that
it must be embeddable in a swing UI
preferably it should support data bindings
support alternate row colours
row sorting
At some point it will be possible to open an editing dialogue, where the content of the row has to be retrieved, can be edited and when saved the row has to be updated.
Before reworking the entire thing I'd like to be sure the component will be able to handle all I want to do with it.
I'm more used to create GUIs using .NET in Visual Studio. It's quite different and a lot more difficult to do the same with swing. Please show me I'm wrong :)
[edit] If someone is willing to reproduce the problem, either get the source or the binaries, launch the application, navigate to the media library tab. In the Genral section import some videos by adding some video files. Go to the library section, click on apply to refresh the list and try to sort the table.
It may be useful to know that JTable columns can be dragged by the user. As a result, the view (JTable or a subclass) and model (an implementation of TableModel) may have different column numbers. Similarly, a RowSorter may affect the order or number of rows in the view as compared to the model. The related conversion methods are mentioned in How to Use Tables: Sorting and Filtering. In particular: "When using a sorter, always remember to translate cell coordinates."
Addendum: As an alternative, consider org.netbeans.swing.etable.ETable or it's subclass org.netbeans.swing.outline.Outline, depicted here.
My current application uses a JList and everything is well (the only customization I did was to set the italic font on some of the rows).
Now I want to "upgrade" the user interface and instead of just labels in the List, I want a checkbox and a text field to be able to update the entry.
I started changing the code and adding a custom cell renderer and a custom cell model. My current problem is that the JPanel that the cell renderer is returning is not using the whole width of the container, so several list items are actually shown on the same line.
But now, I am wondering whether I should just change the whole thing to use JTable. I still need to add / remove items in the list though...
Any suggestion which one is better ? and if going with the JList, how should I go about fixing my current problem ?
In my experience using JTable is usually easier as it allows more complex data and functionality out-of-the-box. Usually when I try to do something the JList can't do, I just switch to JTable without a second thought. What you want sounds like something that should be pretty trivial to implement in a table. I suggest you try it out with some mock data to see if you can make it look and work the way you like (especially in case you want it to look like a list).
Try calling setLayoutOrientation(JList.VERTICAL) on your JList. That will restrict JList to a single column.