maloney
maloney

Reputation: 1653

Drag and Drop JLabel scrolling bug

I have a bug in some code that hopefully someone can assist me with. So I have a drag and drop panel whereby the top part of the panel is the draggable target (JTable) and the bottom part is a list of available files (JTable). If the number of files in the list exceeds a certain amount so that the user has to scroll to get to the top (and to the draggable target), if they try to drag a file from the bottom of the list it does not allow the user to scroll to the target area.

Basically, the panel is made up of 2 JTables separated by a JLabel (just a thick black line). Once the user reaches this JLabel it does not scroll up any further. Is there a way of allowing the ScrollPanel to continue scrolling once the JLabel is reached??

Code is literally just:

JPanel panel = new JPanel(new MigLayout("insets 0, wrap 1", "[grow]"));
JPanel listsPanel = new JPanel(new MigLayout("insets 0, wrap 1","[grow]","[]0[5:5:5]0[]")); 
JLabel separatorLabel = new JLabel("_");

listsPanel.add(activeHouseStylesTable, "growx");

separatorLabel.setBorder(new LineBorder(Color.BLACK, 300));
separatorLabel.setAutoscrolls(true);

listsPanel.add(separatorLabel, "growx");
listsPanel.add(inactiveHousestylesTable, "growx");
setupDragAndDrop();

panel.add(listsPanel, "growx");

Thanks,

Upvotes: 0

Views: 193

Answers (1)

kleopatra
kleopatra

Reputation: 51525

Can't reproduce the problem: the code snippet below is a slightly adjusted version of yours, the upper table being a dragSource, the lower faking to be a dropTarget. Add the panel to any toplevel window and size it so that only part of the lower is visible. Then start a drag in the upper and move the mouse to the last visible row of the lower: the whole panel scrolls up until you release the mouse.

What's not happening by default (and might be the perceived problem, don' know from your description) is any scrolling to the dropTarget: size the frame such that the separator is barely visible, start a drag and move to the lower boundary of the visible content: no scrolling because the visible part is not a valid droplocation anyway so the internal autoscroll not triggered.

    JPanel panel = new JPanel(new MigLayout("insets 0, wrap 1", "[grow]"));
    JPanel listsPanel = new JPanel(new MigLayout("insets 0, wrap 1",
            "[grow]", "[]0[5:5:5]0[]"));
    JLabel separatorLabel = new JLabel("_");
    separatorLabel.setBorder(new LineBorder(Color.BLACK, 300));

    JTable upper = new JTable(20, 3);
    JTable lower = new JTable(20, 3);
    // enable drag in upper
    upper.setDragEnabled(true);
    TransferHandler tableTransfer = new TransferHandler() {
        @Override
        public boolean canImport(JComponent comp,
                DataFlavor[] transferFlavors) {
            return true;
        }
    };
    lower.setTransferHandler(tableTransfer);

    listsPanel.add(upper, "growx");
    listsPanel.add(separatorLabel, "growx");
    listsPanel.add(lower, "growx");
    panel.add(listsPanel, "growx");
    JScrollPane pane = new JScrollPane(panel);

Upvotes: 1

Related Questions