我想订购一下我的JTable。目前我有一个包含所有项目的大型JTable。但由于我处理(子)选择,我想做一些分组。
以文字形式表达:
+---------------------------------------------------------------+
| Heading1 Heading2 Heading3 Heading4 |
+---------------------------------------------------------------+
|+-------------------------------------------------------------+|
|| Subheading with a button to collapse/expand ||
|+-------------------------------------------------------------+|
| Itemx Itemx Itemx Itemx |
| Itemx Itemx Itemx Itemx |
| Itemx Itemx Itemx Itemx |
| Itemx Itemx Itemx Itemx |
|+-------------------------------------------------------------+|
|| Subheading with a button to collapse/expand ||
|+-------------------------------------------------------------+|
| Itemx Itemx Itemx Itemx |
| Itemx Itemx Itemx Itemx |
| Itemx Itemx Itemx Itemx |
| Itemx Itemx Itemx Itemx |
|+-------------------------------------------------------------+|
|| Subheading with a button to collapse/expand ||
|+-------------------------------------------------------------+|
| Itemx Itemx Itemx Itemx |
| Itemx Itemx Itemx Itemx |
| Itemx Itemx Itemx Itemx |
| Itemx Itemx Itemx Itemx |
+---------------------------------------------------------------+
在HTML5中表达: jsfiddle
实验性SSCCE:
import java.awt.BorderLayout;
import java.awt.Component;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.Box;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.table.AbstractTableModel;
public class Main extends JPanel {
public class TestGroup extends JPanel
{
public TestGroup()
{
super(new GridBagLayout());
int i;
for (i = 0; i < 50; i++) {
GridBagConstraints c1 = new GridBagConstraints();
c1.fill = GridBagConstraints.HORIZONTAL;
c1.weightx = 1;
c1.gridx = 0;
c1.gridy = i;
add(new TestGroupContainer(), c1);
}
Component glue = Box.createGlue();
GridBagConstraints glueConstraints = new GridBagConstraints();
glueConstraints.gridx = 0;
glueConstraints.gridy = i;
glueConstraints.weighty = 1;
add(glue, glueConstraints);
}
}
public class TestGroupContainer extends JPanel
{
public TestGroupContainer()
{
super(new GridBagLayout());
TestGroupBody container = new TestGroupBody();
GridBagConstraints headerConstraints = new GridBagConstraints();
headerConstraints.fill = GridBagConstraints.HORIZONTAL;
headerConstraints.anchor = GridBagConstraints.NORTH;
headerConstraints.weightx = 1;
headerConstraints.gridx = 0;
headerConstraints.gridy = 0;
add(new TestGroupHeading(container), headerConstraints);
GridBagConstraints bodyConstraints = new GridBagConstraints();
bodyConstraints.fill = GridBagConstraints.HORIZONTAL;
bodyConstraints.anchor = GridBagConstraints.NORTH;
bodyConstraints.weightx = 1;
bodyConstraints.weighty = 1;
bodyConstraints.gridx = 0;
bodyConstraints.gridy = 1;
add(container, bodyConstraints);
}
}
public class TestGroupBody extends JPanel implements ActionListener
{
private boolean isVisible;
private class TestItemModel extends AbstractTableModel {
private final Object[][] rowData;
private final String[] columnNames = { "Test", "Scenario", "Test mode", "Time [min]", "Selected", "State", "Timer" };
public TestItemModel(final Object[][] data) {
rowData = data;
}
@Override
public int getRowCount() {
// TODO Auto-generated method stub
return this.rowData.length;
}
@Override
public String getColumnName(int column) {
return columnNames[column];
}
@Override
public int getColumnCount() {
// TODO Auto-generated method stub
return columnNames.length;
}
@Override
public Object getValueAt(int rowIndex, int columnIndex) {
// TODO Auto-generated method stub
return rowData[rowIndex][columnIndex];
}
@Override
public boolean isCellEditable(int rowIndex, int columnIndex) {
return false;
}
}
public TestGroupBody()
{
super(new BorderLayout());
isVisible = true;
Object[][] data = {
{"Kathy", "Smith",
"Snowboarding", "a", "a", "a", "a"}
};
TestItemModel model = new TestItemModel(data);
JTable table = new JTable(model);
add(table.getTableHeader(), BorderLayout.PAGE_START);
add(table, BorderLayout.CENTER);
}
@Override
public void actionPerformed(ActionEvent e) {
setVisible(isVisible = !isVisible);
}
}
public class TestGroupHeading extends JPanel
{
public TestGroupHeading(ActionListener btnActionListener)
{
super(new GridBagLayout());
GridBagConstraints hoi1Constraints = new GridBagConstraints();
hoi1Constraints.anchor = GridBagConstraints.WEST;
hoi1Constraints.gridx = 0;
hoi1Constraints.gridy = 0;
hoi1Constraints.weightx = 0;
final JButton button = new JButton("-");
button.addActionListener(btnActionListener);
button.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
if (button.getText().equals("+")) {
button.setText("-");
} else {
button.setText("+");
}
}
});
add(button, hoi1Constraints);
GridBagConstraints hoi2Constraints = new GridBagConstraints();
hoi2Constraints.fill = GridBagConstraints.HORIZONTAL;
hoi2Constraints.anchor = GridBagConstraints.WEST;
hoi2Constraints.gridx = 1;
hoi2Constraints.gridy = 0;
hoi2Constraints.weightx = 1;
hoi2Constraints.insets = new Insets(0, 10, 0, 0);
add(new JLabel("hoi2"), hoi2Constraints);
}
}
public static void main(String[] args) {
// Schedule a job for the event-dispatching thread:
// creating and showing this application's GUI.
javax.swing.SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGUI();
}
});
}
public Main() {
super(new BorderLayout(0, 0));
JScrollPane scrollPane = new JScrollPane(new TestGroup());
add(scrollPane, BorderLayout.CENTER);
}
/**
* Create the GUI and show it. For thread safety, this method should be
* invoked from the event-dispatching thread.
*/
private static void createAndShowGUI() {
// Create and set up the window.
JFrame frame = new JFrame("SimpleTableDemo");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// Create and set up the content pane.
Main newContentPane = new Main();
newContentPane.setOpaque(true); // content panes must be opaque
frame.setContentPane(newContentPane);
// Display the window.
frame.pack();
frame.setVisible(true);
}
}
我的猜测是,开箱即用的JTable真的很难用。因此,我正在寻找任何其他合适的选择。我的SSCCE描述了一种可能的解决方案,但一个很大的缺点是JTableHeader的多个都在我的应用程序中。
有人有一个聪明的想法,可以轻松地显示我的HTML5示例中的数据吗?有或没有JTable,我真的不介意。
答案 0 :(得分:0)
原来JXTreeTable适合我的需要,这个提示是由@mrKorbel提供的。
有关如何实现基本JXTreeTable的详细教程,请参阅http://javanbswing.blogspot.nl/2013/08/swing-treetable-example-using.html。