/* * * Copyright (c) 2007, 2021, Oracle and/or its affiliates. All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * * - Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * * - Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * * - Neither the name of Oracle nor the names of its * contributors may be used to endorse or promote products derived * from this software without specific prior written permission. * * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
/** * main method allows us to run as a standalone demo.
*/ publicstaticvoid main(String[] args) {
FileChooserDemo demo = new FileChooserDemo(null);
demo.mainImpl();
}
/** * FileChooserDemo Constructor
*/ public FileChooserDemo(SwingSet2 swingset) { // Set the title for this demo, and an icon used to represent this // demo inside the SwingSet2 app. super(swingset, "FileChooserDemo", "toolbar/JFileChooser.gif");
createFileChooserDemo();
}
// Create a panel to hold buttons
JPanel buttonPanel = new JPanel() { public Dimension getMaximumSize() { returnnew Dimension(getPreferredSize().width, super.getMaximumSize().height);
}
};
buttonPanel.setLayout(new BoxLayout(buttonPanel, BoxLayout.Y_AXIS));
// Create a panel to hold the image
JPanel imagePanel = new JPanel();
imagePanel.setLayout(new BorderLayout());
imagePanel.setBorder(new BevelBorder(BevelBorder.LOWERED));
JScrollPane scroller = new JScrollPane(theImage);
scroller.getVerticalScrollBar().setUnitIncrement(10);
scroller.getHorizontalScrollBar().setUnitIncrement(10);
imagePanel.add(scroller, BorderLayout.CENTER);
// add buttons and image panels to inner panel
innerPanel.add(buttonPanel);
innerPanel.add(Box.createRigidArea(HGAP30));
innerPanel.add(imagePanel);
innerPanel.add(Box.createRigidArea(HGAP20));
}
public JFileChooser createFileChooser() { // create a filechooser
JFileChooser fc = new JFileChooser(); if (getSwingSet2() != null && getSwingSet2().isDragEnabled()) {
fc.setDragEnabled(true);
}
// set the current directory to be the images directory
File swingFile = new File("resources/images/About.jpg"); if(swingFile.exists()) {
fc.setCurrentDirectory(swingFile);
fc.setSelectedFile(swingFile);
}
return fc;
}
public JButton createPlainFileChooserButton() {
Action a = new AbstractAction(getString("FileChooserDemo.plainbutton")) { publicvoid actionPerformed(ActionEvent e) {
JFileChooser fc = createFileChooser();
// show the filechooser int result = fc.showOpenDialog(getDemoPanel());
// if we selected an image, load the image if(result == JFileChooser.APPROVE_OPTION) {
loadImage(fc.getSelectedFile().getPath());
}
}
}; return createButton(a);
}
public JButton createPreviewFileChooserButton() {
Action a = new AbstractAction(getString("FileChooserDemo.previewbutton")) { publicvoid actionPerformed(ActionEvent e) {
JFileChooser fc = createFileChooser();
// show the filechooser int result = fc.showOpenDialog(getDemoPanel());
// if we selected an image, load the image if(result == JFileChooser.APPROVE_OPTION) {
loadImage(fc.getSelectedFile().getPath());
}
}
}; return createButton(a);
}
private String createFileNameFilterDescriptionFromExtensions(
String description, String[] extensions) {
String fullDescription = (description == null) ? "(" : description + " ("; // build the description from the extension list
fullDescription += "." + extensions[0]; for (int i = 1; i < extensions.length; i++) {
fullDescription += ", .";
fullDescription += extensions[i];
}
fullDescription += ")"; return fullDescription;
}
public JButton createCustomFileChooserButton() {
Action a = new AbstractAction(getString("FileChooserDemo.custombutton")) { publicvoid actionPerformed(ActionEvent e) {
fc = createFileChooser();
public JButton createButton(Action a) {
JButton b = new JButton(a) { public Dimension getMaximumSize() { int width = Short.MAX_VALUE; int height = super.getMaximumSize().height; returnnew Dimension(width, height);
}
}; return b;
}
public JButton createImageButton(Action a) {
JButton b = new JButton(a);
b.setMargin(new Insets(0,0,0,0)); return b;
}
}
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung ist noch experimentell.