Skip to main content

JTable editor component

No replies
Joined: 2008-02-12

I had a problem with JTable editor component which have it's preferred size bigger than cell rectangle.
First i tried it with JPopupMenu but then i ran into problem with focus, so i had to discard it.
Second i overrided add(Component c) in the JTable, where i recognise my oversized editor component and set it's bounds.
(editor component's bounds are set first in editCellAt(int, int, EventObject) method in JTable)

<br />
@Override<br />
public Component add(Component comp) {<br />
    if(comp instanceof JCalendarPanel) {<br />
        Rectangle bounds = comp.getBounds();<br />
        Dimension size = comp.getPreferredSize();<br />
        Rectangle newBounds = new Rectangle(bounds.x, bounds.y, size.width, size.height);<br />
        comp.setBounds(newBounds);<br />
    }<br />
    return super.add(comp);<br />
}<br />

But my editor component have's JTable in it too (JScrollPane with JTable view). So when i pressed mouse on JTable in the editor component it's bounds is set again in paintCell(Graphics, Rectangle, int, int) in the BasicTableUI, so that succed.
(This JTable in my editor component has something common or shared with the table it self, which i haven't today yet discovered)
Third i overrided my editor component's setBounds(Rectangle) method.

<br />
Dimension preferredSize;<br />
@Override<br />
public void setBounds(Rectangle r) {<br />
    if(preferredSize == null) {preferredSize = this.getPreferredSize();}<br />
    Rectangle newBounds = new Rectangle(r.x, r.y, preferredSize.width, preferredSize.height);<br />
    super.setBounds(newBounds);<br />
}<br />

This works pretty good, but i still have to look into painting cells in the area of the editor component bounds when cells editing is stopped by event. Shouldn't be so hard.
But the question is that, is there other (better) way to show oversized editor component?