1
A
回答
4
這裏是我的嘗試:
- 設置
JTable
不吸取細胞,比第一小區中的其它之間的垂直線,設置Border
繪製左側垂直線。
import java.awt.*;
import java.awt.event.*;
import java.util.*;
import javax.swing.*;
import javax.swing.border.*;
import javax.swing.table.*;
public class TableGridLineTest {
public JComponent makeUI() {
JTable table = new JTable(5, 3);
table.setAutoCreateRowSorter(true);
table.setFillsViewportHeight(true);
table.setShowVerticalLines(false);
//table.setShowHorizontalLines(false);
table.setIntercellSpacing(new Dimension(0, 1));
table.setBorder(BorderFactory.createEmptyBorder());
table.getTableHeader().setBorder(BorderFactory.createEmptyBorder());
table.getTableHeader().setDefaultRenderer(new DefaultTableCellRenderer() {
private final CellBorder border = new CellBorder(2, 2, 2, 2);
@Override public Component getTableCellRendererComponent(
JTable table, Object value, boolean isSelected, boolean hasFocus,
int row, int column) {
JLabel c = (JLabel) super.getTableCellRendererComponent(
table, value, isSelected, hasFocus, row, column);
border.isStartCell = column == 0;
c.setBorder(border);
c.setBackground(Color.ORANGE);
return c;
}
});
table.setDefaultRenderer(Object.class, new DefaultTableCellRenderer() {
private final CellBorder border = new CellBorder(2, 2, 2, 2);
@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);
border.isStartCell = column == 0;
c.setBorder(border);
return c;
}
});
JScrollPane scroll = new JScrollPane(table);
scroll.setBorder(BorderFactory.createLineBorder(Color.ORANGE));
JPanel p = new JPanel(new BorderLayout());
p.setBorder(BorderFactory.createLineBorder(Color.DARK_GRAY, 20));
p.add(scroll);
return p;
}
public static void main(String... args) {
EventQueue.invokeLater(() -> {
JFrame f = new JFrame();
f.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
f.getContentPane().add(new TableGridLineTest().makeUI());
f.setSize(320, 240);
f.setLocationRelativeTo(null);
f.setVisible(true);
});
}
}
class CellBorder extends EmptyBorder {
private final Color gridColor = UIManager.getColor("Table.gridColor");
public boolean isStartCell = false;
protected CellBorder(int top, int left, int bottom, int right) {
super(top, left, bottom, right);
}
@Override public boolean isBorderOpaque() {
return true;
}
@Override public void paintBorder(Component c, Graphics g, int x, int y, int w, int h) {
Graphics2D g2 = (Graphics2D) g.create();
g2.translate(x, y);
g2.setPaint(gridColor);
if (!isStartCell) {
g2.drawLine(0, 0, 0, h - 1); // Left line
}
g2.dispose();
}
}
相關問題
- 1. 刪除jtable中的單元格邊框
- 2. 刪除MFMailComposeViewController HTML右邊距?
- 3. 無法刪除右邊距
- 4. 如何刪除右邊
- 5. 刪除最後一行時出錯Jtable
- 6. 刪除邊框右:最後一個孩子假級
- 7. 如何刪除網格行的邊界底線?
- 8. 完全除去jtable網格(單元格邊框)
- 9. 添加右邊界的最後一個表格標題列
- 10. jtable中整行的刪除線
- 11. 刪除UIPopoverPresentationController的左右邊距?
- 12. 我無法刪除右邊的行
- 13. 刪除高圖中的第一個和最後一個網格線?
- 14. 如何刪除CheckBox右邊的不需要的空格?
- 15. 刪除圖片框上的最右邊緣
- 16. 使用Kendo網格如何刪除列邊框?
- 17. 從右邊距移動一個最右邊的浮點數
- 18. jQPlot - 刪除垂直網格線
- 19. 從圖中刪除網格線
- 20. 如何刪除css中最後一列的邊框?
- 21. 在Wpf listview/gridview中刪除右邊的空白空間/列
- 22. FlowDocument中的表格會截斷最右邊的列,爲什麼?
- 23. 如何刪除不同列的html表格的最後一列
- 24. 如何刪除我的網站邊框線重疊我的邊欄邊框
- 25. Swing - JTable JScrollPane刪除下邊框或添加標題邊框
- 26. 刪除數據網格中選定單元格的邊框
- 27. 如何刪除列表中滿足一定條件的最左/最右元素?
- 28. 刪除表格邊框
- 29. CSS:如何刪除表中一行的左右邊界?
- 30. JTable中刪除行
我愛你的解決方案,謝謝!我想我可能不得不做我自己的BasicTableUI.paintGrid(),然後做一些關於表頭,但你的解決方案完美:) – dosa