What method is used to make the open button in FileDialog work?

Sparsh Parnami
JMenuBar menubar = new JMenuBar();
JMenu file = new JMenu("File");  
add(menubar,BorderLayout.NORTH); 
menubar.add(file);              
JMenuItem Open = new JMenuItem("OPEN...     Ctrl+O");
file.add(Open);
Open.addActionListener(new ActionListener() {
    public void actionPerformed(ActionEvent event) { 
        Frame f = new Frame();
        FileDialog openf = new FileDialog(f, "Open");
        openf.setVisible(true);
    }
});

Well, I tried to use a lot many examples on the internet which makes the open button work as you can see i have already made the design but i need help in how to open a .txt file on clicking the open button in the filedialog. how am i supposed to do that?? i would really appreciate if anyone can help me out with few lines of code that actually works as i am sick of searching error generating codes from the internet.

enter image description here

Simon Forsberg

The documentation states:

The FileDialog class displays a dialog window from which the user can select a file.

Since it is a modal dialog, when the application calls its show method to display the dialog, it blocks the rest of the application until the user has chosen a file.

Therefore, instead of calling .setVisible(true) you can call .show() on the dialog, and then you can use getFile() to get the file that was chosen, or getFiles() if you are using multipleMode.

To read the file you can use:

public static String readFile(String path, Charset encoding) throws IOException {
  byte[] encoded = Files.readAllBytes(Paths.get(path));
  return encoding.decode(ByteBuffer.wrap(encoded)).toString();
}

yourComponent.setText(readFile(openf.getFile(), Charset.defaultCharset()));

(Taken from this question)

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

What Scala concepts are being used to make Scalatra DSL work?

From Dev

open a fileDialog in visio vba

From Dev

Make button open link - Swift

From Dev

What is button property "tooltip" used for?

From Dev

What is button property "tooltip" used for?

From Dev

What is the WebRTC method reattachMediaStream used for?

From Dev

What is the "Input Method" application used for?

From Dev

What is the method ".getChildren()" used for in Java?

From Dev

Open a workbook using FileDialog and manipulate it

From Dev

Mock open() function used in a class method

From Dev

My button used to work -- but now it doesn't

From Dev

how to make a method work - Javascript

From Dev

How to make work SqlQuery<> method?

From Dev

How does Dropbox Open button work?

From Dev

How to make a texField open with click of a button?

From Dev

How to make a button function as both open and close?

From Dev

Make the button disappear when the menu is open

From Dev

Android Studio: Make a Button open Next Activity

From Dev

What points to be used for Drawing Hexagonal Shaped Button

From Dev

What are work areas used for with the scrum methodology

From Dev

What is Cometd ? Why it is used and how to work on that

From Dev

What software was used to make the Software Center mockups?

From Dev

What version of GCC is used by the make command?

From Dev

What theme was used to make the Jekyll homepage

From Dev

Javascript button to open a modal; button doesn't work when in a list

From Dev

What is the first semicolon in `addCommandAlias` method used for in SBT?

From Dev

What method is used for textual representation of the data in AngularJS?

From Dev

What is the existingValue parameter used for in the ReadJson method of a JsonConverter?

From Dev

What is the use of this public async method? how it is used?

Related Related

  1. 1

    What Scala concepts are being used to make Scalatra DSL work?

  2. 2

    open a fileDialog in visio vba

  3. 3

    Make button open link - Swift

  4. 4

    What is button property "tooltip" used for?

  5. 5

    What is button property "tooltip" used for?

  6. 6

    What is the WebRTC method reattachMediaStream used for?

  7. 7

    What is the "Input Method" application used for?

  8. 8

    What is the method ".getChildren()" used for in Java?

  9. 9

    Open a workbook using FileDialog and manipulate it

  10. 10

    Mock open() function used in a class method

  11. 11

    My button used to work -- but now it doesn't

  12. 12

    how to make a method work - Javascript

  13. 13

    How to make work SqlQuery<> method?

  14. 14

    How does Dropbox Open button work?

  15. 15

    How to make a texField open with click of a button?

  16. 16

    How to make a button function as both open and close?

  17. 17

    Make the button disappear when the menu is open

  18. 18

    Android Studio: Make a Button open Next Activity

  19. 19

    What points to be used for Drawing Hexagonal Shaped Button

  20. 20

    What are work areas used for with the scrum methodology

  21. 21

    What is Cometd ? Why it is used and how to work on that

  22. 22

    What software was used to make the Software Center mockups?

  23. 23

    What version of GCC is used by the make command?

  24. 24

    What theme was used to make the Jekyll homepage

  25. 25

    Javascript button to open a modal; button doesn't work when in a list

  26. 26

    What is the first semicolon in `addCommandAlias` method used for in SBT?

  27. 27

    What method is used for textual representation of the data in AngularJS?

  28. 28

    What is the existingValue parameter used for in the ReadJson method of a JsonConverter?

  29. 29

    What is the use of this public async method? how it is used?

HotTag

Archive