java - JFrame/JPanel refreshing and text fields -


i'm trying switch between displaying different jpanels in borderlayout.center in jframe. jpanels each contain jtextfield , jtextpane. calling revalidate() , repaint() on jframe, strange result: first time switch secondpanel clicking secondbutton, works expected. when try switch using firstbutton, nothing happens. switches when click secondbutton again, , after works expected, except in reverse: clicking firstbutton displays secondpanel , clicking secondbutton displays firstpanel.

calling revalidate() , repaint() on jpanel displayed instead, works fine, until click either text area. every click, it's part of text "falls off" facade, revealing text of corresponding area of other jpanel underneath. if type something, text changes instantly of other jpanel, , whatever typed added after switching jpanels. it's weird , hard describe properly, see yourselves.

i have 3 questions: 1. hell, 2. why hell, , 3. how fix it?

import javax.swing.*; import java.awt.*; import java.awt.event.*;  public class myframe extends jframe {      class myactionlistener implements actionlistener {         jpanel panel;         myactionlistener(jpanel _panel) {             panel = _panel;         }          @override         public void actionperformed(actionevent ae) {             myframe.this.add(panel, borderlayout.center);             //myframe.this.revalidate();             //myframe.this.repaint();             panel.revalidate();             panel.repaint();         }     }      jpanel buttonpanel = new jpanel();      jbutton firstbutton = new jbutton("first");     jpanel firstpanel = new jpanel();     jtextpane firstpane = new jtextpane();     jtextfield firstfield = new jtextfield("first field");      jbutton secondbutton = new jbutton("second");     jpanel secondpanel = new jpanel();     jtextpane secondpane = new jtextpane();     jtextfield secondfield = new jtextfield("second field");      myframe() {         firstpane.settext("first pane");         firstpanel.setlayout(new borderlayout());         firstpanel.add(firstpane, borderlayout.center);         firstpanel.add(firstfield, borderlayout.south);         firstbutton.addactionlistener(new myactionlistener(firstpanel));          secondpane.settext("second pane");         secondpanel.setlayout(new borderlayout());         secondpanel.add(secondpane, borderlayout.center);         secondpanel.add(secondfield, borderlayout.south);         secondbutton.addactionlistener(new myactionlistener(secondpanel));          buttonpanel.setlayout(new gridlayout(0,1));         buttonpanel.add(firstbutton);         buttonpanel.add(secondbutton);          add(buttonpanel, borderlayout.west);         add(firstpanel, borderlayout.center);          setsize(new dimension(300,300));         setdefaultcloseoperation(jframe.exit_on_close);         setvisible(true);     }      public static void main(string[] args) {         new myframe();     } } 

how fix it?

use cardlayout. see section swing tutorial on how use cardlayout more information , examples.


Comments

Popular posts from this blog

asp.net mvc - SSO between MVCForum and Umbraco7 -

Python Tkinter keyboard using bind -

ubuntu - Selenium Node Not Connecting to Hub, Not Opening Port -