How can I put the html file in a browser into a file in JAVA swing?

I am trying to create my first UI page, although Swing. On this page I want to view the file. Can someone please help me achieve this?

+3
source share
4 answers

Check out this Sun tutorial page: http://java.sun.com/docs/books/tutorial/uiswing/components/filechooser.html .

The main implementation includes:

//Create a file chooser
final JFileChooser fc = new JFileChooser();
...
//In response to a button click:
int returnVal = fc.showOpenDialog(aComponent);

The return value gives you information about whether the user clicked "ok" or "cancel", etc., and then you can query the File Chooser object to find out which file was selected.

+7
+2

CodeExample, JFileChooser.

// This action creates and shows a modal open-file dialog.
    public class OpenFileAction extends AbstractAction {
        JFrame frame;
        JFileChooser chooser;

    OpenFileAction(JFrame frame, JFileChooser chooser) {
        super("Open...");
        this.chooser = chooser;
        this.frame = frame;
    }

    public void actionPerformed(ActionEvent evt) {
        // Show dialog; this method does not return until dialog is closed
        chooser.showOpenDialog(frame);

        // Get the selected file
        File file = chooser.getSelectedFile();
    }
};

// This action creates and shows a modal save-file dialog.
public class SaveFileAction extends AbstractAction {
    JFileChooser chooser;
    JFrame frame;

    SaveFileAction(JFrame frame, JFileChooser chooser) {
        super("Save As...");
        this.chooser = chooser;
        this.frame = frame;
    }

    public void actionPerformed(ActionEvent evt) {
        // Show dialog; this method does not return until dialog is closed
        chooser.showSaveDialog(frame);

        // Get the selected file
        File file = chooser.getSelectedFile();
    }
};
+1
source

Add actionListener button to button to open JFileChooser

0
source

All Articles