如何完全去除摆动JTable中细胞周围的边界,这样细胞之间没有间隙? 这是我尝试过的:
table.setShowGrid(false);
table.setShowVerticalLines(false);
table.setShowHorizontalLines(false);
并编写单元格渲染器并使用:
setBorder(BorderFactory.createEmptyBorder(0,0,0,0));
每个单独的细胞。
但结果是细胞之间的线被移除,但它们之间留有1个像素的间隙。
答案 0 :(得分:12)
并编写单元格渲染器并使用:
一切都取决于Renderer返回但没有Renderer为我工作
setIntercellSpacing()
setShowGrid()
import java.awt.*;
import javax.swing.*;
import javax.swing.table.DefaultTableModel;
public class TableExample {
public TableExample() {
Object[][] data1 = new Object[50][5];
for (int i = 0; i < data1.length; i++) {
data1[i][0] = "Company # " + (i + 1);
for (int j = 1; j < data1[i].length; j++) {
data1[i][j] = "" + (i + 1) + ", " + j;
}
}
String[] headers = {"Col 1", "Col 2", "Col 3", "Col 4", "Col 5"};
DefaultTableModel model1 = new DefaultTableModel(data1, headers);
final JTable jTable3 = new TableBackroundPaint0(data1, headers);
//final JTable jTable3 = new JTable(data1, headers);
jTable3.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
jTable3.setModel(model1);
final JScrollPane sp3 = new JScrollPane();
sp3.setViewportView(jTable3);
jTable3.setIntercellSpacing(new Dimension(0, 0));
jTable3.setShowGrid(false);
//jTable3.setPreferredScrollableViewportSize(jTable3.getPreferredSize());
JFrame frame = new JFrame("tableSelection");
frame.add(sp3);
frame.setSize(new Dimension(600, 200));
//frame.pack();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
TableExample te = new TableExample();
}
});
}
}
class TableBackroundPaint0 extends JTable {
private static final long serialVersionUID = 1L;
TableBackroundPaint0(Object[][] data, Object[] head) {
super(data, head);
setOpaque(false);
((JComponent) getDefaultRenderer(Object.class)).setOpaque(false);
}
@Override
public void paintComponent(Graphics g) {
Color background = new Color(168, 210, 241);
Color controlColor = new Color(230, 240, 230);
int width = getWidth();
int height = getHeight();
Graphics2D g2 = (Graphics2D) g;
Paint oldPaint = g2.getPaint();
g2.setPaint(new GradientPaint(0, 0, background, width, 0, controlColor));
g2.fillRect(0, 0, width, height);
g2.setPaint(oldPaint);
for (int row : getSelectedRows()) {
Rectangle start = getCellRect(row, 0, true);
Rectangle end = getCellRect(row, getColumnCount() - 1, true);
g2.setPaint(new GradientPaint(start.x, 0, controlColor, (int) ((end.x + end.width - start.x) * 1.25), 0, Color.orange));
g2.fillRect(start.x, start.y, end.x + end.width - start.x, start.height);
}
super.paintComponent(g);
}
}
答案 1 :(得分:2)
您可以将边框颜色设置为单元格的背景颜色。另外,请看一下这些例子,
http://www.java2s.com/Code/Java/Swing-Components/CellBorderTableExample.htm
答案 2 :(得分:2)
如果你的列模型没有自动生成(没有数据绑定),你可以在JTable中使用这些方法来删除网格。
FORMAT(@startDate, 'yyyy-MMMM-dd') as '@StartDate',
FORMAT(@endDate, 'yyyy-MMMM-dd') as '@EndDate',
但是如果您使用数据绑定创建了列模型,那么您就遇到了麻烦。即使您调用这些方法,它们仍然会在每个列边距中涂上白色1像素间隙。
您可以使用自定义DefaultTableColumnModel删除该列边距。
$oozie job -rerun ...
然后,您可以将自定义表格列模型添加到表格中。
setShowGrid(false);
setRowMargin(0);
setShowVerticalLines(false);
setShowHorizontalLines(false);