如何在JTable中隐藏网格线 [英] How to hide grid lines in JTable

查看:178
本文介绍了如何在JTable中隐藏网格线的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试隐藏JTable的网格线但没有结果。即使尝试更改网格线的颜色也不起作用。这是我的代码:

I'm trying to hide the grid lines of a JTable but with no results. Even trying to change the color of the grid lines does not work. Here is my code:

    // 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());
    }
    }

始终绘制白色网格线。我被困在这里......

White grid lines are always being drawn. I'm stuck here...

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

Do I have to implement a custom Viewport to get rid of this?

提前致谢,
Alex

Thanks in advance, Alex

推荐答案

你必须设置两件事


  • 禁用网格显示

  • 零行/列单元间间距

代码:

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

或者使用JXTable(来自 SwingX项目)为你做的:

Or use JXTable (from the SwingX project) which does it for you:

xTable.setShowGrid(false, false);

这篇关于如何在JTable中隐藏网格线的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆