netbeans - Java program with multiple windows(JPanel), how to connect them to JFrame -
i have library application permits users log in, , many other things.
i don't know how make multiple windows (views), such login, , if succesfull, closing current window , opening other user interface.
i hope have been clear.
updated answer: suppose 3 jpanel
extensions called myloginpanel
, mywelcomepanel
, , myformpanel
. i'm assuming form mention main class, among these, instead of actual components make form.
each of these panels should take care of own components.
looking @ code below, have find-and-replace names ones you're using.
suggestion: make main class implement interface, listen changes on 3 distinct panels. e.g.: user clicked on button should make application progress next panel. button has inform someone. implementer of interface.
enum appstate { login, welcome, form }
interface progresslistener { void progressfrom(appstate currentstate); }
import java.awt.*; import java.awt.event.*; import javax.swing.*; public class myform implements runnable, progresslistener { // instance variables private jframe frame; private myloginpanel loginpanel; private mywelcomepanel welcomepanel; private myformpanel formpanel; /** empty constructor of objects of class someclassui. */ public someclassui() { // ... } /** interface initialization. */ @override public void run() { // these should handle own component initialization. // should, @ least, receive reference listener. loginpanel = new myloginpanel(this); welcomepanel = new mywelcomepanel(this); formpanel = new myformpanel(this); frame = new jframe("testing"); frame.setdefaultcloseoperation(jframe.exit_on_close); frame.setcontentpane(loginpanel); frame.pack(); frame.setlocationrelativeto(null); frame.setvisible(true); } /** */ @override public void progressfrom(appstate whotriggeredit) { switch (whotriggeredit) { case login: frame.setcontentpane(welcomepanel); frame.pack(); return; case welcome: frame.setcontentpane(formpanel); frame.pack(); return; default: return; } } /** */ public void go() { swingutilities.invokelater(this); } /** */ public static void main(string[] args) { new someclassui().go(); } }
class myloginpanel extends jpanel implements actionlistener { private final progresslistener listener; public myloginpanel(progresslistener listener) { // components, etc. this.listener = listener; } ... /** */ @override public void actionperformed(actionevent e) { // validate login, or whatever. // went well, notify listener progress. this.listener.progressfrom(appstate.login); } }
instead of adding / removing elements directly jframe
, may want these things on content panel of own, store instance variable (see jframe.setcontentpane()
).
if want panels accessible at same time, instead of procedural step-by-step change, may want cardlayout
, suggested in comments.
the suggestions give general guidelines, i'm not aware of details of needs.
Comments
Post a Comment