每行带有可扩展 jtable 的 Jtable
Jtable with expandable jtable for each row
我正在尝试获得一个 table,您可以在其中使用 + 或任何符号来折叠行,例如 Access 中的这张图片:
实际上,我希望有相同的行为,使用这样的列标题。我将只使用 2 个级别:一个有 + 的级别和一个没有的级别(不同的 parents 有不同的孩子,但孩子不会是 parents)。目前我正在 Jtree 中尝试 Jtable 但我离 objective 很远而且结果也不近......我无法编辑 table 单元格我也无法拥有像这种情况的列名(如果我把列的标题放在每个 parent 将有标题)。您是否知道某些 java swing 组件具有与我想要获得的行为类似的行为或某些可以执行此操作的代码?对我编写的代码有帮助吗?
import java.awt.BorderLayout;
import java.awt.Component;
import java.util.EventObject;
// www .ja va 2s . c o m
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTree;
import javax.swing.ListSelectionModel;
import javax.swing.event.CellEditorListener;
import javax.swing.table.DefaultTableColumnModel;
import javax.swing.table.DefaultTableModel;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.TreeCellEditor;
import javax.swing.tree.TreeCellRenderer;
public class main extends JFrame {
private JTree tree;
public main() {
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.setRootVisible(false);
tree.setShowsRootHandles(true);
for (int i = 0; i<tree.getRowCount(); i++){
tree.expandRow(i);
}
tree.setCellRenderer(new MyTableInTreeCellRenderer());
tree.setRowHeight(0);
JScrollPane jsp = new JScrollPane(tree);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
add(jsp, BorderLayout.CENTER);
pack();
}
public static void main(String[] args) throws Exception {
new main().setVisible(true);
}
}
class MyTableInTreeCellRenderer extends JPanel implements TreeCellRenderer {
private JTable table;
public MyTableInTreeCellRenderer() {
super(new BorderLayout());
table = new JTable();
JScrollPane scrollPane = new JScrollPane(table);
table.setTableHeader(null);
table.setAutoCreateRowSorter(true);
table.setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);
table.setCellSelectionEnabled(true);
table.setColumnSelectionAllowed(true);
//No column ordering. Must be before setModel
table.setColumnModel(new DefaultTableColumnModel() {
/**
*
*/
private static final long serialVersionUID = 5;
public void moveColumn(int columnIndex, int newIndex) {
super.moveColumn(columnIndex, columnIndex);
}
});
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() {
@Override
public int getRowCount() {
return 1;
}
@Override
public int getColumnCount() {
return 3;
}
@Override
public Object getValueAt(int row, int column) {
return v + ":" + row + ":" + column;
}
});
table.setPreferredScrollableViewportSize(table.getPreferredSize());
return this;
}
}
对于寻找类似内容的人,我发现了这个:
http://www.hameister.org/JavaSwingTreeTable.html
如果您想使用 swingX,我发现的每个 link 都已损坏(java.net 不再存在),而且我找不到官方存储库。但是,您仍然可以在几个 github 像这样复制它的个人项目中找到 SwingX 项目的最后上传:
我正在尝试获得一个 table,您可以在其中使用 + 或任何符号来折叠行,例如 Access 中的这张图片:
实际上,我希望有相同的行为,使用这样的列标题。我将只使用 2 个级别:一个有 + 的级别和一个没有的级别(不同的 parents 有不同的孩子,但孩子不会是 parents)。目前我正在 Jtree 中尝试 Jtable 但我离 objective 很远而且结果也不近......我无法编辑 table 单元格我也无法拥有像这种情况的列名(如果我把列的标题放在每个 parent 将有标题)。您是否知道某些 java swing 组件具有与我想要获得的行为类似的行为或某些可以执行此操作的代码?对我编写的代码有帮助吗?
import java.awt.BorderLayout;
import java.awt.Component;
import java.util.EventObject;
// www .ja va 2s . c o m
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTree;
import javax.swing.ListSelectionModel;
import javax.swing.event.CellEditorListener;
import javax.swing.table.DefaultTableColumnModel;
import javax.swing.table.DefaultTableModel;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.TreeCellEditor;
import javax.swing.tree.TreeCellRenderer;
public class main extends JFrame {
private JTree tree;
public main() {
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.setRootVisible(false);
tree.setShowsRootHandles(true);
for (int i = 0; i<tree.getRowCount(); i++){
tree.expandRow(i);
}
tree.setCellRenderer(new MyTableInTreeCellRenderer());
tree.setRowHeight(0);
JScrollPane jsp = new JScrollPane(tree);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
add(jsp, BorderLayout.CENTER);
pack();
}
public static void main(String[] args) throws Exception {
new main().setVisible(true);
}
}
class MyTableInTreeCellRenderer extends JPanel implements TreeCellRenderer {
private JTable table;
public MyTableInTreeCellRenderer() {
super(new BorderLayout());
table = new JTable();
JScrollPane scrollPane = new JScrollPane(table);
table.setTableHeader(null);
table.setAutoCreateRowSorter(true);
table.setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);
table.setCellSelectionEnabled(true);
table.setColumnSelectionAllowed(true);
//No column ordering. Must be before setModel
table.setColumnModel(new DefaultTableColumnModel() {
/**
*
*/
private static final long serialVersionUID = 5;
public void moveColumn(int columnIndex, int newIndex) {
super.moveColumn(columnIndex, columnIndex);
}
});
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() {
@Override
public int getRowCount() {
return 1;
}
@Override
public int getColumnCount() {
return 3;
}
@Override
public Object getValueAt(int row, int column) {
return v + ":" + row + ":" + column;
}
});
table.setPreferredScrollableViewportSize(table.getPreferredSize());
return this;
}
}
对于寻找类似内容的人,我发现了这个:
http://www.hameister.org/JavaSwingTreeTable.html
如果您想使用 swingX,我发现的每个 link 都已损坏(java.net 不再存在),而且我找不到官方存储库。但是,您仍然可以在几个 github 像这样复制它的个人项目中找到 SwingX 项目的最后上传: