手动鼠标拖动调整包含JScrollPane的JOptionPanel的大小

时间:2014-07-29 02:22:45

标签: java swing jpanel joptionpane jdialog

我有一个JScrollPane,它有一个filedrop功能,可以拖动文件。我使用JOptionPanel.showConfirmDialog调用了JScrollPane,因此有ok和cancel按钮。我希望能够将鼠标放在窗口边缘并拖动以调整其大小。我已经研究了很多不同的方法,但大多数都涉及框架,其中没有一个有JOptionPanes。如何执行此操作或使用ok和取消按钮制作可调整大小的面板,如showConfirmDialog

这是我的面板的功能:

public static List<String> displayFilePanel() {
        // Declare return object
        final List<String> listOfFiles = new ArrayList<String>();

        // Create scrollable panel to hold file list
        final JTextArea text = new JTextArea();
        JScrollPane scrollPane = new JScrollPane(text);  
        text.setLineWrap(true);  
        text.setWrapStyleWord(true); 
        scrollPane.setPreferredSize( new Dimension( 800, 800 ) );


        // Drag and drop feature
        // Add each file to listofFiles
        //new FileDrop( System.out, text, /*dragBorder,*/ new FileDrop.Listener() for debugging
        new FileDrop( null, text, /*dragBorder,*/ new FileDrop.Listener()
        {   public void filesDropped( java.io.File[] files )
            {   for( int i = 0; i < files.length; i++ )
                {   try
                    {   
                        text.append( files[i].getCanonicalPath() + "\n" );
                        listOfFiles.add(files[i].getCanonicalPath());
                    }   // end try
                    catch( java.io.IOException e ) {}
                }   // end for: through each dropped file
            }   // end filesDropped
        }); // end FileDrop.Listener

        int result = JOptionPane.showConfirmDialog(null, scrollPane, "Files To rename",
            JOptionPane.OK_CANCEL_OPTION, JOptionPane.PLAIN_MESSAGE);
        if (result == JOptionPane.OK_OPTION) {
            //System.out.println(text.getText());
        } else {
            System.out.println("File Pane Cancelled");

            //if Cancelled is pressed clear the listOfFiles so a blank list is returned
            listOfFiles.clear();
        }


        return listOfFiles;
    }

2 个答案:

答案 0 :(得分:3)

如图所示here,您可以将JOptionPane添加到JDialog,这是一个可调整大小的top-level container。此answer中显示了一个完整的示例。

JDialog dialog = new JDialog();
JOptionPane optPane = new JOptionPane();
…
dialog.add(optPane);

答案 1 :(得分:3)

好的,我想我明白了......

JOptionPane#initDialogprivate method,将dialog设置为不可调整大小......这对他们来说很好......但是你想要一个可调整大小的而不是窗口......

关于您唯一的选择是自己制作对话框,例如......

import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.awt.Window;
import java.awt.event.ComponentAdapter;
import java.awt.event.ComponentEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import javax.swing.JDialog;
import javax.swing.JOptionPane;
import static javax.swing.JOptionPane.VALUE_PROPERTY;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

public class CustomOptionPane {

    public static void main(String[] args) {
        new CustomOptionPane();
    }

    public CustomOptionPane() {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
                }

                final JTextArea text = new JTextArea(10, 20);
                JScrollPane scrollPane = new JScrollPane(text);
                text.setLineWrap(true);
                text.setWrapStyleWord(true);

                final JOptionPane optionPane = new JOptionPane(scrollPane, JOptionPane.PLAIN_MESSAGE, JOptionPane.YES_NO_CANCEL_OPTION);
                final JDialog dialog = new JDialog((Window) null, "Files To rename");

                dialog.add(optionPane, BorderLayout.CENTER);
                dialog.pack();
                dialog.setLocationRelativeTo(null);

                final PropertyChangeListener listener = new PropertyChangeListener() {
                    @Override
                    public void propertyChange(PropertyChangeEvent event) {
                // Let the defaultCloseOperation handle the closing
                        // if the user closed the window without selecting a button
                        // (newValue = null in that case).  Otherwise, close the dialog.
                        if (dialog.isVisible()
                                && (event.getPropertyName().equals(VALUE_PROPERTY))
                                && event.getNewValue() != null
                                && event.getNewValue() != JOptionPane.UNINITIALIZED_VALUE) {
                            dialog.setVisible(false);
                        }
                    }
                };

                WindowAdapter adapter = new WindowAdapter() {
                    private boolean gotFocus = false;

                    public void windowClosing(WindowEvent we) {
                        optionPane.setValue(null);
                    }

                    public void windowClosed(WindowEvent e) {
                        optionPane.removePropertyChangeListener(listener);
                        dialog.getContentPane().removeAll();
                    }

                    public void windowGainedFocus(WindowEvent we) {
                        // Once window gets focus, set initial focus
                        if (!gotFocus) {
                            optionPane.selectInitialValue();
                            gotFocus = true;
                        }
                    }
                };
                dialog.addWindowListener(adapter);
                dialog.addWindowFocusListener(adapter);
                dialog.addComponentListener(new ComponentAdapter() {
                    @Override
                    public void componentShown(ComponentEvent ce) {
                        // reset value to ensure closing works properly
                        optionPane.setValue(JOptionPane.UNINITIALIZED_VALUE);
                    }
                });

                optionPane.addPropertyChangeListener(listener);

                dialog.setModal(true);
                dialog.setVisible(true);

                System.out.println(optionPane.getValue());
            }
        });
    }

}