JTable中令单元格能够既居中且文本自动换行显示的话,本人暂时没有找到好方法。
但是在JTable中混合使用JTextArea和DefaultTableCellRenderer能够,让指定某一行居中或者多行显示文本。
InfoDialog.java
import java.util.*;
import java.awt.*;
import javax.swing.*;
public class InfoDialog extends JDialog {
private static final int WIDTH = 700;
private static final int HEIGHT = 750;
private ShowInfoDialog(JFrame owner,String[][]datas) {
setSize(WIDTH, HEIGHT);
setVisible(true);
JTable table = this.drawTable(datas);
add(table);
}
private JTable drawTable(String[][] datas){
String [] titles = {"名称","地址","电话","邮件"};
DefaultTableModel model = new DefaultTableModel(datas,titles){
public boolean isCellEditable(int row, int col){
return false;
}
};
JTable table = new JTable(model);
JTableHeader tableHeader = table.getTableHeader();
tableHeader.setReorderingAllowed(false);//表格列不可移动
//表头加宽加高
tableHeader.setPreferredSize(new Dimension(tableHeader.getWidth(),tableHeader.getHeight()+70));
//表头字体的大小
tableHeader.setFont(new Font("微软雅黑", Font.PLAIN, 26));
//自定义的单元格自动加高
table.getColumnModel().getColumn(0).setPreferredWidth(100);
table.getColumnModel().getColumn(1).setPreferredWidth(100);
table.getColumnModel().getColumn(2).setPreferredWidth(100);
table.getColumnModel().getColumn(3).setPreferredWidth(100);
table.setDefaultRenderer(Object.class, new TableCellTextAreaRenderer());//表格默认为<span style="font-family:Arial, Helvetica, sans-serif;">自动换行</span>
<span style="white-space:pre"> </span>//对第三列设置为文本居中
AlignmentCenterTabelCellRenderer render = new AlignmentCenterTabelCellRenderer();
table.getColumnModel().getColumn(2).setCellRenderer(render);
table.setEnabled(false);
return table;
}
}
AlignmentCenterTabelCellRenderer继承DefaultTableCellRenderer,重写getTableCellRendererComponent方法,设定单元格中的文本内容以及格式等等。
AlignmentCenterTabelCellRenderer.java
import java.awt.*
import javax.swing.*
public class AlignmentCenterTabelCellRenderer extends DefaultTableCellRenderer{
public AlignmentCenterTabelCellRenderer() {
setHorizontalAlignment(SwingConstants.CENTER);//设置为水平方向居中
setVerticalAlignment(SwingConstants.TOP);
}
public Component getTableCellRendererComponent(JTable table, Object value,
boolean isSelected, boolean hasFocus, int row, int column) {
// 计算当下行的最佳高度
int maxPreferredHeight = 0;
for (int i = 0; i < table.getColumnCount(); i++) {
setText(" " + table.getValueAt(row, i));
setSize(table.getColumnModel().getColumn(column).getWidth(), 0);
maxPreferredHeight = Math.max(maxPreferredHeight, getPreferredSize().height);
}
if (table.getRowHeight(row) != maxPreferredHeight)
table.setRowHeight(row, maxPreferredHeight);
setFont(new Font("微软雅黑", Font.PLAIN, 15));
setText(value == null ? "" : value.toString());
if(row%2 == 0){
setBackground(Color.decode("#E0FFFF")); // 设置奇数行底色
}else if(row%2 == 1){
setBackground(Color.decode("#FFFAFA")); // 设置奇数行底色
}
return this;
}
}
TableCellTextAreaRenderer 继承JTextArea ,重写getTableCellRendererComponent方法,与AlignmentCenterTabelCellRenderer相似。
TableCellTextAreaRenderer .java
import javax.swing.*;
import javax.swing.table.*;
import java.awt.*;
public class TableCellTextAreaRenderer extends JTextArea implements TableCellRenderer {
public TableCellTextAreaRenderer() {
setLineWrap(true); //设置为换行
setWrapStyleWord(true); //设置使用单词边界来换行
}
public Component getTableCellRendererComponent(JTable table, Object value,
boolean isSelected, boolean hasFocus, int row, int column) {
// 计算当下行的最佳高度
int maxPreferredHeight = 0;
for (int i = 0; i < table.getColumnCount(); i++) {
setText(" " + table.getValueAt(row, i));
setSize(table.getColumnModel().getColumn(column).getWidth(), 0);
//setMargin(new Insets(0,10,0,10));
maxPreferredHeight = Math.max(maxPreferredHeight, getPreferredSize().height);
}
if (table.getRowHeight(row) != maxPreferredHeight)
table.setRowHeight(row, maxPreferredHeight);
setFont(new Font("微软雅黑", Font.PLAIN, 15));
setText(value == null ? "" : value.toString());
if(row%2 == 0){
setBackground(Color.decode("#E0FFFF")); // 设置奇数行底色
}else if(row%2 == 1){
setBackground(Color.decode("#FFFAFA")); // 设置奇数行底色
}
return this;
}
}
最后调用即可:
public static void main(String[] args) {
String [][]datas = {{"小明","中国","12739382","13"},{"小白","美国","2383539382","19"}};
InfoDialog.ShowInfoDialog(null,datas);
}