Jtable single cell having multiple images - java

I want to display multiple images in a single cell of a jtable.I know how to display a single image in a cell but how to do the same in case of multiple images?

I want to display multiple images in a single cell of a jtable.I know
how to display a single image in a cell but how to do the same in case
of multiple images?
I'm wouldn't going this way, because XxxRanderer is called from every mouse and Key events over visible JTables Rectangle in the JViewport,
prepare this image before, put together all images to one and put only one Icon/ImageIcon to XxxTableModel
DefaultCellRenderer returns JLabel by default, add proper LayoutManager to JLabel and laid there images, save images to local variable to avoiding any FileIO on runtime

There are at least two ways I can think of doing this...
You could merge all the images into a single image and allow the default TableCellRenderer to render the result as normal
Or...
You need to supply your own TableCellRenderer capable of taking the multiple images and displaying them.
Probably the simplest would be to use a JPanel as a base and add a JLabel per image. This would be a rather expensive process (assuming your have a dynamic number of images per cell), as you would need to remove all the labels from the panel and add the new ones for EACH cell.
See Using Custom Renderers for more details.

Related

Table not displaying until clicked when a CellPainterWrapper instead of a TextPainter for columns

I would like to add dynamic icons to column headers. To do this I've created a class that extends CellPainterWrapper and added a CellPainterDecorator with a TextPainter as the base painter and my dynamic icon painter as the decorator painter.
If I then swap the CELL_PAINTER in the config to use my class instead of the TextPainter the table doesn't display. If I click on where a row should be it then resizes and displays the table as expected with the column header text and icons.
I've also tried using the SortableHeaderTextPainter and get the same issue occur.
configRegistry.registerConfigAttribute(CellConfigAttributes.CELL_PAINTER, new SortableHeaderTextPainter(),
DisplayMode.NORMAL, GridRegion.COLUMN_HEADER);
Overall this means there is something different with how TextPainter and SortableHeaderTextPainter is implemented. In particular I can see parts in TextPainter that will call commands to resize the layer but I'm struggling to figure out how this should be done.
What needs to be added for it to set the initial column/row sizes?
Either set the initial widths and heights via DataLayer or configure the TextPainter to calculate the dimensions. Either via setter or constructor parameter.
This is caused by one any of the painters having paintBg set to true and by my style not specifying the background color. No exceptions would be thrown but it would cause my table to not resize automatically.
To fix this I can either set paintBg to false for all of my painters or set the background color in the style.

Animated table of strings

I'm trying to write a scorekeeping app in Java that allows a server application to send scores to client applications. The clients will then display a list of teams, team IDs and their scores, sorted by score. Of course I could just use a swing JTable to display everything, but I want to achieve a unique effect: I want the text dynamically reorder itself every time a team moves up in the list. That is, it want to animate the text around the screen. I would also need to be able to update the contents of the labels after being added. What would be the best way to achieve this? Is there any component that allows you to add other components to it and place/move them freely?
JTable is a JComponent so you can set desired LayoutManager and add JLabels above the JTable. Then move/reorder them to achieve desired effect. See SwingWorker as well.
You could use a JTable and change the contents of the rows as teams move up. Or you could arrange a series of labels and change the text whenever you want. To change the value displayed for a JLabel you simply use the JLabel.setText("new value"); method.
I've never done this but I think you need to use a panel with a 'null' layout manager. Meaning you are responsible for absolutely positioning your elements
http://docs.oracle.com/javase/tutorial/uiswing/layout/none.html
You would need some kind of SwingWorker or Timer running to update the gui layout. Make sure you actually make the GUI changes on the EventThread, which you can do by using SwingUtilities.invokeLater();
As alternatives to a null layout, consider letting the layout work for you using
one of the marquee effects discussed here, or
shuffling labels in a suitable layout, shown here and here.

JTable change cell colors using TableCellRenderer

I am using a JTable in my GUI application as a grid to represent positions for a game. I want the cells of the table that represent a certain position of an object to have a certain color, and on some actions, the object to move (i.e the color cell to move around in the Grid /JTable). I know that I can change cell colors by making a class that extends DefaultTableCellRenderer , is this the only way it can be done? or is there a simpler way to chage cell colors??Also Is JXTable better than JTable for such an application ?
EDIT: I didn't include the fact that I need certain cell colors to change dynamically, i.e with button clicks, keyboard clicks ...etc, is that still possible with any TableCellRenderer in case I am still using JTable ?
Thanks,
As an alternative, consider using prepareRenderer(), as suggested by #mKorbel and shown in the article Table Row Rendering.
With JTable, DefaultTableCellRenderer is the best way to do it.
However, if you use JXTable, it'll be much easier to use a Highlighter and a custom Predicate. Generally, Predicates are used to color based on contents of the cell, but you could just as easily have it color based on row/column and some external value.
Look in http://www.jarvana.com/jarvana/view/org/swinglabs/swingx-core/1.6.2/swingx-core-1.6.2-javadoc.jar!/org/jdesktop/swingx/JXTable.html under Rendering and Highlighting, which is a striped table and pattern matching. You'd essentially do the same thing as the pattern highlighter, but with your own Predicate.

Interactive JTable

I would like to create an interactive JTable. For this, I would like to add JPanels in the cells of the table. Once the JPanels are in the cells, I can add my various components to the JPanels thus making the table interactive. Each JPanel could have different components. Would it be possible to accomplish this and only have to create 1 table cell editor and 1 table cell tenderer. Does anyone know of a better way to do it?
Thanks
EDIT: Thanks for the responses. I actually already have a framework I am using. I just needed a JTable that users could drag and drop images in, play movies, display graphs, etc... I already have the functionality to do those things, I just needed a JPanel to add them too. I wanted it to be displayed in a JTable so the cells could be sorted, moved, add/delete rows/col, and well structured. I couldn't get it to work using the JTable, so I went ahead an created my own. Its just a JPanel that contains smaller JPanels (the table cells) using the GridLayout. It works well enough for my puposes. Just a pain to rewrite all of the functionality from scratch that a table has.
This is hard. JTable actually uses the cell renderers only for painting the cell content. I would recommend to check if a gridlayout packaged into a scrollpane would be the easier solution.
It sounds like you're trying to use JTable as a docking framework. Assuming this is the case you're better off using something like MyDoggy or JDock which allow you to decompose your GUI into multiple split pane areas.
JSplitPane may be an alternative in this context: one pane would hold the JTable, while the other displays expanded details of the selected row. A compete example using GridLayout is shown here.

Align an image in a SWT TableViewer

I just found out, that its somehow impossible to align an image in a swt tableviewer. Creating a TableColumn with SWT.RIGHT (for instance) has no effect on that column if its labelprovider returns an image for it.
Question: Is there any other way to align an image instead of modifiying the image file itself and put some extra pixel into it?
I believe that you're running into a limitation of the underlying platform (or something to that effect). You can have total control over what goes into the cell if you draw it yourself. There is a snippet that shows you how to do this.
Table example snippet: draw images on right side of table item
Of course, you'll also have to draw any text you want in the cell.

Categories