Adding multiple icons with separate tooltips in JTable - java

I need to show multiple icons in a single column in JTable. I am using an modified Icon implementation.
Class IconCollection implements Icon {
Icon[] icons
}
Since it implements Icon interface , I am able to show multiple icons in single column. However I need to assign separate tooltips for each icon. Since JComponent has single setToolTipText() method, it is not possible.
Any suggestions?

Since the JTable has a decent implementation of getTooltipText which delegates to the component of the renderer, you can completely solve this in the renderer.
In the getTableCellRendererComponent method you can return any JComponent. If you would opt for a JPanel containing multiple icon instances with each a correct tooltip (instead of your big icon).

You could create a component to contain your icons and override getToolTipText(MouseEvent), basing the text returned on the relative position of the mouse.

Related

How to add a control panel on a JTable row on MouseOver (hover)

I have a non editable table, on which I'd like to display a row contextual panel (likely a JPanel). Somewhat like Gmail is doing: when moving the mouse over mail rows there's a simple tool bar showing up on that specific row.
Like in gmail the action of controls I'd like to display won't edit the values, instead they will use the value in the row to perform some offer work.
I have played with the following :
TableCellRenderer, the display mostly works, but it has limitations:
the component is only used for rendering, so one cannot use it to simply add multiple buttons
it requires another column
for the hovering behavior (ie display on row only when the mouse is hovering the row) it requires collaboration with the table's MouseListener
TableCellEditor, my table is not editable so the cell editor is neither called
it also requires a specific column
it also requires collaboration with the table's MouseListener
MouseMotionListener can be used to display a popup for certain coordinates
the popup feels like it's the right component for this
there's quite some code to handle the popup lifecycle (closing it when the mouse move out of the row, don't re-open a popup if there is already one open)
tool tips: as far as I am aware the swing tooltips do not allow to have control components like buttons, etc
I did related question and answer on stack overflow. But they all require to add a column to display and use these swing components.
Given that you have posted no code, this question is a bit broad.
Nevertheless, the way to do it would be to stick a JPanel in a JPopupMenu. You need to create a listener on your GUI to know when and where the JPopupMenu should appear
--- Edit ---
I think you have to add JMenus to a JPopupMenu, and what I suggested about adding a JPanel won't work cleanly. You can either use JPopupMenu, or use a JWindow and put your JPanel in that.

Using an image instead of JDialog/frame to hold swing components?

I currently have a JDialog (class that implements JDialog and is constructed like a jframe), and has 3 swing buttons placed on it. Currently I have it set, undecorated = true, to hide the outer frame. Is there any way to use my image to replace the default square frame?
This is what I aim for :
The blue square with shadow is the pre made image.
Regards
The blue square with shadow is the pre made image.
Well, the best way would be to set the background of the panel and then add a ShadowBorder to the panel. This will provide you with far more flexibility in the future as you can create many panels with different colors and reuse the same ShadowBorder instead of having to create an Image every time. I don't have an example of a ShadowBorder, but you might find one if you search the web.
Is there any way to use my image to replace the default square frame?
But if you really want to use your premade Image, then you can just:
create a JLabel and add your Image to the label as an Icon
add the label to the dialog
set the layout manager of the label
add your components to the label.

Draw round Button in LookAndFeel-Style

I'm experimenting with JRadioButton's to put them on a JToolbar and select the last one clicked. If i'd use JButtons they wouldn't keep the Selection.
Since JRadioButton always have that Dot, I need to draw them myself by overriding the paint-methods.
The Button's will be circles with an Icon in it. That works if I draw Images, but looks aweful. The problem I have is that I would like to draw the circle so that these Buttons always look like the JButtons with the current LookAndFeel.
How can i do that? I searched for a while now, but I didn't find methods to read some default-colors of the LookAndFeel which I could use.
So how can i read Background-Colors etc. of the current LookAndFeel to use it for some custom Button-Drawing?
So how can i read Background-Colors etc. of the current LookAndFeel to use it for some custom Button-Drawing?
See UIManager Defaults.
I need to draw them myself by overriding the paint-methods
Don't do custom painting in the component. If you don't like the default Icons, then create your own Icon and do the custom painting there or create an Image and use an ImageIcon. The you can use the setXXXIcon() methods.

Two images in JTreeNode?

I have some custom JTree. That tree has nodes with custom icons. I also have a class that extends DefaultTreeCellRenderer with method getTreeCellRendererComponent as simple as this:
super.getTreeCellRendererComponent(tree, value, sel, expanded, leaf, row, hasFocus);
MyTreeNode node = ((MyTreeNode) value);
Icon icon = node.getIcon();
setIcon(icon);
return this;
It works. My tree nodes will be rendered with custom icons in ICON TEXT format. BUT! Is there a way to put TWO icons in my node! I would like to have a node that looks like this: ICON TEXT ANOTHER_JLABEL_WITH_IMAGE.
How to do it?
You should create your own TreeCellRenderer that extends from something like a JPanel.
This way, you can define your own layout requirements and add your own components to support your requirements
You can also create an Icon which combines two (or more) other icons. See for example http://tips4java.wordpress.com/2009/03/29/compound-icon/
The default component used to render a Tree node is a JLabel. Thus you have access to the setIcon method.
But if you want to set two icons to a node you need to create a JPanel with two labels in it.
(J)Component / JLabel (returned by Renderer) haven't implemented any LayoutManager in the API
right JLabel has constructor for Text and Icon, there is not place for another Icon
basically every JComponents are containers too, part of Containers have got implemented LayoutManager in the API (JFrame, JPanel), you can to put any JComponent to the another
for example
great workarounds by camickr, especially Compound Icon

In Java Swing how can you manage a list of panels allowing multiple panels to be selected?

I’m working on an in-house app that tracks a bunch of tasks. I wanted to have a simple task monitor that would list the task name and the task’s status. I need this to look just a little nice, I’m no designer so whatever I do is going to suck, but a basic text display won’t work for the project requirements.
What I am essentially attempting to do is show something similar to the Firefox download window, the I-Tunes download window, and well I could name more but they all look basically the same. In each of these apps, each of the ‘progress panels’ is selectable. So to implement this I thought it would be simple to just use a list of JPanels each with a JProgressBar and a JLabel, each of which can just accept focus to determine if it and others are selected. I thought this was going to be an easy task, but if I use a JList it just displays text. I then just figured I would show all the task panels in a larger panel, but I cannot get the inner panels to recognize focus.
Is there a pattern for this? Is there a rolled standard solution that I just have not found? Or is there a better method for doing this? I don’t want to re-invent the wheel, but I thought this was just going to be simple.
It sounds like what you may be looking for is an JList.
You can add your items to the JList's by first adding your "task" to the JList object's ListModel (see the Create a Model section from The Java Tutorials), and then you'll want to assigned a custom ListCellRenderer which will accept your "task" and render on the JList as a JPanel in the list itself. The key here is to make your custom ListCellRenderer be able to display your "task" in the JList the way you want to have it show on the JList.
Take a look into the Writing a Custom Cell Renderer section from the How to Use Lists page of The Java Tutorials. It will describe how to make your custom ListCellRenderer so you can represent your "task" as anything you want.
To keep it short, you will implement the ListCellRenderer interface by implementing the getListCellRendererComponent which will return a Component which is the representation of your task in the JList. You'll probably want to either construct or instantiate your JPanel in this method and return it as the Component.
The standard way of doing this kind of things is to use JTable (or JList) as a container.
You don't have to use default renderes fot table cells, but you can specify your own renderer for specific cells. Take a look at CellRenderer
How about a JTable (which you can set to allow multiple rows to be selected) with an internal JPanel occupying the single cell in each row, which contains a JProgressBar and a JLabel. Or you could use a JList with the same structure as I just described.

Categories