java - JTable inside JScrollPane inside JPanel with GridBagLayout, doesn't look as it should -
first, hier code:
import java.awt.dimension; import java.awt.gridbagconstraints; import java.awt.gridbaglayout; import java.awt.insets; import javax.swing.jcomponent; import javax.swing.jbutton; import javax.swing.jtoolbar; import javax.swing.jframe; import javax.swing.jlabel; import javax.swing.jpanel; import javax.swing.jscrollpane; import javax.swing.jtable; import javax.swing.table.tablecolumn; @suppresswarnings("serial") public class spielklasse extends jpanel{ private int ientries = 1; public spielklasse(string strtitle){ setlayout(new gridbaglayout()); //dummy fill vertical space gridbagconstraints c = new gridbagconstraints(); c.gridx = 0; c.gridy = 1000; c.fill = gridbagconstraints.both; c.weightx=1; c.weighty=1; c.gridwidth = 2; add(new jlabel(" "),c); } public void addentry(jcomponent component){ gridbagconstraints c = new gridbagconstraints(); c.anchor = gridbagconstraints.northwest; c.gridx = 1; c.gridy = ientries; c.weightx = 1; c.insets = new insets(10, 20, 10, 20); c.fill = gridbagconstraints.horizontal; add(component, c); ientries++; } public static jpanel addexampletablepanel() { string[] columnnames = { "first", "second", "third", "fourth", "fifth", "sixth" }; string[][] strarr = new string[100][columnnames.length]; (int = 0; < strarr.length; i++) { (int j = 0; j < strarr[0].length; j++) { strarr[i][j] = + " xxxxx " + j; } } jtable table = new jtable(strarr, columnnames) { @override public dimension getpreferredscrollableviewportsize() { int headerheight = gettableheader().getpreferredsize().height; int height = headerheight + (10 * getrowheight()); int width = getpreferredsize().width; return new dimension(width, height); } }; jscrollpane scrollpane = new jscrollpane(table, jscrollpane.vertical_scrollbar_as_needed, jscrollpane.horizontal_scrollbar_as_needed); table.setautoresizemode(jtable.auto_resize_off); tablecolumn column = null; (int = 0; < columnnames.length; i++) { column = table.getcolumnmodel().getcolumn(i); column.setpreferredwidth(50); column.setmaxwidth(50); column.setminwidth(50); } jtoolbar toolbar = new jtoolbar(); jbutton btn = new jbutton("test123"); toolbar.add(btn); jpanel panel = new jpanel(new gridbaglayout()); gridbagconstraints c = new gridbagconstraints(); c.fill = gridbagconstraints.horizontal; c.gridy = 0; c.weightx = 1; c.weighty = 1; c.gridwidth = 1; c.anchor = gridbagconstraints.line_start; panel.add(toolbar, c); c.gridy = 1; panel.add(scrollpane, c); return panel; } public static void main(string[] args) { spielklasse mainpanel = new spielklasse("test"); mainpanel.addentry(addexampletablepanel()); mainpanel.addentry(addexampletablepanel()); mainpanel.addentry(addexampletablepanel()); jframe frame = new jframe(); frame.setsize(300, 400); frame.setdefaultcloseoperation(jframe.exit_on_close); frame.getcontentpane().add(new jscrollpane(mainpanel)); frame.setvisible(true); } }
the class "spieklasse" should create jpanel multiple entries of different types tables, textboxes etc.
in example here, 3 jtable-containing panels should added.
this jtable inside jsrollpane , should have fixed column widths. jscrollpane inside jpanel, wich contains toolbar above table perform actions etc. jpanel added main panel of type "spielklasse". main panel inside jscrollpane.
here problem: - table-panel should have fixed height wich can set want. in code example, size fixed, dont know why , wrong size :-) - @ table horizontal scrollbar should appear, when size of frame smaller size of table (all columns together). when frame bigger, everying should stretched horizontal (works already)
i hope explanation enough , can me :-)
edit: updated improvement of camickr, vertical size problem solved.
the size fixed, dont know why , wrong size :-)
the size of scroll pane determined getpreferredscrollableviewportsize()
method of jtable.
when table created following hardcoded in jtable:
setpreferredscrollableviewportsize(new dimension(450, 400));
so if want control default width/height of scroll pane need override getpreferredscrollableviewportsize()
method return reasonable value. height should include column header number of rows in table wish display.
maybe like:
@override public dimension getpreferredscrollableviewportsize() { int headerheight = table.gettableheader().getpreferredsize().height; int height = headerheight + (10 * getrowheight()); int width = getpreferredsize().width; return new dimension(width, height); }
edit:
to use scrollablepanel can change code to:
//public class table8 extends jpanel{ public class table8 extends scrollablepanel{ private int ientries = 1; public table8(string strtitle){ setlayout(new gridbaglayout()); setscrollablewidth( scrollablepanel.scrollablesizehint.fit );
another problem gridbaglayout. if there not enough space display component @ preferred size, component snaps minimum size. causes problems scroll pane need add:
scrollpane.setminimumsize( scrollpane.getpreferredsize() );
or may easier use layout manager. maybe vertical boxlayout.
Comments
Post a Comment