久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

可以避免在同一應用程序中的 JComponent 之間的

Can serialization be avoided in DnD between JComponents within the same application?(可以避免在同一應用程序中的 JComponent 之間的 DnD 中進行序列化嗎?)
本文介紹了可以避免在同一應用程序中的 JComponent 之間的 DnD 中進行序列化嗎?的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

問題描述

限時送ChatGPT賬號..

最近我解決了當 DnD 項目從 JList 到 JTable 對象時得到的神秘"IOException.顯然,我傳輸的對象必須是可序列化的.這是必須",還是有辦法避免序列化?

Recently I solved a "mysterious" IOException that I got while DnD items from a JList to the JTable objects. Apparently objects that I transfer must be serializable. Is this "a must", or there is a way to avoid serialization?

我必須注意的一件事 - 我要轉移的類型在不同的包中.

One thing I must note - the type I am transferring is in a different package.

推薦答案

你可以寫一個自定義的TransferHandler.例如,我相信 JTabel 的 TranferHandler 將導出一個以逗號分隔的字符串,然后導入將解析該字符串以將每個標記添加為不同的列.

You can write a custom TransferHandler. For example I believe a TranferHandler for a JTabel will export a String that is comma delimited and then the import will parse the string to add each token as a different column.

因此,在您的情況下,您可以以相同的方式導出數據.然后在您導入時,您需要能夠使用已解析的標記重新創建您的自定義對象.

So in your case you could export you data the same way. Then on you import you would need to be able to recreate your custom Object using the parsed tokens.

查看 拖放和數據傳輸上的 Swing 教程 了解更多信息和示例.

Take a look at the Swing tutorial on Drag and Drop and Data Transfer for more information and examples.

或者,如果 DnD 僅在您的 Java 應用程序之間,則可能比您傳遞對對象的實際引用更容易.這是我嘗試通過在面板之間拖動 Swing 組件來執行此類操作的示例:

Or maybe easier if the DnD is only between your Java application than you can pass the actual reference to the object. Here is an example of my attempt to do something like this by dragging a Swing component between panels:

import java.awt.*;
import java.awt.datatransfer.*;
import java.awt.event.*;
import java.beans.*;
import javax.swing.*;
import javax.swing.border.*;
import javax.swing.plaf.*;
import javax.swing.text.*;
import java.io.*;

public class DragComponent extends JPanel
{
//  public final static DataFlavor COMPONENT_FLAVOR = new DataFlavor(Component[].class, "Component Array");
    public static DataFlavor COMPONENT_FLAVOR;

    public DragComponent()
    {
        try
        {
            COMPONENT_FLAVOR = new DataFlavor(DataFlavor.javaJVMLocalObjectMimeType + ";class="" + Component[].class.getName() + """);
        }
        catch(Exception e)
        {
            System.out.println(e);
        }

        setLayout(null);
        setTransferHandler( new PanelHandler() );

        MouseListener listener = new MouseAdapter()
        {
            @Override
            public void mousePressed(MouseEvent e)
            {
                JComponent c = (JComponent) e.getSource();
                TransferHandler handler = c.getTransferHandler();
                handler.exportAsDrag(c, e, TransferHandler.MOVE);
            }
        };

        TransferHandler handler = new ComponentHandler();

        for (int i = 0; i < 5; i++)
        {
            JLabel label = new JLabel("Label " + i);
            label.setSize( label.getPreferredSize() );
            label.setLocation(30 * (i+1), 30 * (i+1));
            label.addMouseListener( listener );
            label.setTransferHandler( handler );
            add( label );
        }
    }

    private static void createAndShowUI()
    {
        DragComponent north = new DragComponent();
        north.setBackground(Color.RED);
        north.setPreferredSize( new Dimension(200, 200) );

        DragComponent south = new DragComponent();
        south.setBackground(Color.YELLOW);
        south.setPreferredSize( new Dimension(200, 200) );

        JFrame frame = new JFrame("DragComponent");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.add(north, BorderLayout.NORTH);
        frame.add(south, BorderLayout.SOUTH);
        frame.pack();
        frame.setLocationByPlatform( true );
        frame.setVisible( true );
    }

    public static void main(String[] args)
    {
        EventQueue.invokeLater(new Runnable()
        {
            public void run()
            {
                createAndShowUI();
            }
        });
    }
}

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

    @Override
    public Transferable createTransferable(final JComponent c)
    {
        return new Transferable()
        {
            @Override
            public Object getTransferData(DataFlavor flavor)
            {
                Component[] components = new Component[1];
                components[0] = c;
                return components;
            }

            @Override
            public DataFlavor[] getTransferDataFlavors()
            {
                DataFlavor[] flavors = new DataFlavor[1];
                flavors[0] = DragComponent.COMPONENT_FLAVOR;
                return flavors;
            }

            @Override
            public boolean isDataFlavorSupported(DataFlavor flavor)
            {
                return flavor.equals(DragComponent.COMPONENT_FLAVOR);
            }
        };
    }

    @Override
    public void exportDone(JComponent c, Transferable t, int action)
    {
        System.out.println(c.getBounds());
    }
}

class PanelHandler extends TransferHandler
{
    @Override
    public boolean canImport(TransferSupport support)
    {
        if (!support.isDrop())
        {
            return false;
        }

        boolean canImport = support.isDataFlavorSupported(DragComponent.COMPONENT_FLAVOR);
        return canImport;
    }

    @Override
    public boolean importData(TransferSupport support)
    {
        if (!canImport(support))
        {
            return false;
        }

        Component[] components;

        try
        {
            components = (Component[])support.getTransferable().getTransferData(DragComponent.COMPONENT_FLAVOR);
        }
        catch (Exception e)
        {
            e.printStackTrace();
            return false;
        }

        Component component = components[0];
        Container container = (Container)support.getComponent();
        container.add(component);
//      container.revalidate();
//      container.repaint();
        container.getParent().revalidate();
        container.getParent().repaint();

        JLabel label = (JLabel)component;
        DropLocation location = support.getDropLocation();
        System.out.println(label.getText() + " + " + location.getDropPoint());
        label.setLocation( location.getDropPoint() );
        return true;
    }
}

這篇關于可以避免在同一應用程序中的 JComponent 之間的 DnD 中進行序列化嗎?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!

【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!

相關文檔推薦

Parsing an ISO 8601 string local date-time as if in UTC(解析 ISO 8601 字符串本地日期時間,就像在 UTC 中一樣)
How to convert Gregorian string to Gregorian Calendar?(如何將公歷字符串轉換為公歷?)
Java: What/where are the maximum and minimum values of a GregorianCalendar?(Java:GregorianCalendar 的最大值和最小值是什么/在哪里?)
Calendar to Date conversion for dates before 15 Oct 1582. Gregorian to Julian calendar switch(1582 年 10 月 15 日之前日期的日歷到日期轉換.公歷到儒略歷切換)
java Calendar setFirstDayOfWeek not working(java日歷setFirstDayOfWeek不起作用)
Java: getting current Day of the Week value(Java:獲取當前星期幾的值)
主站蜘蛛池模板: 一区二区久久电影 | 国产小视频精品 | 欧美日韩亚洲在线 | 色香婷婷 | 精品一区二区三区在线播放 | 亚洲人成人一区二区在线观看 | 古典武侠第一页久久777 | 欧美一级特黄aaa大片在线观看 | 又爽又黄axxx片免费观看 | 日本不卡一区 | 久久久久国产一区二区三区四区 | 日韩av第一页 | 久久久久久亚洲 | 成人免费精品 | av看片网站 | 精品国产免费人成在线观看 | 国产成人精品一区二区三区在线观看 | 亚洲一区视频在线 | 国产乱码久久久久久 | 美女久久| 91视频一区 | 成人欧美一区二区三区在线播放 | heyzo在线| 91成人在线| 日本精品一区二区三区在线观看 | 99精品在线 | 国产一区免费 | 久久国内精品 | 国产福利在线播放 | 久久亚洲欧美日韩精品专区 | 中文字幕亚洲一区二区va在线 | 91av免费版 | 国产精品福利在线观看 | 欧美精品1区2区 | 国产真实精品久久二三区 | 久久99精品久久久97夜夜嗨 | 国产伦一区二区三区四区 | 天堂久久久久久久 | 亚洲精品久久久久avwww潮水 | 欧产日产国产精品视频 | 久久国产精品一区二区 |