Size of cell-component is zero in Swing JTable - java

I’m currently writing a custom TableCellRenderer using a JEditorPane as cell-component for a JTable. I want to make hyperlinks clickable in that component.
Therefore, I wrote a tiny hack that forwards mouse events that are fired on the Table to the specific component. The problem is, that I cannot use the viewToModel function of the JEditorPane to determine the text under the mouse cursor, because the size of my editor pane is always zero and therefore viewToModel always returns -1.
Is there any way to get the components rendered in the table’s cells to have their correct sizes set? Do you have any suggestions on doing it in another way?
Some side information: I cannot use JTextPane since I'm using the scala programming language and there is currently no implementation for it. Furthermore, I have read that it may be possible to use the native event implementation by using custom cell editors. But I could not get this working in the first place. I want that, for instance, links are highlighted by hovering over. But, as far as I can see, that is not possible with the cell editor approach, since you have to click at the cell to start editing first.

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.

Trying to recreate Skype conversation panel with Swing

I am trying to recreate the Skype handles instant messaging using Swing components. I am using JList with a custom ListCellRenderer to render each cell in the list. The ListCellRenderer extends a JPanel, the JPanel simply contains a label (where I will put the username) and a JTextArea which is where the users' messages will go. The JTextArea is what Im having problems with.
Here's an image of what I have a the moment -
Ive removed the scrollpane that automatically comes with the textarea in netbeans.
I am showing the Navigator, the Design view and the actual program (the list has two elements) in this image.
The the text in textarea is actually much longer than in this image but it is not word wrapping. I have set lineWrap and wrapStyleWord to true in the properties box for this textarea but it doesn't seem to take any effect. I then tried to set maximum size using the properties box and that doesn't have any effect either.
Is there any way to control the padding/margins around components with netbeans gui designer. The automatic placement it gives me for spacing between components is either several pixels too small or two large. I need exact placement on the list's cell components.
For reference here is how skype's convesation panel looks (ive added in the red "Brian cs"'s as thats how I will be doing it in my program). As you can see the sentences wrap and there is an appropriate amount of space between cells. So anyone know how to achieve this using Swing?
The JTextarea is not the problem. The JList sets the heigth for each row. See JList.setFixedCellHeigth or setPrototypeCellValue

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.

JTextPane insert component, faulty vertical alignment

I have a JTextPane, into which I need to insert a JComponent. I'm using
JTextPane.insertComponent(Component)
The item is indeed inserted, but the vertical positioning is too high. Instead of having the bottom of the component aligned with the baseline of the current line of text, the component is way above that position, blocking out/over-painting lines of text appearing above.
I have tried calling setAlignmentY(float) with various values, on both the inserted component and the JTextPane, but it doesn't affect the behavior at all.
My guess: there seems to be some state inside my JTextPane or its Document that I need to be changing. But I don't know what it is.
Have you tried calling setSize(width, height) on the JComponent before you insert it into the JTextPane? It should work for most components.
I know, this is a pretty old question, but your approach of using setAlignmentY is totally correct. Don't know what code caused it to not work, but the javadoc of JTextPane.insertComponent(Component) says the following about the alignment:
The component is placed relative to the text baseline according to the
value returned by Component.getAlignmentY. For Swing components this
value can be conveniently set using the method
JComponent.setAlignmentY. For example, setting a value of 0.75 will
cause 75 percent of the component to be above the baseline, and 25
percent of the component to be below the baseline
So using textPane.setAlignmentY(1.0f) has the desired effect.
I ran into the same problem and could not find a solution using JTextPane or JEditorPane. But I was able to use JavaFX/WebView/WebEngine/JFXPanel. You will need to update to Java 8 (JDK 1.8). I made my own class HTMLPaneType, an extension of JFXPanel, and use HTMLPaneType in place of a JTextPane.
A JTextPane requires adding a HyperlinkListener if you want to respond to href clicks. HTMLPaneType requires adding a listener if you want to NOT respond to href clicks or to respond differently. In my case, I wanted to launch an external browser on an href click. I was able to do that with both the JTextPane and the extended JFXPanel. See also
http://blogs.kiyut.com/tonny/2013/07/30/javafx-webview-addhyperlinklistener/#.VK-JIHsueWN

JTree with left and right-aligned text in cells?

What I'm trying to do is create a JTree with each cell containing normal text but also containing a right-aligned text for each cell (right-aligned to the edge of the tree, regardless of the hierarchy level).
I've tried creating my own TreeCellRenderer, but the cells' sizes are not being updated. I also tried this idea with a custom tree UI but am experiencing similar issues.
I also have tried creating a custom component with a JPanel "glued" to the right of the tree. This has been the most successful, but I have been unable to have the tree cells extend to the right and touch this extra panel:
alt text http://img718.imageshack.us/img718/3676/problem.png
You could try to use a JXTreeTable, from the SwingX package.
jxtreetable example http://blogs.sun.com/geertjan/resource/outline-tim-browser.png
You would define then such an object with two columns (one for the tree, to other for the right aligned text), and it should work out fine.
To prevent it from looking like a table, though, I would recommend you to deactivate the header (setTableHeader(null)), and use their "packing" methods, to have the columns on optimal size.

Categories