Reputation: 329
I have a JScrollPane
that has a filedrop feature to allow the dragging of files. I have the JScrollPane being called with a JOptionPanel.showConfirmDialog
so there are ok and cancel buttons. I want to be able to put my mouse on the edge of the window and drag to resize it. I have researched many different ways but most of them involve frames and none of them had JOptionPanes
in them. How can I do this or make a resizable panel with ok and cancel buttons like a showConfirmDialog
.
here is the function for my panels:
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;
}
Upvotes: 0
Views: 701
Reputation: 347334
Okay, I think I understand...
JOptionPane#initDialog
, which is a private
method
, sets the dialog
to non-resizable...which is nice of them...but you want a resizable window instead...
About the only choice you have is to make the dialog yourself, for example...
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());
}
});
}
}
Upvotes: 3
Reputation: 205875
As shown here, you can add a JOptionPane
to a JDialog
, which is a resizable, top-level container. A complete example is shown in this answer.
JDialog dialog = new JDialog();
JOptionPane optPane = new JOptionPane();
…
dialog.add(optPane);
Upvotes: 3