0

I am completely new to doing UI stuff with java. I read through some tutorials and wrote myself a mouse listener like this:

 import java.awt.event.MouseEvent;
 import java.awt.event.MouseAdapter;
 ...
    
 class MyPanel extends JPanel { 
     public MyPanel() {
     ...

        addMouseListener(new MouseAdapter() {
            public void mousePressed(MouseEvent e) {
                do some stuff
            }
        });
     ...
     }
}

As I understand it this mouse listener uses AWT, but it would be better if I used Swing. I tried changing it to something like what is shown here , but what confuses me about it is that apparently they import some AWT classes. Does that mean Swing uses AWT components and if yes, how do I know I am "Using swing instead of AWT" ?

krise
  • 485
  • 1
  • 10
  • 22
  • 2
    See [here](https://stackoverflow.com/questions/408820/what-is-the-difference-between-swing-and-awt). TL;DR: Swing is basically built on top of AWT. – maloomeister Aug 19 '20 at 08:01
  • 2
    Swing uses awt components. They only changed those for which they actually changed the code. But Swing is already a pretty old technology. Maybe if you are starting, it wouldn't hurt to look at newer front-end technologies. – Stultuske Aug 19 '20 at 08:04
  • @Stultuske like what? I know Angular a little but I always thought you mainly use that for developing websites, and I am trying to create a desktop app. – krise Aug 19 '20 at 09:20
  • javaFX, for instance – Stultuske Aug 19 '20 at 09:53
  • 2
    @Stultuske Java is also “a pretty old technology”, even older than Swing. So what? – Holger Aug 19 '20 at 17:07
  • 3
    Instead of adding a mouse listener, you should consider using a semantic listener. E.g. use a `JButton` and add an `ActionListener` or use a `JSlider` and add a `ChangeListener`, etc. Only use a mouse listener, when there is no builtin event for your intended semantic. – Holger Aug 19 '20 at 17:09
  • @Holger Java as language/syntax is being maintained and renewed. Improvements are being made to it, and new functionalities are being added. That is not the case for Swing. There's your "what". – Stultuske Aug 20 '20 at 06:54
  • 2
    @Stultuske Swing is still maintained. Like with 99% of the core library, there’s no dramatic makeover every release. Swing is part of Java. JavaFX has been removed from the core libraries. It’s a 3rd party library now. – Holger Aug 20 '20 at 07:01
  • @Holger I am using this mouse listener to detect when the user clicks some drawn shapes inside my jpannel. Is there an alternative or should I use a mouselistener in that case? – krise Aug 20 '20 at 19:09
  • 1
    For hit testing with non-rectangular shapes, processing mouse events is the straight-forward approach. When you’re creating a subclass of a component class, you don’t need to register listeners on yourself though. You may invoke [`super.enableEvents(AWTEvent.MOUSE_EVENT_MASK)`](https://docs.oracle.com/en/java/javase/14/docs/api/java.desktop/java/awt/Component.html#enableEvents%28long%29) in the constructor and override [`processMouseEvent(MouseEvent)`](https://docs.oracle.com/en/java/javase/14/docs/api/java.desktop/java/awt/Component.html#processMouseEvent%28java.awt.event.MouseEvent%29). – Holger Aug 21 '20 at 06:57

0 Answers0