# Example from www.tutorialspoint.com (java)

**URL:** <https://ifnotnil.com/t/example-from-www-tutorialspoint-com-java/3847>\
**Category:** Java\
**Created:** [21 February 2024 11:51 UTC](https://ifnotnil.com/t/example-from-www-tutorialspoint-com-java/3847 "2024-02-21T11:51:45Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![Emile](https://ifnotnil.com/letter_avatar_proxy/v4/letter/e/58956e/32.png) [@Emile](https://ifnotnil.com/u/Emile)\
**Post date:** [21 February 2024 11:51 UTC](https://ifnotnil.com/t/example-from-www-tutorialspoint-com-java/3847/1 "2024-02-21T11:51:45Z")

</div>

The example (below) gives an error (all other Itested from that site too, same error).  
Link: [Show Open File Dialog](https://www.tutorialspoint.com/swingexamples/show_open_file_dialog.htm)

Using the terminal.  
I Pasted the code in TextEdit, saved it as SwingTester.java  
Set cd in Terminal to point to the .java folder (checked with ls -l),  
javac SwingTester

and get the error.

```auto
error: Class names, 'SwingTester', are only accepted if annotation processing is explicitly requested
1 error

```

idea(s) ?

```auto
import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.LayoutManager;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;

import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;

public class SwingTester {
   public static void main(String[] args) {
      createWindow();
   }

   private static void createWindow() {    
      JFrame frame = new JFrame("Swing Tester");
      frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      createUI(frame);
      frame.setSize(560, 200);      
      frame.setLocationRelativeTo(null);  
      frame.setVisible(true);
   }

   private static void createUI(final JFrame frame){  
      JPanel panel = new JPanel();
      LayoutManager layout = new FlowLayout();  
      panel.setLayout(layout);       

      JButton button = new JButton("Click Me!");
      final JLabel label = new JLabel();

      button.addActionListener(new ActionListener() {
         @Override
         public void actionPerformed(ActionEvent e) {
            JFileChooser fileChooser = new JFileChooser();
            int option = fileChooser.showOpenDialog(frame);
            if(option == JFileChooser.APPROVE_OPTION){
               File file = fileChooser.getSelectedFile();
               label.setText("File Selected: " + file.getName());
            }else{
               label.setText("Open command canceled");
            }
         }
      });

      panel.add(button);
      panel.add(label);
      frame.getContentPane().add(panel, BorderLayout.CENTER);    
   }  
}

```

---

<div class="post-metadata">

**Author:** ![Emile](https://ifnotnil.com/letter_avatar_proxy/v4/letter/e/58956e/32.png) [@Emile](https://ifnotnil.com/u/Emile)\
**Post date:** [21 February 2024 12:01 UTC](https://ifnotnil.com/t/example-from-www-tutorialspoint-com-java/3847/2 "2024-02-21T12:01:26Z")

</div>

Forget the question, my fault. The correct commands are;

```auto
 javac SwingTester.java
 java SwingTester

```

I forgot to check my reminder and asked in the wrong order.

Now I have to find how to generate the application (instead of running it in the Terminal).

Once I choosed an image file I get:

 ![image](https://ifnotnil.com/uploads/default/original/2X/7/7cf8736724a19d2198a0c424cc1a6837edbf40ed.jpeg)

---

<div class="post-metadata">

**Author:** ![thorstenstueker](https://ifnotnil.com/letter_avatar_proxy/v4/letter/t/aca169/32.png) [@thorstenstueker](https://ifnotnil.com/u/thorstenstueker)\
**Post date:** [21 February 2024 15:33 UTC](https://ifnotnil.com/t/example-from-www-tutorialspoint-com-java/3847/3 "2024-02-21T15:33:22Z")

</div>

> [@Emile](#):
>
> ```auto
> public static void main(String[] args) {
> createWindow();
> }
> 
> private static void createWindow() {    
> JFrame frame = new JFrame("Swing Tester");
> frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
> createUI(frame);
> frame.setSize(560, 200);      
> frame.setLocationRelativeTo(null);  
> frame.setVisible(true);
> }
> 
> private static void createUI(final JFrame frame){  
> JPanel panel = new JPanel();
> LayoutManager layout = new FlowLayout();  
> panel.setLayout(layout);       
> 
> JButton button = new JButton("Click Me!");
> final JLabel label = new JLabel();
> 
> button.addActionListener(new ActionListener() {
> @Override
> public void actionPerformed(ActionEvent e) {
> JFileChooser fileChooser = new JFileChooser();
> int option = fileChooser.showOpenDialog(frame);
> if(option == JFileChooser.APPROVE_OPTION){
> File file = fileChooser.getSelectedFile();
> label.setText("File Selected: " + file.getName());
> }else{
> label.setText("Open command canceled");
> }
> }
> });
> 
> panel.add(button);
> panel.add(label);
> frame.getContentPane().add(panel, BorderLayout.CENTER);    
> }  
> 
> ```

Simple question: why not with the Netbeans IDE and the Screen Designer but hand coded what is a bit harder to do? Only as a mention. The example works without any problem like you see.

---

<div class="post-metadata">

**Author:** ![Emile](https://ifnotnil.com/letter_avatar_proxy/v4/letter/e/58956e/32.png) [@Emile](https://ifnotnil.com/u/Emile)\
**Post date:** [22 February 2024 08:16 UTC](https://ifnotnil.com/t/example-from-www-tutorialspoint-com-java/3847/4 "2024-02-22T08:16:46Z")

</div>

Because there is no MacOS (nor Apple arm) version available ?

> **[Downloading Apache NetBeans 21](https://netbeans.apache.org/front/main/download/nb21/)**
>
> Apache NetBeans 21 Download

Google propose me to downlad version 12.2 as dmg.

MacBook Pro 13" m1  
Sonoma 14.3.1

java -version  
java version “21.0.2” 2024-01-16 LTS  
Java™ SE Runtime Environment (build 21.0.2+13-LTS-58)  
Java HotSpot™ 64-Bit Server VM (build 21.0.2+13-LTS-58, mixed mode, sharing)

---

<div class="post-metadata">

**Author:** ![thorstenstueker](https://ifnotnil.com/letter_avatar_proxy/v4/letter/t/aca169/32.png) [@thorstenstueker](https://ifnotnil.com/u/thorstenstueker)\
**Post date:** [22 February 2024 10:33 UTC](https://ifnotnil.com/t/example-from-www-tutorialspoint-com-java/3847/5 "2024-02-22T10:33:01Z")

</div>

Take the platform independent one. And install java 21 idk from Azul. The platform installers will come the next few days also.

---

<div class="post-metadata">

**Author:** ![Emile](https://ifnotnil.com/letter_avatar_proxy/v4/letter/e/58956e/32.png) [@Emile](https://ifnotnil.com/u/Emile)\
**Post date:** [22 February 2024 11:08 UTC](https://ifnotnil.com/t/example-from-www-tutorialspoint-com-java/3847/6 "2024-02-22T11:08:45Z")

</div>

Thank you for your answer Thorsten. As you can see, things are not so easy :(.

Netbeans v 20 nor 19 are not available for MacOS…

I know about Abdul (never heard about abdul, nor idk).

And, yes, after waiting so many years, I can wait for few days…

---

<div class="post-metadata">

**Author:** ![Emile](https://ifnotnil.com/letter_avatar_proxy/v4/letter/e/58956e/32.png) [@Emile](https://ifnotnil.com/u/Emile)\
**Post date:** [22 February 2024 11:46 UTC](https://ifnotnil.com/t/example-from-www-tutorialspoint-com-java/3847/7 "2024-02-22T11:46:48Z")

</div>

azul (zulu) installed:

java -version

```auto
openjdk version "21.0.2" 2024-01-16 LTS
OpenJDK Runtime Environment Zulu21.32+17-CA (build 21.0.2+13-LTS)
OpenJDK 64-Bit Server VM Zulu21.32+17-CA (build 21.0.2+13-LTS, mixed mode, sharing)

```

But no man page for java… in the Terminal.

---

<div class="post-metadata">

**Author:** ![thorstenstueker](https://ifnotnil.com/letter_avatar_proxy/v4/letter/t/aca169/32.png) [@thorstenstueker](https://ifnotnil.com/u/thorstenstueker)\
**Post date:** [22 February 2024 14:04 UTC](https://ifnotnil.com/t/example-from-www-tutorialspoint-com-java/3847/8 "2024-02-22T14:04:16Z")

</div>

java --help  
will bring you the commends  
but for what do you need them for Develöopment?

Normally you can start the IDE and set the IDE settings that you will get the command descriptions all inside the IDE. I do not need any man page when working with IDE.

By the way, pkg is now available for macos install. Just downloaded it and installed without any problem. Runs.

Noe you can start with Java with ant project as a Java application. There y\<ou can add a JFrame which you can edit via drag and drop

---

<div class="post-metadata">

**Author:** ![Emile](https://ifnotnil.com/letter_avatar_proxy/v4/letter/e/58956e/32.png) [@Emile](https://ifnotnil.com/u/Emile)\
**Post date:** [26 February 2024 07:50 UTC](https://ifnotnil.com/t/example-from-www-tutorialspoint-com-java/3847/9 "2024-02-26T07:50:00Z")

</div>

Hi Thorsten,

I downloaded the pkg…

The stupid guys do not used **MacOS** , it is a pure hazard that I understand pkg download is for me !

Yes, for development.

I fired it and it looks nice; I do nothing with it (lack of time, I only wanted to be sure it does not asked me to download some more thing “à la Microsoft”…).

Regards,

Emile

---

<div class="post-metadata">

**Author:** ![thorstenstueker](https://ifnotnil.com/letter_avatar_proxy/v4/letter/t/aca169/32.png) [@thorstenstueker](https://ifnotnil.com/u/thorstenstueker)\
**Post date:** [26 February 2024 08:01 UTC](https://ifnotnil.com/t/example-from-www-tutorialspoint-com-java/3847/10 "2024-02-26T08:01:51Z")

</div>

Na it needs no downloads or something. When and if you want to use for example Libs via Maven they gert loaded from mavencentral. Only if you want to. And yes, it fires and makes no problems at all.
