Create a properties frame in Java - java

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.

Related

Combine javafx 2 ListView and GridPane features

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!

JList text alignment

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.

JTable with jgoodies sorting trouble

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.

JList with custom cell renderer vs Jtable

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.

Java Swing: what class can be used to implement this?

What Swing class can I use to implement something like this?
Add To List http://img22.imageshack.us/img22/3260/swingwidget.jpg
EDIT: Hmm..for some reason I cannot add the image here. Well, here's the link:
Basically, I need a list table, where each column can be of different type of gui (i.e. plain text, check box, or drop-down menu).
EDIT I have re-publish the image for you ;)
You would use a JTable to implement it. Your data will be stored in a TableModel. The DefaultTableCellRenderer and DefaultTableCellEditor should do what you need, but you can also customize the rendering/behavior if necessary.
More info on renderers/editors: http://java.sun.com/docs/books/tutorial/uiswing/components/table.html#editrender
Definitely JTable.
JTable allow you to define what render will have each column. That way you can add checkboxes, combos, textfields etc. to it.
Here's an screenshot:
alt text http://img43.imageshack.us/img43/9430/jtable.png
You can read more about it here: How to use Tables
JTable will do this for you, you're going to need to understand the MVC pattern pretty well to acheive this as you will need a custom model and renderer, but once you get the hang of it its not too hard.
David gearys book "Graphic Java Vol.2" has an excellent section on JTable, whilst this book is now fairly old I still personally think thats the best explanation of JTable I've seen.

Categories