java - Opening a new JFrame from a Button -
i want open new jframe clicking button (btnadd); have tried create actionlistener having no luck; code runs nothing happens when button clicked. methods in question last 2 in following code. appreciated!
package advancedweatherapp; import java.awt.borderlayout; import java.awt.color; import java.awt.component; import java.awt.container; import java.awt.dimension; import java.awt.flowlayout; import java.awt.font; import java.awt.toolkit; import java.awt.event.actionevent; import java.awt.event.actionlistener; import javax.swing.boxlayout; import javax.swing.jframe; import javax.swing.jpanel; import javax.swing.swingconstants; import javax.swing.event.listselectionlistener; import weatherforecast.fetchweatherforecast; public class mainframe extends jframe implements listselectionlistener { private boolean initialized = false; private actions actions = new actions(); private javax.swing.jscrollpane jspfavouriteslist = new javax.swing.jscrollpane(); private javax.swing.defaultlistmodel<string> listmodel = new javax.swing.defaultlistmodel<string>(); private javax.swing.jlist<string> favouriteslist = new javax.swing.jlist<string>( listmodel); private javax.swing.jlabel lblacknowledgement = new javax.swing.jlabel(); private javax.swing.jlabel lbltitle = new javax.swing.jlabel(); private javax.swing.jbutton btnadd = new javax.swing.jbutton(); private javax.swing.jbutton btnremove = new javax.swing.jbutton(); public void initialize() { initializegui(); initializeevents(); this.setdefaultcloseoperation(jframe.dispose_on_close); } /** * */ private void initializegui() { if (initialized) return; initialized = true; this.setsize(500, 400); dimension windowsize = this.getsize(); dimension screensize = toolkit.getdefaulttoolkit().getscreensize(); this.setlocation(screensize.width / 2 - windowsize.width / 2, screensize.height / 2 - windowsize.height / 2); container pane = this.getcontentpane(); pane.setlayout(new borderlayout()); setlayout(new borderlayout()); settitle("favourite weather locations"); jpanel jpsouth = new jpanel(); jpsouth.setlayout(new flowlayout()); jpanel jpnorth = new jpanel(); jpnorth.setlayout(new flowlayout()); jpanel jpcenter = new jpanel(); jpcenter.setlayout(new boxlayout(jpcenter, boxlayout.page_axis)); jpanel jpeast = new jpanel(); jpanel jpwest = new jpanel(); getcontentpane().setbackground(color.white); jpeast.setbackground(color.white); jpwest.setbackground(color.white); jpcenter.setbackground(color.white); getcontentpane().add(jspfavouriteslist); jpcenter.add(jspfavouriteslist); jspfavouriteslist.setviewportview(favouriteslist); favouriteslist .setselectionmode(javax.swing.listselectionmodel.single_selection); favouriteslist.addlistselectionlistener(this); jpcenter.add(btnadd); jpcenter.add(btnremove); jpcenter.setalignmenty(center_alignment); btnadd.settext("add location"); btnadd.setalignmentx(component.center_alignment); btnadd.setfont(new font("calibri", font.plain, 18)); jpcenter.add(btnremove); btnremove.settext("remove location"); btnremove.setalignmentx(component.center_alignment); btnremove.setfont(new font("calibri", font.plain, 18)); getcontentpane().add(jpeast, borderlayout.east); getcontentpane().add(jpwest, borderlayout.west); getcontentpane().add(jpsouth); jpsouth.add(lblacknowledgement); add(lblacknowledgement, borderlayout.south); lblacknowledgement.settext(fetchweatherforecast.getacknowledgement()); lblacknowledgement.sethorizontalalignment(swingconstants.center); lblacknowledgement.setfont(new font("tahoma", font.italic, 12)); getcontentpane().add(jpnorth); jpnorth.add(lbltitle); add(lbltitle, borderlayout.north); lbltitle.settext("your favourite locations"); lbltitle.sethorizontalalignment(swingconstants.center); lbltitle.setfont(new font("calibri", font.plain, 32)); lbltitle.setforeground(color.dark_gray); getcontentpane().add(jpcenter); } private void initializeevents() { // todo: add action listeners, etc } public class actions implements actionlistener { public void actionperformed(actionevent e) { string command = e.getactioncommand(); command = command == null ? "" : command; // todo: add if...if else... action commands } } public void dispose() { // todo: save settings // super.dispose(); system.exit(0); } public void setvisible(boolean b) { initialize(); super.setvisible(b); } public static void main(string[] args) { new mainframe().setvisible(true); } public void actionperformed(actionevent evt){ if (evt.getsource() == btnadd) { shownewframe(); //open search window } } private void shownewframe() { jframe frame = new jframe("search window" ); frame.setsize( 500,120 ); frame.setlocationrelativeto( null ); frame.setvisible( true ); } }
although have implemented actionperformed
method per actionlistener interface, class not of that type haven't implemented interface. once implement interface , register jbutton
btnadd
,
btnadd.addactionlistener(this);
the method called.
a more compact alternative might use anonymous interface:
btnadd.addactionlistener(new actionlistener() { @override public void actionperformed(actionevent e) { // handle button actionevent & display dialog... } });
side notes:
- using more 1
jframe
in application creates lot of overhead managing updates may need exist between frames. preferred approach use modaljdialog
if window required. discussed more here.
Comments
Post a Comment