OnJava8-Examples/gui/FileChooserTest.java

66 lines
1.9 KiB
Java
Raw Normal View History

2015-04-20 15:36:01 -07:00
//: gui/FileChooserTest.java
// Demonstration of File dialog boxes.
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import static net.mindview.util.SwingConsole.*;
public class FileChooserTest extends JFrame {
private JTextField
fileName = new JTextField(),
dir = new JTextField();
private JButton
open = new JButton("Open"),
save = new JButton("Save");
public FileChooserTest() {
JPanel p = new JPanel();
open.addActionListener(new OpenL());
p.add(open);
save.addActionListener(new SaveL());
p.add(save);
add(p, BorderLayout.SOUTH);
dir.setEditable(false);
fileName.setEditable(false);
p = new JPanel();
p.setLayout(new GridLayout(2,1));
p.add(fileName);
p.add(dir);
add(p, BorderLayout.NORTH);
}
class OpenL implements ActionListener {
2015-05-05 11:20:13 -07:00
@Override
2015-04-20 15:36:01 -07:00
public void actionPerformed(ActionEvent e) {
JFileChooser c = new JFileChooser();
// Demonstrate "Open" dialog:
int rVal = c.showOpenDialog(FileChooserTest.this);
if(rVal == JFileChooser.APPROVE_OPTION) {
fileName.setText(c.getSelectedFile().getName());
dir.setText(c.getCurrentDirectory().toString());
}
if(rVal == JFileChooser.CANCEL_OPTION) {
fileName.setText("You pressed cancel");
dir.setText("");
}
}
}
class SaveL implements ActionListener {
2015-05-05 11:20:13 -07:00
@Override
2015-04-20 15:36:01 -07:00
public void actionPerformed(ActionEvent e) {
JFileChooser c = new JFileChooser();
// Demonstrate "Save" dialog:
int rVal = c.showSaveDialog(FileChooserTest.this);
if(rVal == JFileChooser.APPROVE_OPTION) {
fileName.setText(c.getSelectedFile().getName());
dir.setText(c.getCurrentDirectory().toString());
}
if(rVal == JFileChooser.CANCEL_OPTION) {
fileName.setText("You pressed cancel");
dir.setText("");
}
}
}
public static void main(String[] args) {
run(new FileChooserTest(), 250, 150);
}
} ///:~