java - JTable grid lines disappear unexpectedly -
i have s (very s) scce illustrates problem: there jtable , jtree both contained in jpanel borderlayout. of grid lines in jtable show, not 1 @ left, , not 1 @ top. have tried experimenting borders, placing jtable on own dedicated jpanel, etc.
i have gleaned experts bad idea call of setxxxsize methods, have not resorted setpreferredsize, example. in sscce can swap tree , table positions round uncommenting "east" line)
how make these obscured/missing gridlines reappear?
(nb idea table grow/shrink dynamically jtree, there same number of rows in table , tree... hence desire contain both components on same jpanel).
sscce:
import java.awt.*; import java.lang.reflect.invocationtargetexception; import javax.swing.*; import javax.swing.border.lineborder; public class tablegridprob { public static void main( string[] args ) throws interruptedexception, invocationtargetexception{ eventqueue.invokeandwait( new runnable(){ @override public void run() { jframe frame = new jframe( "table grid prob" ); frame.setsize( 800, 300 ); frame.setvisible( true ); jsplitpane split_pane = new jsplitpane(); jscrollpane split_jsp = new jscrollpane( split_pane ); jpanel left_panel = new jpanel(); left_panel.setlayout( new borderlayout() ); jtree jt = new jtree(); jt.setrowheight( 20 ); left_panel.add( jt, borderlayout.west ); // left_panel.add( jt, borderlayout.east ); jtable jtable = new jtable( 4, 1 ); // jtable.setborder( new lineborder( color.green, 3 )); jtable.setrowheight( 20 ); left_panel.add( jtable, borderlayout.center ); split_pane.setleftcomponent( left_panel ); frame.getcontentpane().add( split_jsp ); split_pane.setdividerlocation( 0.5d ); } }); } }
later
in answer suggestion have wrap jtable in jscrollpane... doesn't appear solve problem, @ least standard java l&f. have tried various permutations of sitting jtable on jscrollpane on jpanel, sitting jtable on own jpanel , giving latter lineborder, etc. problem appears bit trickier. chief suspects insets , layout managers... 1) neither of getinsets methods of jtable called in course of code 2) maybe borderlayout not ideal. did try gridlayout , boxlayout but, recall, neither of these solved problem...
later
even simpler sscce illustrates prob (cannot see left-hand vertical gridline)
import java.awt.*; import java.lang.reflect.invocationtargetexception; import javax.swing.*; public class tablegridprob2 { public static void main( string[] args ) throws interruptedexception, invocationtargetexception{ eventqueue.invokelater( new runnable(){ @override public void run() { jframe frame = new jframe( "table grid prob2" ); frame.setdefaultcloseoperation(jframe.exit_on_close); jscrollpane sp = new jscrollpane( new jtable( 20, 5 )); frame.getcontentpane().add( sp ); frame.pack(); frame.setvisible( true ); } }); } }
* later (6 sep) *
screenshots 2 sscces:
hope can see problem: missing vertical left-hand gridline (and top gridline appears) in both cases...
a few notes reference:
i've incorporated @reimeus' suggestion, i'm not sure want.
i'm sure border merely illustrative, note api authors, "recommend put component in
jpanel
, set border onjpanel
." in particular, green border obscures part of first table row.note gridlines belong ui delegate, shown here; if critical, plan test on each target l&f.
unless have specific reason wait on initial thread, use
invokelater()
.if
jtable
dominates initial appearance, overridingscrollable
interface methodgetpreferredscrollableviewportsize()
reasonable compromise of general guidelines here.use
pack()
leverage component's preferred size.make
setvisible()
last.
addendum: based on update, tablegridprob2
, added colored border enclosing panel see better. indeed, com.apple.laf.aquatableui
leaves one-pixel left margin, presumably selection rectangle. might try chosen l&f.
import java.awt.*; import javax.swing.*; import javax.swing.border.lineborder; import javax.swing.plaf.coloruiresource; public class tablegridprob2 { public static void main(string[] args) { uimanager.put("table.gridcolor", new coloruiresource(color.gray)); eventqueue.invokelater(new runnable() { @override public void run() { jframe frame = new jframe("table grid prob2"); frame.setdefaultcloseoperation(jframe.exit_on_close); jscrollpane sp = new jscrollpane(new jtable(20, 5)); jpanel p = new jpanel(new gridlayout()); p.setborder(new lineborder(color.green, 4)); p.add(sp); frame.getcontentpane().add(p); frame.pack(); frame.setvisible(true); } }); } }
import java.awt.*; import javax.swing.*; import javax.swing.border.lineborder; import javax.swing.plaf.coloruiresource; public class tablegridprob { public static void main(string[] args) { uimanager.put("table.gridcolor", new coloruiresource(color.gray)); eventqueue.invokelater(new runnable() { @override public void run() { jframe frame = new jframe("table grid prob"); frame.setdefaultcloseoperation(jframe.exit_on_close); jpanel leftpanel = new jpanel(new borderlayout()); jtree jt = new jtree(); jt.expandrow(1); leftpanel.add(jt, borderlayout.west); jtable table = new jtable(20, 1) { @override public dimension getpreferredscrollableviewportsize() { return new dimension(300, 200); } }; table.setborder(new lineborder(color.green, 4)); table.settableheader(null); leftpanel.add(new jscrollpane(table), borderlayout.center); jsplitpane splitpane = new jsplitpane(); splitpane.setleftcomponent(leftpanel); frame.add(splitpane); frame.pack(); frame.setlocationbyplatform(true); frame.setvisible(true); } }); } }
Comments
Post a Comment