I need to customize my jTable. All I need, is to put a custom Swing object (like jButon, jPanel, etc.) into the table cell. Is it possible? I'm trying:
jTable.getModel.setValueAt(jPanel1,0,0)
and
jTable.getModel.setValueAt(jPanel1.getUI(),0,0)
But the result is only a some kind of string, representing the object...
I'm aware of custom renderers, but still don't get the technique of registering them properly. How do you do that?
See How to Use Tables, Concepts: Editors and Renderers in the swing tutorial. It sounds like you're getting the default renderer for Object, which is "rendered by a label that displays the object's string value." You can use setDefaultRenderer to associate your class with your renderer, as shown in this example.
You have to make use of a ListCellRenderer for this,
Read a similar question here.
you can visit this webpage it's reeeeeeally helpful
Related
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 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.
Does anyone have any good tutorials on how to fill a JList (within a JPanel) with user inputted data. Specifically, I want to add people to a selected roster. Is this a matter of filling it with an ArrayList?
Any help would be much appreciated.
create a ListModel which wrapps your java.util.List (e.g. by extending AbstractListModel)
configure JList to use this model
create and configure a renderer to format/ display the Objects in your list as needed
http://docs.oracle.com/javase/tutorial/uiswing/components/list.html
You create a JList with a ListModel. When you edit your ListModel, it is reflected on JList as well.
I think it is having a little bit time with Google. I find the following results with Google.
How to Use Lists
Customize Your JList Display
Basic Swing components II
JLists, Data Models, and Cell Renderers
Use any layout manager like Gridlayout.
Is there a way to store both key and value in an item in a SWT Combo/List? If not, are there SWT components that look like a Combo and/or List that do?
Yes, there is. Use the setData( String key, Object value ) method.
Take a look at JFace, especially the viewers. The ComboViewer should be the right tool for the job.
Basically, the viewers are there for mapping between your domain objects and SWT components. There is even data binding functionality available which saves you a lot of boiler-plate code.
JFace Structured Viewers are the answer. Learn more about them here. They have a special combo implementation.
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.