如何在DragAndDrop期间在Main-JTable上绘制RowHeader-JTable的下拉线?

我在JScrollPane的视口中使用第二个JTable为主表构建RowHeader。主表上的拖动和拖放处于禁用状态。在行标题表上,启用了 DnD。

如果用户启动了对行标题的拖动,我想将绘制的行标题下拉线(图像中的黑线)扩展到主表上(如图像中的绿线)。

dropline for the maintable

有人对我有建议吗?
以下是 SSCCE:

import java.awt.Component;
import java.awt.EventQueue;
import java.awt.Font;
import java.awt.datatransfer.StringSelection;
import java.awt.datatransfer.Transferable;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import javax.swing.DropMode;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JViewport;
import javax.swing.TransferHandler;
import javax.swing.UIManager;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
import javax.swing.table.DefaultTableCellRenderer;
import javax.swing.table.JTableHeader;
import javax.swing.table.TableColumn;


public class DNDLinePainterExampleMain extends JFrame {

  public DNDLinePainterExampleMain() {
    JTable mainTable = new JTable(4, 3);
    mainTable.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);

    JTable rowTable = new RowHeaderTable(mainTable);
    rowTable.setAutoscrolls(true);
    rowTable.setDragEnabled(true);
    rowTable.setTransferHandler(new RowHeaderTransferHandler());
    rowTable.setDropMode(DropMode.INSERT_ROWS);

    JScrollPane scrollPane = new JScrollPane(mainTable);
    scrollPane.setRowHeaderView(rowTable);
    scrollPane.setCorner(JScrollPane.UPPER_LEFT_CORNER,
        rowTable.getTableHeader());
    this.add(scrollPane);
  }

  public static void main(String[] args) {
    EventQueue.invokeLater(new Runnable() {
      @Override
      public void run() {
        JFrame f = new DNDLinePainterExampleMain();
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        f.pack();
        f.setVisible(true);
      }
    });
  }


  /*
   * Use a JTable as a renderer for row numbers of a given main table. This
   * table must be added to the row header of the scrollpane that contains the
   * main table. from:
   * http://tips4java.wordpress.com/2008/11/18/row-number-table/
   */
  public class RowHeaderTable extends JTable implements ChangeListener,
      PropertyChangeListener {

    private final JTable table;

    public RowHeaderTable(JTable table) {
      this.table = table;
      table.addPropertyChangeListener(this);

      setFocusable(false);
      setAutoCreateColumnsFromModel(false);

      updateRowHeight();
      updateModel();
      updateSelectionModel();

      TableColumn column = new TableColumn();
      column.setHeaderValue("");
      addColumn(column);
      column.setCellRenderer(new RowNumberRenderer());

      getColumnModel().getColumn(0).setPreferredWidth(50);
      setPreferredScrollableViewportSize(getPreferredSize());

      getTableHeader().setReorderingAllowed(false);
    }

    @Override
    public void addNotify() {
      super.addNotify();
      Component c = getParent();
      // Keep scrolling of the row table in sync with the main table.
      if (c instanceof JViewport) {
        JViewport viewport = (JViewport) c;
        viewport.addChangeListener(this);
      }
    }

    /*
     * Delegate method to main table
     */
    @Override
    public int getRowCount() {
      return table.getRowCount();
    }

    @Override
    public int getRowHeight(int row) {
      return table.getRowHeight(row);
    }

    /*
     * This table does not use any data from the main TableModel, so just return
     * a value based on the row parameter.
     */
    @Override
    public Object getValueAt(int row, int column) {
      return Integer.toString(row + 1);
    }

    /*
     * Don't edit data in the main TableModel by mistake
     */
    @Override
    public boolean isCellEditable(int row, int column) {
      return false;
    }

    // implements ChangeListener
    @Override
    public void stateChanged(ChangeEvent e) {
      // Keep the scrolling of the row table in sync with main table
      JViewport viewport = (JViewport) e.getSource();
      JScrollPane scrollPane = (JScrollPane) viewport.getParent();
      scrollPane.getVerticalScrollBar().setValue(viewport.getViewPosition().y);
    }

    // implements PropertyChangeListener
    @Override
    public void propertyChange(PropertyChangeEvent e) {
      // Keep the row table in sync with the main table
      if ("rowHeight".equals(e.getPropertyName()))
        updateRowHeight();

      if ("selectionModel".equals(e.getPropertyName()))
        updateSelectionModel();

      if ("model".equals(e.getPropertyName()))
        updateModel();
    }

    private void updateRowHeight() {
      setRowHeight(table.getRowHeight());
    }

    private void updateModel() {
      setModel(table.getModel());
    }

    private void updateSelectionModel() {
      setSelectionModel(table.getSelectionModel());
    }


    /*
     * Borrow the renderer from JDK1.4.2 table header
     */
    private class RowNumberRenderer extends DefaultTableCellRenderer {

      public RowNumberRenderer() {
        setHorizontalAlignment(JLabel.CENTER);
      }

      @Override
      public Component getTableCellRendererComponent(JTable table,
          Object value, boolean isSelected, boolean hasFocus, int row,
          int column) {
        if (table != null) {
          JTableHeader header = table.getTableHeader();

          if (header != null) {
            setForeground(header.getForeground());
            setBackground(header.getBackground());
            setFont(header.getFont());
          }
        }

        if (isSelected) {
          setFont(getFont().deriveFont(Font.BOLD));
        }

        setText((value == null) ? "" : value.toString());
        setBorder(UIManager.getBorder("TableHeader.cellBorder"));

        return this;
      }
    }//class RowNumberRenderer

  }//class RowHeaderTable


  public class RowHeaderTransferHandler extends TransferHandler {

    @Override
    public int getSourceActions(JComponent c) {
      return COPY_OR_MOVE;
    }

    @Override
    protected Transferable createTransferable(JComponent c) {
      return new StringSelection(c.getName());
    }

    @Override
    public boolean canImport(TransferSupport supp) {
      return true;
    }
  }//class RowHeaderTransferHandler


}//class DNDLinePainterExampleMain

答案 1

感谢naugler,Xeon和Boro的巨大贡献,我现在使用他们的3个例子的组合。它看起来像这样:

the final table dropline

代码如下:

import java.awt.AlphaComposite;
import java.awt.Color;
import java.awt.Component;
import java.awt.EventQueue;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Rectangle;
import java.awt.datatransfer.StringSelection;
import java.awt.datatransfer.Transferable;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import javax.swing.DropMode;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTable.DropLocation;
import javax.swing.JViewport;
import javax.swing.RootPaneContainer;
import javax.swing.SwingUtilities;
import javax.swing.TransferHandler;
import javax.swing.UIManager;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
import javax.swing.table.DefaultTableCellRenderer;
import javax.swing.table.JTableHeader;
import javax.swing.table.TableColumn;


public class DNDLinePainterSolutionMain
{
  public static void main(String[] args)
  {
    EventQueue.invokeLater(new Runnable()
    {
      @Override
      public void run()
      {
        JFrame f = new MainFrame();
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        f.pack();
        f.setVisible(true);
      }
    });
  }
}//public class DNDLinePainterSolutionMain


class MainFrame extends JFrame
{
  public MainFrame()
  {
    JTable mainTable = new JTable(4, 3);
    mainTable.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);

    JTable rowTable = new RowHeaderTable(mainTable);
    rowTable.setAutoscrolls(true);
    rowTable.setDragEnabled(true);
    rowTable.setTransferHandler(new RowHeaderTransferHandler());
    rowTable.setDropMode(DropMode.INSERT_ROWS);

    //install the DropLocation-Extension:
    rowTable.addPropertyChangeListener("dropLocation",
        new DropLocationRepainter(this));

    JScrollPane scrollPane = new JScrollPane(mainTable);
    scrollPane.setRowHeaderView(rowTable);
    scrollPane.setCorner(JScrollPane.UPPER_LEFT_CORNER,
        rowTable.getTableHeader());
    this.add(scrollPane);
  }
}//class MainFrame


class RowHeaderTransferHandler extends TransferHandler
{
  @Override
  public int getSourceActions(JComponent c)
  {
    return COPY_OR_MOVE;
  }

  @Override
  protected Transferable createTransferable(JComponent c)
  {
    return new StringSelection(c.getName());
  }

  @Override
  public boolean canImport(TransferSupport supp)
  {
    return true;
  }
}//class RowHeaderTransferHandler


/**
 * Listens to a dropLocation-PropertyChange and repaints the DropLine.
 */
class DropLocationRepainter implements PropertyChangeListener
{
  private static RowHeaderDropLineGlassPane glassPane;
  private final RootPaneContainer           rootPaneContainer;

  public DropLocationRepainter(RootPaneContainer dndLinePainterSolutionMain)
  {
    this.rootPaneContainer = dndLinePainterSolutionMain;
  }

  @Override
  public void propertyChange(PropertyChangeEvent pce)
  {
    String propertyName = pce.getPropertyName();
    if ("dropLocation".equals(propertyName) && pce.getNewValue() != null)
    {
      if (glassPane == null)
      {
        rootPaneContainer.getRootPane().setGlassPane(
            glassPane = new RowHeaderDropLineGlassPane((RowHeaderTable) pce
                .getSource()));
      }
      rootPaneContainer.getRootPane().getGlassPane().setVisible(true);

      repaintDropLocation(((JTable) pce.getSource()).getDropLocation());
    }
    else
      if ("dropLocation".equals(propertyName))
        rootPaneContainer.getRootPane().getGlassPane().setVisible(false);
  }

  private void repaintDropLocation(DropLocation loc)
  {
    Component c = rootPaneContainer.getRootPane().getGlassPane();
    if (c instanceof RowHeaderDropLineGlassPane)
    {
      RowHeaderDropLineGlassPane glassPane = (RowHeaderDropLineGlassPane) c;
      glassPane.repaint();
    }
  }
}//class DropLocationRepainter


class RowHeaderDropLineGlassPane extends JPanel
{
  private RowHeaderTable table;

  public RowHeaderDropLineGlassPane(RowHeaderTable table)
  {
    this.table = table;
    setOpaque(false);
  }

  @Override
  public void paintComponent(Graphics g)
  {
    Graphics2D g2 = (Graphics2D) g;
    g2.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, 0.3f));
    Rectangle rect = table.getDropLineRect();
    if (rect != null)
    {
      Rectangle r = SwingUtilities.convertRectangle(table, rect, this);
      g2.setColor(new Color(40, 80, 0));
      g2.fill(r);
    }
  }
}//class RowHeaderDropLineGlassPane


/**
 * Use a JTable as a renderer for row numbers of a given main table. This table
 * must be added to the row header of the scrollpane that contains the main
 * table. From: http://tips4java.wordpress.com/2008/11/18/row-number-table/
 * <p>
 * Added {@code getDropLineRect()} for DropLine extension over the maintable.
 * </p>
 */
class RowHeaderTable extends JTable implements ChangeListener,
    PropertyChangeListener
{

  private final JTable mainTable;

  public RowHeaderTable(JTable mainTable)
  {
    this.mainTable = mainTable;
    mainTable.addPropertyChangeListener(this);

    setFocusable(false);
    setAutoCreateColumnsFromModel(false);

    updateRowHeight();
    updateModel();
    updateSelectionModel();

    TableColumn column = new TableColumn();
    column.setHeaderValue("");
    addColumn(column);
    column.setCellRenderer(new RowNumberRenderer());

    getColumnModel().getColumn(0).setPreferredWidth(50);
    setPreferredScrollableViewportSize(getPreferredSize());

    getTableHeader().setReorderingAllowed(false);
  }

  /*
   * called from the class RowHeaderDropLineGlassPane
   */
  public Rectangle getDropLineRect()
  {
    DropLocation loc = getDropLocation();
    if (loc == null /* || !loc.isDropable() */)
      return null;

    final Rectangle lineRect = new Rectangle();

    int index = loc.getRow();
    if (index < 0)
    {
      lineRect.setRect(0, 0, 0, 0);
      return null;
    }

    Rectangle r = getCellRect(index, 0, true);
    if (index == getRowCount())
      r.height = getCellRect(index - 1, 0, true).height;//if the last line is the DropTarget a height of 0 (of a non-existing Cell after the last row) is returned.

    lineRect.setRect(r.x, r.y - (r.height / 4d), getVisibleRect().width
        + mainTable.getWidth(), r.height / 2d - 1);
    return lineRect;
  }

  @Override
  public void addNotify()
  {
    super.addNotify();
    Component c = getParent();
    // Keep scrolling of the row table in sync with the main table.
    if (c instanceof JViewport)
    {
      JViewport viewport = (JViewport) c;
      viewport.addChangeListener(this);
    }
  }

  /*
   * Delegate method to main table
   */
  @Override
  public int getRowCount()
  {
    return mainTable.getRowCount();
  }

  @Override
  public int getRowHeight(int row)
  {
    return mainTable.getRowHeight(row);
  }

  /*
   * This table does not use any data from the main TableModel, so just return a
   * value based on the row parameter.
   */
  @Override
  public Object getValueAt(int row, int column)
  {
    return Integer.toString(row + 1);
  }

  /*
   * Don't edit data in the main TableModel by mistake
   */
  @Override
  public boolean isCellEditable(int row, int column)
  {
    return false;
  }

  // implements ChangeListener
  @Override
  public void stateChanged(ChangeEvent e)
  {
    // Keep the scrolling of the row table in sync with main table
    JViewport viewport = (JViewport) e.getSource();
    JScrollPane scrollPane = (JScrollPane) viewport.getParent();
    scrollPane.getVerticalScrollBar().setValue(viewport.getViewPosition().y);
  }

  // implements PropertyChangeListener
  @Override
  public void propertyChange(PropertyChangeEvent e)
  {
    // Keep the row table in sync with the main table
    if ("rowHeight".equals(e.getPropertyName()))
      updateRowHeight();

    if ("selectionModel".equals(e.getPropertyName()))
      updateSelectionModel();

    if ("model".equals(e.getPropertyName()))
      updateModel();
  }

  private void updateRowHeight()
  {
    setRowHeight(mainTable.getRowHeight());
  }

  private void updateModel()
  {
    setModel(mainTable.getModel());
  }

  private void updateSelectionModel()
  {
    setSelectionModel(mainTable.getSelectionModel());
  }


  /*
   * Borrow the renderer from JDK1.4.2 table header
   */
  private class RowNumberRenderer extends DefaultTableCellRenderer
  {
    public RowNumberRenderer()
    {
      setHorizontalAlignment(JLabel.CENTER);
    }

    @Override
    public Component getTableCellRendererComponent(JTable table, Object value,
        boolean isSelected, boolean hasFocus, int row, int column)
    {
      if (table != null)
      {
        JTableHeader header = table.getTableHeader();

        if (header != null)
        {
          setForeground(header.getForeground());
          setBackground(header.getBackground());
          setFont(header.getFont());
        }
      }

      if (isSelected)
        setFont(getFont().deriveFont(Font.BOLD));

      setText((value == null) ? "" : value.toString());
      setBorder(UIManager.getBorder("TableHeader.cellBorder"));

      return this;
    }
  }//class RowNumberRenderer

}//class RowHeaderTable

答案 2

好。忏悔时间。这是一些粗糙的,钝器代码就在这里。我很抱歉无法产生更优雅或更干净的解决方案。我是一个1级游客,在秋千的黑暗地下城。

此自定义代码属于快速和肮脏的类别。它不适用于列,我不确定所有的边缘情况,并且我不知道在绘制例程中间窃取另一个组件图形上下文的规则。底线:这是一个例子,而不是一个完整的解决方案。

由于您正在使用单独的表进行拖放(您的rowTable),因此该表没有很好的方式可以绘制到mainTable。应该注意的是,使用 PropertyChangeListeners 或某些东西将事件触发到 mainTable 可能会有一条光滑而闪亮的路径,但我没有管理它。下面是 BasicTableUI 的自定义扩展:

public class ExtendedDropLineTableUI extends BasicTableUI {

        private JTable drawTable;
        private Integer oldRow;

        //We give this UI instance a reference to a separate table for drawing
        public ExtendedDropLineTableUI(JTable drawTable) {
            this.drawTable = drawTable;
        }

        @Override
        public void paint(Graphics g, JComponent c) {
            super.paint(g, c);
            paintExtendedDropLine();
        }

        private void paintExtendedDropLine() {
            JTable.DropLocation loc = table.getDropLocation();
            if (loc == null) {
                drawTable.repaint();
                return;
            }

            //get the correct line color. no color? no line!
            Color color = UIManager.getColor("Table.dropLineColor");
            if (color == null) {
                return;
            }

            //try to repaint the draw table only if the row changes
            if (oldRow != null && oldRow != loc.getRow()) {
                drawTable.repaint();
            }
            oldRow = loc.getRow();
            //get location of cell rectangle
            int row = loc.getRow();
            int col = loc.getColumn();
            if (col >= table.getColumnCount()) {
                col--;
            }
            Rectangle rect = table.getCellRect(row, col, true);
            //adjust rectangle to fit between the cells
            if (rect.y == 0) {
                rect.y = -1;
            } else {
                rect.y -= 2;
            }
            //what's a line but a really thin rectangle?
            rect.height = 3;
            //extend the rectangle to the width of the drawing table
            rect.width = drawTable.getWidth();
            //draw the rectangle
            Graphics g = drawTable.getGraphics();
            g.setColor(color);
            g.fillRect(rect.x, rect.y, rect.width, rect.height);
        }
    }
}

您需要将此 UI 提供给 rowTable,以便它可以绘制到 mainTable,如下所示:

rowTable.setUI(new ExtendedDropLineTableUI(mainTable));

我严重依赖实际的源代码来劫持放置行的绘制,因为有关该内容的方法都是私有的。

编辑我忘了顺便说一句,我担心你可能会试图通过拖动rowTable中的单元格来重新排列mainTable中的整行。这个解决方案也没有考虑到这一点,它只是划清了界限。为此,您确实需要一个更优雅的解决方案,或者进行检查,使mainTable中的行与rowTable保持同步。