Question / Possible idea for Swing GUI Intentions

Hello,

There are several patterns in Java Swing development.
Is there a way to generate this code automatically in IDEA, or are these good ideas for Intentions?

  • * Anonymous/Inner Adapter/Listener classes*

Swing uses callbacks to notify when a button is pressed, an item is selected, focus is lost, etc. One strategy is to have your GUI panel implement the listener class, but I don't like this because you have to advertise the callbacks as public methods of your class which pollutes your class interface. So, I usually just use anonymous classes. Here is some example code:

It would be nice to have an intention when the caret is at:

with choices like "Create inline anonymous class implementing ListSelectionListener" or "Create named anonymous class implementing ListSelectionListener". This could be shortened to just "Create inline ListSelectionListener" and "Create named ListSelectionListner".

  • Create inline ListSelectionListener *

  • Create named ListSelectionListener *


This intention could be applied to any class which has a constructor with Zero arguments. (A requirement of anonymous classes)

3 comments
Comment actions Permalink

But can't you just use the built in Implement methods assistant to stub out
all the methods for you?

--
Duane Fields
Deep Magic Software and Consulting
http://www.deepmagic.com


"Alex" <itnadmin@jetbrains.com> wrote in message
news:27068031.1054834818580.JavaMail.jrun@is.intellij.net...

Hello,

>

There are several patterns in Java Swing development.
Is there a way to generate this code automatically in IDEA, or are these

good ideas for Intentions?
>

  • * Anonymous/Inner Adapter/Listener classes*

Swing uses callbacks to notify when a button is pressed, an item is

selected, focus is lost, etc. One strategy is to have your GUI panel
implement the listener class, but I don't like this because you have to
advertise the callbacks as public methods of your class which pollutes your
class interface. So, I usually just use anonymous classes. Here is some
example code:

         mParentLM = new DefaultListModel();
>         mParentL = new JList(mParentLM);
>         mParentL.setBackground(new Color(238, 238, 238));
>         {
>             MouseListener ml = new MouseAdapter()
>             {
>                 public void mouseClicked(MouseEvent e) {
>                     if (!mParentL.isEnabled()) {
>                         return;
>                     }
>                     if (e.getClickCount() == 2) {
>                         int index =
mParentL.locationToIndex(e.getPoint());
>                         mParentL.setSelectedIndex(index);
>                         mGotoParentB.doClick();
>                     }
>                 }
>             };
>             mParentL.addMouseListener(ml);
>         }
>         mParentL.addListSelectionListener(
>                 new ListSelectionListener()
>                 {
>                     public void valueChanged(ListSelectionEvent evt) {
>                         if
(mParentL.getSelectionModel().isSelectionEmpty()) {
>                             mDeleteParentB.setEnabled(false);
>                             mGotoParentB.setEnabled(false);
>                         } else {
>                             if (mNode.getFlag() == Owner.USER) {
>                                 mDeleteParentB.setEnabled(true);
>                             }
>                             mGotoParentB.setEnabled(true);
>                         }
>                     }
>                 }
>         );
> ]]>

It would be nice to have an intention when the caret is at:

   mParentL.addListSelectionListener(|
> ]]>

with choices like "Create inline anonymous class implementing

ListSelectionListener" or "Create named anonymous class implementing
ListSelectionListener". This could be shortened to just "Create inline
ListSelectionListener" and "Create named ListSelectionListner".
>

  • Create inline ListSelectionListener *

   mParentL.addListSelectionListener(
>                 new ListSelectionListener()
>                 {
>                     public void valueChanged(ListSelectionEvent evt) {
>                     }
>                 });
> ]]>
  • Create named ListSelectionListener *

 ListSelectionListener listSelectionListener =
>   new ListSelectionListener() {
>                     public void valueChanged(ListSelectionEvent evt) {
>                     }
>                 };
>   mParentL.addListSelectionListener(listSelectionListener);
> ]]>

>

This intention could be applied to any class which has a constructor with

Zero arguments. (A requirement of anonymous classes)


0
Comment actions Permalink

Alex,

ChangeListener viewportListener = new |

Ctrl+Space

result in:

ChangeListener viewportListener = new ChangeListener() {
public void stateChanged(ChangeEvent e) {
}
};

then (if you need) Refactor->Convert Anonimous to Inner

ListSelectionListener listSelectionListener =
new ListSelectionListener() {
public void valueChanged(ListSelectionEvent evt) {
}
};
mParentL.addListSelectionListener(listSelectionListener);


--
Dmitry Skavish
-


Boston, MA, USA
tel. +1 781 910-3810
http://www.jzox.com
http://www.flashgap.com

0
Comment actions Permalink

Thanks! That is exactly what I wanted.

But I had to hit "new |<CTRLSHIFTSpace>" (Code->Complete Code->SmartType). If I just hit "new |<CTRL+Space>" it just pops up a list of all class names.
This is in IDEA 818.

-Alex

0

Please sign in to leave a comment.