Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/366.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何在JTable中隐藏网格线_Java_Swing_Jtable_Gridlines - Fatal编程技术网

Java 如何在JTable中隐藏网格线

Java 如何在JTable中隐藏网格线,java,swing,jtable,gridlines,Java,Swing,Jtable,Gridlines,我试图隐藏JTable的网格线,但没有结果。即使尝试更改网格线的颜色也不起作用。这是我的密码: // build the table tableView = new JTable(ttm); //Specifify the selection Listener and model listSelectionModel = tableView.getSelectionModel(); listSelectionModel.addListSelectionListener(new Share

我试图隐藏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());
    }
    }
始终绘制白色网格线。我被困在这里

我是否必须实现自定义视口才能摆脱此问题

提前感谢,,
亚历克斯

你必须设置两件事

  • 禁用网格显示
  • 零行/列单元间距
代码:

table.setShowGrid(false);
table.setIntercellSpacing(new Dimension(0, 0));
或者使用JXTable(从)为您执行以下操作:

xTable.setShowGrid(false, false);

仅jtable 1.设置显示水平线(false);或jTable1.setShowVerticalline(假);或者您可以使用2选项

jTable1.setShowHorizontalLines(false);

jTable1.setShowVerticalLines(false);

真奇怪
JTable.setShowGrid(false)
适用于我的自定义
TableCellRenderer
。发布更多相关代码可能是有益的,或者更好的是,发布一个。
我使用的是定制的CellRenderer,不知道这是否会导致问题。
这很容易理解。在不使用自定义渲染器的情况下尝试您的代码。@camickr尝试过,但没有成功。可能是重复的谢谢,伙计,这个单元格间距让我发疯了。我禁用了网格线,它看起来像是一个白色的网格。为此浪费了几个小时。