java - Is it possible that JButton and actionPerformed method are in different files? -


is possible have button on 1 file , actionperformed(actionevent e) method in different file? trying add actionlistener button, choose1 in file trialdump.java actionperformed(actionevent e) method in file listen.java. tried extending public class trialdump extends listen shows error. idea how can add method button? thanks.

here code in file trialdump.java:

package core;  import javax.swing.grouplayout; import javax.swing.jbutton; import javax.swing.jframe; import javax.swing.jlabel; import javax.swing.jpanel; import javax.swing.jtextarea; import javax.swing.jtextfield; import javax.swing.swingconstants; import javax.swing.*; import java.awt.*;  // create simple gui window public class trialdump {  private static void createwindow() {      // create , set window.     jframe frame = new jframe("pdf denoiser");     frame.setdefaultcloseoperation(jframe.exit_on_close);      // edit     jpanel panel = new jpanel();     grouplayout layout = new grouplayout(panel);     panel.setlayout(layout);      layout.setautocreategaps(true);     layout.setautocreatecontainergaps(true);      jlabel label1 = new jlabel("image file");     jlabel label2 = new jlabel("destination");     jlabel label3 = new jlabel("preview");      jtextfield current = new jtextfield();     jtextfield dest = new jtextfield();     jtextarea preview = new jtextarea();      preview.seteditable(false);     jscrollpane previewscrollpane = new jscrollpane(preview);      jbutton choose1 = new jbutton("search1");     jbutton choose2 = new jbutton("search2");     jbutton algo1 = new jbutton("mdwm");     jbutton algo2 = new jbutton("bfmr");     jbutton algo3 = new jbutton("mine");        // horizontal arrangement     layout.sethorizontalgroup(layout             .createsequentialgroup()             .addgroup(                     layout.createparallelgroup(grouplayout.alignment.leading).addcomponent(label1)                             .addcomponent(label2).addcomponent(label3))             .addgroup(                     layout.createparallelgroup(grouplayout.alignment.leading).addcomponent(current)                             .addcomponent(dest).addcomponent(preview))             .addgroup(                     layout.createparallelgroup(grouplayout.alignment.leading).addcomponent(choose1)                             .addcomponent(choose2).addcomponent(algo1).addcomponent(algo2).addcomponent(algo3)));      layout.linksize(swingconstants.horizontal, choose1, choose2, algo1, algo2, algo3);      // vertical arrangement     layout.setverticalgroup(layout             .createsequentialgroup()             .addgroup(                     layout.createparallelgroup(grouplayout.alignment.baseline).addcomponent(label1)                             .addcomponent(current).addcomponent(choose1))             .addgroup(                     layout.createparallelgroup(grouplayout.alignment.baseline).addcomponent(label2)                             .addcomponent(dest).addcomponent(choose2))             .addgroup(                     layout.createparallelgroup(grouplayout.alignment.leading)                             .addcomponent(label3)                             .addcomponent(preview)                             .addgroup(                                     layout.createsequentialgroup().addcomponent(algo1).addcomponent(algo2)                                             .addcomponent(algo3))));      // display window.     frame.setlocationrelativeto(null);     frame.add(panel);     frame.pack();     frame.setvisible(true); }  public static void main(string[] args) {      createwindow();  } } 

and here code in listen.java:

package components;  import java.io.*; import java.awt.*; import java.awt.event.*; import javax.swing.*; import javax.swing.swingutilities; import javax.swing.filechooser.*;  public class listen extends jpanel implements actionlistener{  static private string newline = "\n"; private jtextarea log; private jfilechooser fc;  public listen() {    }  public void actionperformed(actionevent e) {     //set file chooser.     if (fc == null) {         fc = new jfilechooser();      //add custom file filter , disable default     //(accept all) file filter.         fc.addchoosablefilefilter(new imagefilter());         fc.setacceptallfilefilterused(false);      //add custom icons file types.         fc.setfileview(new imagefileview());      //add preview pane.         fc.setaccessory(new imagepreview(fc));     }      //show it.     int returnval = fc.showdialog(listen.this,                                   "attach");      //process results.     if (returnval == jfilechooser.approve_option) {         file file = fc.getselectedfile();      } else {         log.append("attachment cancelled user." + newline);     }     log.setcaretposition(log.getdocument().getlength());      //reset file chooser next time it's shown.     fc.setselectedfile(null); }  /**  * create gui , show it.  thread safety,  * method should invoked  * event dispatch thread.  */   public static void main(string[] args) {     //schedule job event dispatch thread:     //creating , showing application's gui.     swingutilities.invokelater(new runnable() {         public void run() {             //turn off metal's use of bold fonts             uimanager.put("swing.boldmetal", boolean.false);          }     }); } } 

you can add listener defined in class doing this

jbutton choose1 = new jbutton("search1"); choose1.addactionlistener(new listen()); 

btw, should take care of more bits of code:

  • classs names should typed in camelcase first letter capital
  • use proper names packages
  • i don't think main method in listener class making usefull
  • there not need @ extend jpanel in listener class
  • try avoid make empty constructor, doing in listen(). @ least call super(), or just, don't include it.

Comments

Popular posts from this blog

android - getbluetoothservice() called with no bluetoothmanagercallback -

sql - ASP.NET SqlDataSource, like on SelectCommand -

ios - Undefined symbols for architecture armv7: "_OBJC_CLASS_$_SSZipArchive" -