将JTable放在JTree中
在连接线程Jtable作为Jtree节点我把JTable JTree,但JTree
视图不能正确呈现在start_up,我怎么能setPreferredSize
JTable
,因为PreferredScrollableViewportSize
收缩JTable
与呈现TableHeader
+一行,一行保持隐藏,但扩展Node(s)TreeRenderer更改并将setPreferredSize
重新绘制为预期的Dimension
import java.awt.*; import javax.swing.*; import javax.swing.table.DefaultTableModel; import javax.swing.tree.*; public class TreeWithTableRenderer extends JFrame { private static final long serialVersionUID = 1L; private JTree tree; public TreeWithTableRenderer() { DefaultMutableTreeNode AA1 = new DefaultMutableTreeNode("AA1"); DefaultMutableTreeNode AA2 = new DefaultMutableTreeNode("AA2"); DefaultMutableTreeNode A = new DefaultMutableTreeNode("A"); A.add(AA1); A.add(AA2); DefaultMutableTreeNode BB1 = new DefaultMutableTreeNode("BB1"); DefaultMutableTreeNode BB2 = new DefaultMutableTreeNode("BB2"); DefaultMutableTreeNode B = new DefaultMutableTreeNode("B"); B.add(BB1); B.add(BB2); DefaultMutableTreeNode CC1 = new DefaultMutableTreeNode("CC1"); DefaultMutableTreeNode CC2 = new DefaultMutableTreeNode("CC2"); DefaultMutableTreeNode C = new DefaultMutableTreeNode("C"); C.add(CC1); C.add(CC2); DefaultMutableTreeNode root = new DefaultMutableTreeNode("root"); root.add(A); root.add(B); root.add(C); tree = new JTree(root); tree.setCellRenderer(new MyTableInTreeCellRenderer()); tree.setRowHeight(0); JScrollPane jsp = new JScrollPane(tree); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); add(jsp, BorderLayout.CENTER); pack(); setLocationRelativeTo(null); } class MyTableInTreeCellRenderer extends JPanel implements TreeCellRenderer { private static final long serialVersionUID = 1L; private JTable table; public MyTableInTreeCellRenderer() { super(new BorderLayout()); table = new JTable(); JScrollPane scrollPane = new JScrollPane(table); add(scrollPane); } public Component getTreeCellRendererComponent(JTree tree, Object value, boolean selected, boolean expanded, boolean leaf, int row, boolean hasFocus) { final String v = (String) ((DefaultMutableTreeNode) value).getUserObject(); table.setModel(new DefaultTableModel() { private static final long serialVersionUID = 1L; @Override public int getRowCount() { return 2; } @Override public int getColumnCount() { return 2; } @Override public Object getValueAt(int row, int column) { return v + ":" + row + ":" + column; } }); table.setPreferredScrollableViewportSize(table.getPreferredSize()); return this; } } public static void main(String[] args) throws Exception { SwingUtilities.invokeLater(new Runnable() { public void run() { new TreeWithTableRenderer().setVisible(true); } }); } }
摆脱scrollPane,它无论如何(到目前为止,我同意罗素:-),并添加表和它的标题面板,使用适当的LayoutManager:
public MyTableInTreeCellRenderer() { super(); setLayout(new BoxLayout(this, BoxLayout.PAGE_AXIS)); table = new JTable(); add(table.getTableHeader()); add(table); }
您可能需要稍微调整一下视觉效果,左边框和上边框线都不见了 – 不能完全确定哪个组件正常描绘它们,可能是scrollPane
编辑
忘了:查询scrollPane的prefSize在计算渲染组件的所需大小(在ui委托中完成,即在VariableHeightLayoutCache中完成)的原因是,scrollPane尚未configuration标题。 查询发生在将面板添加到rendererPane之前,完成的configuration在表的addNotify中完成,只有在将面板添加到层次结构
你可能只是摆脱ScrollPane,并直接布局在面板中的标题和表(使用null
布局pipe理器,所以你可以控制一切自己):
static class TableTreeCellRenderer extends JPanel implements TreeCellRenderer { private final JTable table; TableTreeCellRenderer() { table = new JTable(); setLayout(null); add(table.getTableHeader()); add(table); } public Dimension getPreferredSize() { Dimension headerSize = table.getTableHeader().getPreferredSize(); Dimension tableSize = table.getPreferredSize(); return new Dimension(Math.max(headerSize.width, tableSize.width), headerSize.height + tableSize.height); } public void setBounds(int x, int y, int width, int height) { super.setBounds(x, y, width, height); int headerHeight = table.getTableHeader().getPreferredSize().height; table.getTableHeader().setBounds(0, 0, width, headerHeight); table.setBounds(0, headerHeight, width, height - headerHeight); } public Component getTreeCellRendererComponent(JTree tree, Object value, boolean selected, boolean expanded, boolean leaf, int row, boolean hasFocus) { final String v = (String) ((DefaultMutableTreeNode) value).getUserObject(); table.setModel(new DefaultTableModel(new Object[][] { { v + "0", "1" }, { v + "2", "3" } }, new Object[] { "id", "value" } )); invalidate(); return this; } }