Java swing ActionEvent for button click -


@hovercraftfullofeels helped create code, what best way add button selects of ellipses , changes color inactive_color active_color.

my approach add button button panel , define action performed.
trying add functionality drawpanel2 class not accessible. possible best way this?

as always, other suggested improvements appreciated, thanks.

import javax.swing.*; import java.awt.*; import java.awt.event.actionevent; import java.awt.event.mouseadapter; import java.awt.event.mouseevent; import java.awt.geom.ellipse2d; import java.util.*; import java.util.list;  @suppresswarnings("serial") public class drawpanelmain extends jpanel {      private static final int pref_w = 700;     private static final int pref_h = pref_w;     private list<point> point_list = arrays.aslist(new point[] {             new point(40, 40),             new point(40, 100),             new point(40, 160),             new point(40, 220),             new point(40, 280),             new point(40, 340),             new point(40, 400),             new point(40, 460),             new point(40, 520),             new point(40, 580),             new point(100, 100),             new point(100, 160),             new point(100, 220),             new point(100, 280),             new point(100, 340),             new point(100, 400),             new point(100, 460),             new point(100, 520),             new point(100, 580),             new point(160, 160),             new point(160, 220),             new point(160, 280),             new point(160, 340),             new point(160, 400),             new point(160, 460),             new point(160, 520),             new point(160, 580),             new point(220, 220),             new point(220, 280),             new point(220, 340),             new point(220, 400),             new point(220, 460),             new point(220, 520),             new point(220, 580),             new point(280, 280),             new point(280, 340),             new point(280, 400),             new point(280, 460),             new point(280, 520),             new point(280, 580),             new point(340, 340),             new point(340, 400),             new point(340, 460),             new point(340, 520),             new point(340, 580),             new point(400, 400),             new point(400, 460),             new point(400, 520),             new point(400, 580),             new point(460, 460),             new point(460, 520),             new point(460, 580),             new point(520, 520),             new point(520, 580),             new point(580, 580)     });     private jtabbedpane tabbedpane = new jtabbedpane();     private int tabindex = 0;      public drawpanelmain() {         jpanel btnpanel = new jpanel();         btnpanel.add(new jbutton(new addswitchaction("add switch panel")));         btnpanel.add(new jbutton(new pushconfigaction("push config")));         btnpanel.add(new jbutton(new activateallaction("activate all")));  //button want work          setlayout(new borderlayout());         add(tabbedpane, borderlayout.center);         add(btnpanel, borderlayout.page_end);     }      @override     public dimension getpreferredsize() {         if (ispreferredsizeset()) {             return super.getpreferredsize();         }         return new dimension(pref_w, pref_h);     }      private class addswitchaction extends abstractaction {             public addswitchaction(string name) {                 super(name);                 int mnemonic = (int) name.charat(0);                 putvalue(mnemonic_key, mnemonic);             }          @override         public void actionperformed(actionevent e) {             tabindex++;             string title = "switch " + tabindex;             drawpanel2 tabcomponent = new drawpanel2(point_list);             tabbedpane.add(title, tabcomponent);         }     }      private class pushconfigaction extends abstractaction {         public pushconfigaction(string name) {             super(name);             int mnemonic = (int) name.charat(0);             putvalue(mnemonic_key, mnemonic);         }          @override         public void actionperformed(actionevent e) {             /*add code sending configuration switch panel*/             joptionpane.showmessagedialog(drawpanelmain.this, "configuration pushed panel");         }     }      //here code want work making button activate circles     private class activateallaction extends abstractaction {         public activateallaction(string name) {             super(name);             int mnemonic = (int) name.charat(1);             putvalue(mnemonic_key, mnemonic);         }          @override         public void actionperformed(actionevent e) {             //add code performing action here         }     }      private static void createandshowgui() {         drawpanelmain mainpanel = new drawpanelmain();         final double version = 0.1;         jframe frame = new jframe("rf connection panel " + version);         frame.setdefaultcloseoperation(jframe.dispose_on_close);         frame.getcontentpane().add(mainpanel);         frame.pack();         frame.setlocationbyplatform(true);         frame.setvisible(true);     }      public static void main(string[] args) {         swingutilities.invokelater(new runnable() {             public void run() {                 createandshowgui();             }         });     }  }  @suppresswarnings("serial") class drawpanel2 extends jpanel {     private static final int oval_width = 40;     private static final color inactive_color = color.red;     private static final color active_color = color.green;     private list<point> points;     private list<ellipse2d> ellipses = new arraylist<>();     private map<ellipse2d, color> ellipsecolormap = new hashmap<>();      public drawpanel2(list<point> points) {         this.points = points;         (point p : points) {             int x = p.x - oval_width / 2;             int y = p.y - oval_width / 2;             int w = oval_width;             int h = oval_width;             ellipse2d ellipse = new ellipse2d.double(x, y, w, h);             ellipses.add(ellipse);             ellipsecolormap.put(ellipse, inactive_color);         }          mymouseadapter mlistener = new mymouseadapter();         addmouselistener(mlistener);         addmousemotionlistener(mlistener);     }      @override     protected void paintcomponent(graphics g) {         super.paintcomponent(g);         graphics2d g2 = (graphics2d) g;         g2.setrenderinghint(renderinghints.key_antialiasing,                 renderinghints.value_antialias_on);         (ellipse2d ellipse : ellipses) {             g2.setcolor(ellipsecolormap.get(ellipse));             g2.fill(ellipse);         }     }      private class mymouseadapter extends mouseadapter {         @override         public void mousepressed(mouseevent e) {             (ellipse2d ellipse : ellipses) {                 if (ellipse.contains(e.getpoint())) {                     color c = ellipsecolormap.get(ellipse);                     c =  (c == inactive_color) ? active_color : inactive_color;                     ellipsecolormap.put(ellipse, c);                 }             }             repaint();         }     } } 

because can have multiple drawpanel2 instances , each 1 contained within it's own "tab", need find selected tab , extract drawpanel2 instance it, example

private class activateallaction extends abstractaction {      public activateallaction(string name) {         super(name);         int mnemonic = (int) name.charat(1);         putvalue(mnemonic_key, mnemonic);     }      @override     public void actionperformed(actionevent e) {         //add code performing action here         component comp = tabbedpane.getselectedcomponent();         if (comp instanceof drawpanel2) {             drawpanel2 drawpanel = (drawpanel2) comp;         }     } } 

this first checks see if selected tab component instance of drawpanel2, before casts it.

next, need way change state of ellipses...

to end, added activateall method drawpanel2 class, add 1 allows pass boolean sets the state instead

public void activateall() {     (ellipse2d ellipse : ellipses) {         ellipsecolormap.put(ellipse, active_color);     }     repaint(); } 

then action, called it

//... drawpanel2 drawpanel = (drawpanel2) comp; drawpanel.activateall(); 

Comments