加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > Java > 正文

java – 如何在JTable中隐藏网格线

发布时间:2020-12-15 00:48:00 所属栏目:Java 来源:网络整理
导读:我试图隐藏JTable的网格线,但没有结果.甚至尝试改变网格线的颜色不起作用.这是我的代码: // build the tabletableView = new JTable(ttm);//Specifify the selection Listener and modellistSelectionModel = tableView.getSelectionModel();listSelectionM
我试图隐藏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,200);
/**
 * Second gradient color
 */
private static final Color COLOR_2 = new Color(255,200);
/**
 * Controls gradient direction
 */
private static final float SIDE = 50;

private GradientPaint gradientPaint = new GradientPaint(0,COLOR_1,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,getWidth(),getHeight());
    }
    }

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

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

提前致谢,
亚历克斯

解决方法

你必须设置两件事情

>禁用网格显示
>零行/列单元间距

代码:

table.setShowGrid(false);
table.setIntercellSpacing(new Dimension(0,0));

或者使用JXTable(从SwingX project开始):

xTable.setShowGrid(false,false);

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读