Java table隐藏行线_java – 如何在JTable中隐藏网格线

这篇博客探讨了如何在Java的JTable组件中隐藏网格线的问题。作者通过设置表格属性`setShowGrid(false)`尝试隐藏网格线,但遇到了白色网格线仍然显示的困扰。他们还尝试了自定义单元格渲染器`CustomCellRenderer`,并调整了表格的其他视觉属性,如颜色和列宽。尽管如此,白色网格线仍然无法消除。作者询问是否需要实现自定义视口来解决这个问题,并寻求帮助。
摘要由CSDN通过智能技术生成

我试图隐藏JTable的网格线,但没有结果.甚至尝试改变网格线的颜色不起作用.这是我的代码:

// build the table

tableView = new JTable(ttm);

//Specifify the selection Listener and model

listSelectionModel = tableView.getSelectionModel();

listSelectionModel.addListSelectionListener(new SharedListSelectionHandler(tableView));

tableView.setSelectionModel(listSelectionModel);

//Add a mouse listener to our table and implement double click event

tableView.addMouseListener(new MouseAdapter(){

public void mouseClicked(MouseEvent e){

//If double click in a message show the Message Details window

if (e.getClickCount() == 2){

showMessageDetail();

}

}

} );

// set my own renderer

CustomCellRenderer mtr = new CustomCellRenderer();

tableView.setDefaultRenderer(Object.class, mtr);

// table properties

tableView.setGridColor(Color.black);

tableView.setShowGrid(false);

//tableView.setShowVerticalLines(false);

//tableView.setShowHorizontalLines(false);

tableView.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);

//hide header

tableView.setTableHeader(null);

// hide the id column

String columName = tableView.getColumnName(TableModel.COLUMN_ID);

tableView.getColumn(columName).setMaxWidth(0);

tableView.getColumn(columName).setMinWidth(0);

tableView.getColumn(columName).setWidth(0);

//load the messages in the table

loadMessages();

//adjust column width

tableView = autoResizeColWidth(tableView, ttm);

public class CustomCellRenderer extends JPanel implements TableCellRenderer {

/**

* First gradient color

*/

private static final Color COLOR_1 = new Color(255, 255, 255, 200);

/**

* Second gradient color

*/

private static final Color COLOR_2 = new Color(255, 0, 255, 200);

/**

* Controls gradient direction

*/

private static final float SIDE = 50;

private GradientPaint gradientPaint = new GradientPaint(0, 0, COLOR_1, SIDE, SIDE, COLOR_2, true);

private JLabel label = new JLabel();

public CustomCellRenderer() {

setOpaque(true);

setLayout(new BorderLayout());

add(label, BorderLayout.CENTER);

label.setHorizontalAlignment(SwingConstants.CENTER);

}

public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) {

label.setText(value.toString());

return this;

}

@Override

protected void paintComponent(Graphics g) {

super.paintComponent(g);

Graphics2D g2 = (Graphics2D) g;

g2.setPaint(gradientPaint);

g2.fillRect(0, 0, getWidth(), getHeight());

}

}

白色网格线总是被绘制.我被困在这里

我必须实现一个自定义的视口来摆脱这一点吗?

提前致谢,

亚历克斯

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值