0

fileItem1is a JMenuItem when i click on fileItem1, this is how you would make it open a file and then just display the name of that file in the JFrame:

// open file
fileItem1.addActionListener(new ActionListener() {
    public void actionPerformed(ActionEvent e) {
        JFileChooser chooser = new JFileChooser();
        Component parent = null;
        int returnVal = chooser.showOpenDialog(parent);
        if(returnVal == JFileChooser.APPROVE_OPTION) {
               System.out.println("You chose to open this file: " + chooser.getSelectedFile().getName());
            }               
            jStyledTextPane.setText("You chose to open this file: " + chooser.getSelectedFile().getName());
        }
    });
tshepang
  • 12,111
  • 21
  • 91
  • 136
user2145688
  • 11
  • 1
  • 1
  • 7
  • 1
    What about your last question? OK, you have deleted it, but before you told you want to try this on your own with the help of the link I've posted... You should really spend some effort instead of using SO as a code generator. – Kai Mar 19 '13 at 11:53
  • i have, sorry i was struggling a bit but i managed well and this is the code for anyone who is in need of in the future. – user2145688 Mar 19 '13 at 11:59

2 Answers2

1
fileItem1.addActionListener(new ActionListener() {
    @Override
    public void actionPerformed(ActionEvent event) {
    JFileChooser fc = new JFileChooser();

    int returnVal = fc.showOpenDialog(YourClassName.this);

    if (returnVal == JFileChooser.APPROVE_OPTION) {
        File file = fc.getSelectedFile();
        filePath = file.getAbsolutePath();
        try {
        //your write to Jframe method
        } catch (FileNotFoundException e) {
        Logger.getLogger(YourClassName.class.getName()).log(
            Level.SEVERE, null, e);
        }

      }
    }
});
Cugomastik
  • 911
  • 13
  • 22
0

The Oracle example is pretty good in my opinion: http://docs.oracle.com/javase/tutorial/uiswing/components/filechooser.html

Here is the implementation:

    int returnVal = fc.showOpenDialog(FileChooserDemo.this);

    if (returnVal == JFileChooser.APPROVE_OPTION) {
        File file = fc.getSelectedFile();
        //This is where a real application would open the file.
        log.append("Opening: " + file.getName() + "." + newline);
    } else {
        log.append("Open command cancelled by user." + newline);
    }
oblivion19
  • 308
  • 2
  • 5
  • 14