JTable 中跨越两个单元格的图形内容

Graphic content that spans two cells in a JTable

我有一个使用 DefaultTableModel 的 JTable,它生成如下表格:

但是我很好奇是否有一种好方法可以像下面的示例那样在交叉的单元格中创建箭头...

这对我来说似乎很难。有什么办法可以使用 DefaultTableCellRenderer,或者使用 awt.Paint 包是可行的方法吗?想法?

这不会很容易使用单元格渲染器来完成,因为这将涉及很多匹配的事情。

您最简单的方法可能是覆盖整个 JTablepaintComponent 方法,然后在完成其余绘图后 (super.paintComponent())手动绘制箭头。

我通过覆盖 JTable 的 paintComponent() 得到了这个:

/**For drawing arrows between cells to clarify workflow.
             */
            @Override
            protected void paintComponent(Graphics g){

                super.paintComponent(g);

                Graphics2D g2 = (Graphics2D) g;
                g2.setStroke(new BasicStroke(2));

                //draw arrows
                for (int i = 0; i < getRowCount() - 1; i++)
                {
                    // -- draw arrow between A,B columns --
                    // stem
                    Rectangle cell = getCellRect(i, 0, false); // cell.x , y = top left corner coords. of rectangle
                    int outReach = 5; //length to go from paint starting point
                    Point center = new Point(cell.x + cell.width, cell.y + cell.height); //bottom right corner of cell
                    Point sw = new Point(center.x - outReach, center.y + outReach); //go southwest. remember x++ is right, y++ is down
                    Point ne = new Point(center.x + outReach, center.y - outReach); //go northeast
                    g2.setColor( Color.BLACK);
                    g2.drawLine(sw.x, sw.y, ne.x, ne.y);

                    //now the arrow tip starting from (sw.x, sw.y) - upper part here
                    outReach = 6;
                    center = new Point(sw.x, sw.y);
                    Point north = new Point(center.x, center.y - outReach); 
                    g2.drawLine(center.x, center.y, north.x, north.y);

                    //lower part of arrow tip
                    Point e = new Point(center.x + outReach, center.y); //east
                    g2.drawLine(center.x, center.y, e.x, e.y);


                    // -- X,Y columns --
                    cell = getCellRect(i, 2, false);
                    outReach = 5; 
                    center = new Point(cell.x + cell.width, cell.y + cell.height); //bottom right corner of cell
                    Point nw = new Point(center.x - outReach, center.y - outReach); 
                    Point se = new Point(center.x + outReach, center.y + outReach); 
                    g2.drawLine(nw.x, nw.y, se.x, se.y);

                    //horizontal tip (above other part)
                    outReach = 6;
                    center = new Point(nw.x, nw.y);
                    e = new Point(center.x + outReach, center.y ); 
                    g2.drawLine(center.x, center.y, e.x, e.y);

                    //vertical tip (lower)
                    Point south = new Point(center.x, center.y + outReach);
                    g2.drawLine(center.x, center.y, south.x, south.y);

                }
            }// end paintComponent()