Java JTable分组
我想在我的JTable上订购一些。目前我有一个包含所有项目的大型JTable。但是因为我要处理(子)选择,所以我想进行一些分组 以文本形式表达:Java JTable分组,java,swing,user-interface,awt,Java,Swing,User Interface,Awt,我想在我的JTable上订购一些。目前我有一个包含所有项目的大型JTable。但是因为我要处理(子)选择,所以我想进行一些分组 以文本形式表达: +---------------------------------------------------------------+ | Heading1 Heading2 Heading3 Heading4 | +--------------------------------------------
+---------------------------------------------------------------+
| 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表达这一点:
实验性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);
}
}
导入java.awt.BorderLayout;
导入java.awt.Component;
导入java.awt.GridBagConstraints;
导入java.awt.GridBagLayout;
导入java.awt.Insets;
导入java.awt.event.ActionEvent;
导入java.awt.event.ActionListener;
导入javax.swing.Box;
导入javax.swing.JButton;
导入javax.swing.JFrame;
导入javax.swing.JLabel;
导入javax.swing.JPanel;
导入javax.swing.JScrollPane;
导入javax.swing.JTable;
导入javax.swing.table.AbstractTableModel;
公共类Main扩展了JPanel{
公共类TestGroup扩展了JPanel
{
公共测试组()
{
超级(新GridBagLayout());
int i;
对于(i=0;i<50;i++){
GridBagConstraints c1=新的GridBagConstraints();
c1.fill=GridBagConstraints.HORIZONTAL;
c1.x=1;
c1.gridx=0;
c1.gridy=i;
添加(新的TestGroupContainer(),c1);
}
Component glue=Box.createGlue();
GridBagConstraints glueConstraints=新的GridBagConstraints();
glueConstraints.gridx=0;
glueConstraints.gridy=i;
1.weighty=1;
添加(粘合、粘合约束);
}
}
公共类TestGroupContainer扩展了JPanel
{
公共TestGroupContainer()
{
超级(新GridBagLayout());
TestGroupBody容器=新的TestGroupBody();
GridBagConstraints headerConstraints=新的GridBagConstraints();
headerConstraints.fill=GridBagConstraints.HORIZONTAL;
headerConstraints.anchor=GridBagConstraints.NORTH;
headerConstraints.weightx=1;
headerConstraints.gridx=0;
headerConstraints.gridy=0;
添加(新的TestGroupHeading(容器)、headerConstraints);
GridBagConstraints bodyConstraints=新的GridBagConstraints();
bodyConstraints.fill=GridBagConstraints.HORIZONTAL;
bodyConstraints.anchor=GridBagConstraints.NORTH;
bodyConstraints.weightx=1;
bodyConstraints.weighty=1;
bodyConstraints.gridx=0;
bodyConstraints.gridy=1;
添加(容器、车身约束);
}
}
公共类TestGroupBody扩展JPanel实现ActionListener
{
私有布尔值是可见的;
私有类TestItemModel扩展了AbstractTableModel{
私有最终对象[][]行数据;
私有最终字符串[]columnNames={“测试”、“场景”、“测试模式”、“时间[min]”、“选定”、“状态”、“计时器”};
公共TestItemModel(最终对象[][]数据){
rowData=数据;
}
@凌驾
public int getRowCount(){
//TODO自动生成的方法存根
返回this.rowData.length;
}
@凌驾
公共字符串getColumnName(int列){
返回columnNames[列];
}
@凌驾
public int getColumnCount(){
//TODO自动生成的方法存根
返回columnNames.length;
}
@凌驾
公共对象getValueAt(int行索引、int列索引){
//TODO自动生成的方法存根
返回rowData[rowIndex][columnIndex];
}
@凌驾
公共布尔值isCellEditable(int-rowIndex、int-columnIndex){
返回false;
}
}
公共TestGroupBody()
{
超级(新边框布局());
isVisible=true;
对象[][]数据={
{“凯西”,“史密斯”,
“滑雪板”、“a”、“a”、“a”、“a”}
};
TestItemModel=新的TestItemModel(数据);
JTable table=新的JTable(模型);
添加(table.getTableHeader(),BorderLayout.PAGE_START);
添加(表格、边框布局、中心);
}
@凌驾
已执行的公共无效操作(操作事件e){
setVisible(isVisible=!isVisible);
}
}
公共类TestGroupHeading扩展了JPanel
{
公共测试组标题(ActionListener btnActionListener)
{
超级(新GridBagLayout());
GridBagConstraints hoi1Constraints=新的GridBagConstraints();
hoi1Constraints.anchor=gridbagstraints.WEST;
hoi1Constraints.gridx=0;
hoi1Constraints.gridy=0;
hoi1Constraints.weights x=0;
最终JButton按钮=新JButton(“-”);
addActionListener(btnActionListener);
addActionListener(新建ActionListener()){
@凌驾
已执行的公共无效操作(操作事件e){
if(button.getText().equals(“+”)){
按钮。setText(“-”);
}否则{
按钮。setText(“+”);
}
}
});
添加(按钮、选项1约束);
GridBagConstraints hoi2Constraints=新的GridBagConstraints();
hoi2Constraints.fill=GridBagConstraints.HORIZONTAL;
hoi2Constraints.anchor=GridBagConstraints.WEST;
hoi2Constraints.gridx=1;
hoi2Constraints.gridy=0;
hoi2Constraints.weights x=1;
hoi2Constraints.insets=新的insets(0,10,0,0);
增加(新的JLabel(“hoi2”),hoi2承包商;
}
}
公共静态void main(字符串[]args){
//为t安排一项工作