【问题标题】:jxtable with on gridline that is thicker带有较粗网格线的 jtable
【发布时间】:2014-05-07 01:38:17
【问题描述】:

我有一个jxtable。它有horizontalGridLines enabled。这是它的样子。

我希望水平网格线更粗。请参阅下面的所需外观。第二行之后的线应该有一个更粗的分隔线。

【问题讨论】:

    标签: java swing jtable swingx jxtable


    【解决方案1】:

    您可以覆盖 JXTable 中的paintComponent 方法。以下示例在第 2 行之后创建一个线宽为 3 像素的 JTable:

    JXTable table = new JXTable() {
        protected void paintComponent(Graphics g) {
            super.paintComponent(g);
    
            // Actual line thickness is: thickness * 2 + 1
            // Increase this as you wish.
            int thickness = 1;
    
            // Number of rows ABOVE the thick line
            int rowsAbove = 2;
    
            g.setColor(getGridColor());
            int y = getRowHeight() * rowsAbove - 1;
            g.fillRect(0, y - thickness, getWidth(), thickness * 2 + 1);
        };
    };
    

    【讨论】:

    • +1 一个 hack 和另一个一样好(或坏 :-)。一些建议:a)增加相邻行之一的rowHeight,否则该行可能会覆盖内容b)对于绘画位置,查询表格(通过getCellRect())而不是手动计算
    • @kleopatra 我最初有使用getCellRect() 的代码,但我对其进行了更改以使代码更简单。至于增加 rowHeight,这会起作用,但如果有 2 个不平衡的行,它看起来会很奇怪。顺便感谢您的反馈。
    • @kleopatra 嗯,我猜。但我认为这不会出错。
    【解决方案2】:

    网格线的绘制由表格的 ui-delegate 控制。没有办法干涉,所有的选择都是黑客。

    也就是说:如果目标行是第二个,SwingX'sh hack 将使用一个 Highlighter,它用一个 MatteBorder 装饰渲染器。

    table.setShowGrid(true, false);
    // apply the decoration for the second row only
    HighlightPredicate pr = new HighlightPredicate() {
    
        @Override
        public boolean isHighlighted(Component renderer, ComponentAdapter adapter) {
            return adapter.row == 1;
        }
    };
    int borderHeight = 5;
    // adjust the rowHeight of the second row 
    table.setRowHeight(1, table.getRowHeight() + borderHeight);
    Border border = new MatteBorder(0, 0, borderHeight, 0, table.getGridColor());
    // a BorderHighlighter using the predicate and the MatteBorder
    Highlighter hl = new BorderHighlighter(pr, border);
    table.addHighlighter(hl);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2023-03-28
      • 1970-01-01
      • 2012-02-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多