答案 0 :(得分:1)
JTable
具有方法setRowHeight(...)
,该方法可让您增加特定行的高度。
关于边界,您可以签出Table Row Rendering。它演示了如何覆盖prepareRenderer(...)
的{{1}}方法以使用自定义JTable
。
答案 1 :(得分:1)
这是带有自定义行边框的JTable
的示例-用于特定行。这些特定的行由表行中的数据确定。
该示例将JTable
与自定义渲染器-CustomBorderRenderer
类一起使用,该类扩展了javax.swing.table.DefaultTableCellRenderer
。此示例应用程序中有两个类:TableCellRendererTester.java
和CustomBorderRenderer.java
。
以下是具有特定行的自定义边框的表的图像-“年份”值为“ 2012”。这两个类的源代码如下。
示例代码:
import java.awt.Dimension;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;
public class TableCellRendererTester {
private static final Object[] TABLE_COLUMNS = {"Book", "Author", "Year"};
private static final Object [][] TABLE_DATA = {
{"Book 1", "author 1", "1972"}, {"Book 2", "author 1", "1945"},
{"Book 3", "author 2", "2012"}, {"Book 4", "author 3", "1999"}
};
public static void main(String [] args) {
new TableCellRendererTester().createAndShowGUI();
}
public void createAndShowGUI() {
JFrame frame = new JFrame("Table Custom Row Border Tester");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(getTablePanel());
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
private JPanel getTablePanel() {
DefaultTableModel model = new DefaultTableModel(TABLE_DATA, TABLE_COLUMNS);
JTable table = new JTable(model);
table.setRowHeight(30);
CustomBorderRenderer renderer = new CustomBorderRenderer();
table.setDefaultRenderer(Object.class, renderer);
JScrollPane scrollpane = new JScrollPane(table);
scrollpane.setPreferredSize(new Dimension(400, 150));
scrollpane.setViewportView(table);
JPanel panel = new JPanel();
panel.add(scrollpane);
return panel;
}
}
渲染器类:
import java.awt.Color;
import java.awt.Component;
import javax.swing.JTable;
import javax.swing.JComponent;
import javax.swing.BorderFactory;
import javax.swing.border.Border;
import javax.swing.table.TableModel;
import javax.swing.table.DefaultTableCellRenderer;
public class CustomBorderRenderer extends DefaultTableCellRenderer {
private int prevRow;
private boolean customBorderFlag;
@Override
public Component getTableCellRendererComponent(JTable table, Object value,
boolean isSelected, boolean hasFocus, int row, int column) {
JComponent c = (JComponent) super.getTableCellRendererComponent(table,
value, isSelected, hasFocus, row, column);
if (row != prevRow) {
// Row has changed.
// Store it so that all the columns can be rendered
// with the custom border - if condition is met.
customBorderFlag = false;
prevRow = row;
TableModel m = table.getModel();
String s = (String) m.getValueAt(row, 2);
// Check if the row needs to be rendered with a custom border.
if (s.equals("2012")) {
customBorderFlag = true;
}
}
if (customBorderFlag) {
// Set custom border for all column cells - for a row.
c.setBorder(getCustomBorder());
}
return c;
}
/*
* Returns a custom border with:
* 1. Thick border
* 2. A margin between cell content (text) and the border.
*/
private Border getCustomBorder() {
Border insideMargin = BorderFactory.createEmptyBorder(10, 10, 10, 10);
Border thickBorder = BorderFactory.createLineBorder(Color.BLACK, 3);
Border borderWithMargin =
BorderFactory.createCompoundBorder(thickBorder, insideMargin);
return borderWithMargin;
}
}
编辑:更新了图像和CustomBorderRenderer.java
类,以显示带有边框的自定义边框 。