java swing Jtable 合并单元格
时间: 2023-10-13 15:04:50 浏览: 352
在Java Swing中,可以使用JTable实现表格的展示和操作。JTable提供了一些方法可以用来合并单元格。
下面是一个简单的示例代码,演示如何使用JTable合并单元格:
```java
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.EventQueue;
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.ListSelectionModel;
import javax.swing.UIManager;
import javax.swing.table.DefaultTableCellRenderer;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.JTableHeader;
public class JTableMergeCellDemo extends JFrame {
private static final long serialVersionUID = 1L;
private JPanel contentPane;
private JTable table;
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
JTableMergeCellDemo frame = new JTableMergeCellDemo();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
public JTableMergeCellDemo() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 450, 300);
contentPane = new JPanel();
contentPane.setBorder(UIManager.getBorder("ScrollPane.border"));
contentPane.setLayout(new BorderLayout(0, 0));
setContentPane(contentPane);
JScrollPane scrollPane = new JScrollPane();
contentPane.add(scrollPane, BorderLayout.CENTER);
String[] columnNames = {"姓名", "性别", "年龄", "爱好", "备注"};
Object[][] rowData = {{"张三", "男", 18, "篮球", "小张"},
{"李四", "女", 20, "足球", "小李"},
{"王五", "男", 22, "乒乓球", "小王"},
{"赵六", "女", 24, "羽毛球", "小赵"},
{"钱七", "男", 26, "游泳", "小钱"},
{"孙八", "女", 28, "跑步", "小孙"},
{"周九", "男", 30, "健身", "小周"},
{"吴十", "女", 32, "瑜伽", "小吴"}};
DefaultTableModel model = new DefaultTableModel(rowData, columnNames);
table = new JTable(model);
table.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
table.setRowHeight(30);
table.setPreferredScrollableViewportSize(new Dimension(400, 180));
table.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
JTableHeader header = table.getTableHeader();
header.setPreferredSize(new Dimension(header.getWidth(), 30));
header.setDefaultRenderer(new MergeHeaderCellRenderer());
// 合并单元格
MergeCellUtil.mergeCells(table, new int[]{0, 1}, new int[]{2, 3, 4});
scrollPane.setViewportView(table);
JPanel panel = new JPanel();
contentPane.add(panel, BorderLayout.SOUTH);
JButton btnNewButton = new JButton("获取选中行");
btnNewButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
int row = table.getSelectedRow();
if (row != -1) {
String name = (String) table.getValueAt(row, 0);
JOptionPane.showMessageDialog(null, "选中的行为:" + name);
}
}
});
panel.add(btnNewButton);
}
/**
* 表头合并单元格的渲染器
*/
private class MergeHeaderCellRenderer extends DefaultTableCellRenderer {
private static final long serialVersionUID = 1L;
@Override
public Component getTableCellRendererComponent(JTable table, Object value,
boolean isSelected, boolean hasFocus, int row, int column) {
super.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column);
setHorizontalAlignment(CENTER);
setBackground(Color.LIGHT_GRAY);
return this;
}
}
}
```
在这个示例中,我们使用了一个名为MergeCellUtil的工具类,其中包含了一个mergeCells方法,可以用来合并单元格。这个方法的实现如下:
```java
public class MergeCellUtil {
/**
* 合并单元格
*
* @param table JTable对象
* @param rows 要合并的行数组
* @param cols 要合并的列数组
*/
public static void mergeCells(JTable table, int[] rows, int[] cols) {
DefaultTableModel model = (DefaultTableModel) table.getModel();
for (int i = 0; i < rows.length; i++) {
int row = rows[i];
for (int j = 1; j < cols.length; j++) {
int col = cols[j];
model.setValueAt("", row, col);
table.getCellRenderer(row, col);
}
int width = 0;
for (int j = 0; j < cols.length; j++) {
int col = cols[j];
TableColumn column = table.getColumnModel().getColumn(col);
width += column.getPreferredWidth();
}
TableColumn column = table.getColumnModel().getColumn(cols[0]);
column.setPreferredWidth(width);
}
}
}
```
通过这个工具类,我们可以传入要合并的行和列的数组,然后在表格中实现单元格的合并。
需要注意的是,表格中的单元格合并只是外观上的合并,并不会改变单元格的数据。如果需要获取合并后的单元格数据,需要自行编写相关的代码来实现。
阅读全文