gpt4 book ai didi

java - JTable 多行表头

转载 作者:行者123 更新时间:2023-12-02 05:47:20 28 4
gpt4 key购买 nike

我面临的问题的 SSSCE 发布在下面

import java.awt.Dimension;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.SwingUtilities;
import javax.swing.WindowConstants;
import javax.swing.table.DefaultTableColumnModel;
import javax.swing.table.DefaultTableModel;


public class TableTest extends JFrame{

private static final long serialVersionUID = 1L;
private JPanel panel;
private JPanel buttonPanel;
private JTable table;
private JTable headerTable;
JButton cancelButton;
private DefaultTableModel tableModel;
private DefaultTableModel headerTableModel;
String fileName = null;

public TableTest() {
init();
}

public void init() {
setTitle("Table");

//Panel
panel = new JPanel();
panel.setLayout(new GridBagLayout());
this.getContentPane().add(panel);

buttonPanel = new JPanel();

//cancel button
cancelButton = new JButton("Cancel");
cancelButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
dispose();
}
});

buttonPanel.add(cancelButton);

String[] headerNames = {"", "Col 1", "Col 2", "Col 1", "Col 2"};
String[] mainHeaderNames = {"Header 1", "Header2"};

headerTableModel = new DefaultTableModel();

for (int i = 0; i < mainHeaderNames.length; i++) {
headerTableModel.addColumn(mainHeaderNames[i]);
}

//table model
tableModel = new DefaultTableModel() {
private static final long serialVersionUID = 1L;
public boolean isCellEditable(int row, int column) {
if(column == 0){
return true;
} else {
return false;
}
}
public Class<?> getColumnClass(int columnIndex) {
switch (columnIndex) {
case 0:
return Boolean.class;
}
return null;
}
};

//set default model to the table
table = new JTable(tableModel);
headerTable = new JTable(headerTableModel);

// set table header
for (int col = 0; col < headerNames.length; col++) {
tableModel.addColumn(headerNames[col]);
}

//set table dimensions
int height = 0;
int width = 1000;
if(tableModel.getRowCount() < 20) {
height = tableModel.getRowCount()*16;
} else {
height = 350;
}
table.setPreferredScrollableViewportSize(new Dimension(width, height));

//table header column width and height
table.getTableHeader().setPreferredSize(new Dimension(100,28));
headerTable.getTableHeader().setPreferredSize(new Dimension(100,28));

//Set the width for column
DefaultTableColumnModel colModel = (DefaultTableColumnModel)table.getColumnModel();
colModel.setColumnSelectionAllowed(false);
colModel.getColumn(0).setPreferredWidth(5);
colModel.getColumn(1).setPreferredWidth(300);
colModel.getColumn(2).setPreferredWidth(200);
colModel.getColumn(3).setPreferredWidth(300);
colModel.getColumn(4).setPreferredWidth(200);

//table setting
table.setFillsViewportHeight(true);
headerTable.setFillsViewportHeight(true);
table.setAutoResizeMode(JTable.AUTO_RESIZE_ALL_COLUMNS);
headerTable.setAutoResizeMode(JTable.AUTO_RESIZE_ALL_COLUMNS);

//The header of column should be fixed.If set to true - columns can be re-arranged
table.getTableHeader().setReorderingAllowed(false);
headerTable.getTableHeader().setReorderingAllowed(false);

//Create the scroll pane and add the table to it.
JScrollPane scrollPane = new JScrollPane(table);
scrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
JScrollPane headerScrollPane = new JScrollPane(headerTable);
headerScrollPane.setPreferredSize(new Dimension(100,28));
setDefaultCloseOperation(WindowConstants.HIDE_ON_CLOSE);

//Add the scroll pane to this panel.
GridBagConstraints c = new GridBagConstraints();

c.anchor = GridBagConstraints.CENTER;
c.gridx = 0;
c.gridy = 0;
c.fill = GridBagConstraints.BOTH;
panel.add(headerScrollPane, c);

c.anchor = GridBagConstraints.CENTER;
c.gridx = 0;
c.gridy = 1;
c.fill = GridBagConstraints.BOTH;
c.weightx = 1.0;
c.weighty = 1.0;
panel.add(scrollPane, c);

c.fill = GridBagConstraints.NONE;
c.gridx = 0;
c.gridy = 2;
c.weightx = 0;
c.weighty = 0;
panel.add(buttonPanel, c);

addWindowListener(new java.awt.event.WindowAdapter() {
@Override
public void windowClosing(java.awt.event.WindowEvent windowEvent) {
setDefaultCloseOperation(WindowConstants.HIDE_ON_CLOSE);

int option = JOptionPane.showConfirmDialog(panel, "Do you want to close this window?", " Confirm" , JOptionPane.YES_NO_OPTION,JOptionPane.QUESTION_MESSAGE);

if (option == JOptionPane.YES_OPTION) {
dispose();
} else {
setDefaultCloseOperation(WindowConstants.DO_NOTHING_ON_CLOSE);
}
}
});

//Display the window.
if(tableModel.getRowCount() >= 0){
pack();
setAlwaysOnTop(true);
setLocationRelativeTo(this);
setVisible(true);
}
}

public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run(){
new TableTest();
}
});
}

}

我在这里使用两个JTable

GUI

我正在尝试实现类似的功能,如果我在标题 1 和标题 2 之间移动拆分,则下表中相应的拆分线也应该移动。

由于我使用两个jTables,我不确定如何实现这一点。或者还有其他方法吗,我是java新手,请帮忙。

最佳答案

您的布局错误。尝试制作这样的布局:

enter image description here

关于java - JTable 多行表头,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23932904/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com