import java.awt.*; import java.awt.event.*; import java.io.FileNotFoundException; import java.io.File; import javax.swing.*; public class FilViser { static JLabel label = new JLabel("Velg en fil: "); public static void main(String[] args) { JFrame vindu = new JFrame("FilLeser"); vindu.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); JPanel panel = new JPanel(); vindu.add(panel); panel.add(label); class VelgFilAction implements ActionListener { @Override public void actionPerformed(ActionEvent e) { JFileChooser velger = new JFileChooser(); File workingDir = new File(System.getProperty("user.dir")); velger.setCurrentDirectory(workingDir); int resultat = velger.showOpenDialog(null); if (resultat == JFileChooser.APPROVE_OPTION) { try { FilLeser leser = new FilLeser(velger.getSelectedFile()); label.setText(leser.hentStreng()); } catch (FileNotFoundException err) {err.printStackTrace();} } } } JButton velgFilKnapp = new JButton("Velg fil"); velgFilKnapp.addActionListener(new VelgFilAction()); panel.add(velgFilKnapp); // DETTE SKJER TIL SLUTT vindu.pack(); vindu.setVisible(true); } }