java - Repaint() between different classes does not update -
i'm trying make simple word processor there controlpanel on top of jframe , textpanel in center jtextarea component. basic overview of program there 1 jpanel called mypanel takes entire jframe , holds rest of panels. inside mypanel controlpanel , textpanel (not nested). controlpanel contains various jbuttons , jcomboboxes font styling while textpanel has jtextarea.
what want when press jbutton in controlpanel class, example bold, go actionperformed() , "text.repaint." i'm finding text.repaint not go textpanel , never enters paintcomponent method. tried nesting controlpanel in textpanel , giving borderlayout.north, , works fine, seems paper connected control panel, don't like.
can give me alternative or explain why text.repaint() not working?
here's important portion of code reference: (important parts marked //)
notice: class controlpanel , class textpanel aren't aligned because lazy fix alignment on mediocre text editor. trust me, not nested , both in class mypanel
class controlpanel extends jpanel implements actionlistener { jbutton bold, italics; jcombobox font; jcombobox size; string [] fontsizes = {"8", "10", "12", "16", "20", "24", "36", "48", "56", "72"}; string [] fonttypes = {"arial", "serif", "sans serif", "gothic", "helvetica", "times new roman", "comic sans"}; public controlpanel() // class controlpanel control { setbackground(color.gray); this.setlayout(new flowlayout()); font boldfont = new font("serif", font.bold, 16); bold = new jbutton("b"); bold.setfont(boldfont); //bold.getmodel().setpressed(true); bold.addactionlistener(this); this.add(bold); font italicsfont = new font("serif", font.italic, 16); italics = new jbutton("i"); italics.setfont(italicsfont); //italics.getmodel().setpressed(true); italics.addactionlistener(this); this.add(italics); font = new jcombobox(fonttypes); font.setselectedindex(0); font.addactionlistener(this); this.add(font); size = new jcombobox(fontsizes); size.setselectedindex(2); size.addactionlistener(this); size.seteditable(true); this.add(size); } public void actionperformed(actionevent e) { string command = e.getactioncommand(); if (command.equals("b")) { if (boldfont) boldfont = false; else boldfont = true; } if (command.equals("i")) { if (italicsfont) italicsfont = false; else italicsfont = true; } fontselection = (string)font.getselecteditem(); sizeselection = integer.parseint((string)(size.getselecteditem())); text.repaint(); // repaints textpanel text class } } class textpanel extends jpanel // class textpanel text { jtextarea type; public textpanel() { this.setlayout(new borderlayout()); type = new jtextarea(); type.seteditable(true); type.setlinewrap(true); type.setwrapstyleword(true); type.settabsize(4); type.setmargin(new insets(80, 100, 80, 100)); this.add(type, borderlayout.center); } public void paintcomponent(graphics g) // paintcomponent() method textpanel { system.out.println("paintcomponent of text"); // not print out in terminal super.paintcomponent(g); font regfont; int fontstyle = 0; regfont = new font("arial", font.plain, 12); if (boldfont) { fontstyle = 1; } else if (!boldfont) { fontstyle = 0; } if (italicsfont) { if (boldfont) fontstyle = 3; else fontstyle = 2; } else if (!italicsfont) { if (boldfont) fontstyle = 1; else fontstyle = 0; } regfont = new font(fontselection, fontstyle, sizeselection); type.setfont(regfont); } }
your paint method adds no functionality overall application. you'd better off allowing control pane pass state request textpane effect text area directly.
basically, you're doing in paintcomponent method wrong approach trying , could, in fact, produce cyclic repaint call scenario consume cpu
updated example
import java.awt.borderlayout; import java.awt.color; import java.awt.eventqueue; import java.awt.flowlayout; import java.awt.font; import java.awt.graphics; import java.awt.insets; import java.awt.event.actionevent; import java.awt.event.actionlistener; import javax.swing.jbutton; import javax.swing.jcombobox; import javax.swing.jframe; import javax.swing.jpanel; import javax.swing.jtextarea; import javax.swing.uimanager; import javax.swing.unsupportedlookandfeelexception; public class testtextpane01 { public static void main(string[] args) { new testtextpane01(); } public testtextpane01() { eventqueue.invokelater(new runnable() { @override public void run() { try { uimanager.setlookandfeel(uimanager.getsystemlookandfeelclassname()); } catch (classnotfoundexception | instantiationexception | illegalaccessexception | unsupportedlookandfeelexception ex) { } textpanel textpanel = new textpanel(); controlpanel controlpanel = new controlpanel(textpanel); jframe frame = new jframe("test"); frame.setdefaultcloseoperation(jframe.exit_on_close); frame.setlayout(new borderlayout()); frame.add(controlpanel, borderlayout.north); frame.add(textpanel); frame.pack(); frame.setlocationrelativeto(null); frame.setvisible(true); } }); } class controlpanel extends jpanel implements actionlistener { jbutton bold, italics; jcombobox font; jcombobox size; string[] fontsizes = {"8", "10", "12", "16", "20", "24", "36", "48", "56", "72"}; string[] fonttypes = {"arial", "serif", "sans serif", "gothic", "helvetica", "times new roman", "comic sans"}; private boolean boldfont; private boolean italicsfont; private string fontselection; private int sizeselection; private textpanel textpane; public controlpanel(textpanel txtpanel) // class controlpanel control { textpane = txtpanel; setbackground(color.gray); this.setlayout(new flowlayout()); font boldfont = new font("serif", font.bold, 16); bold = new jbutton("b"); bold.setfont(boldfont); //bold.getmodel().setpressed(true); bold.addactionlistener(this); this.add(bold); font italicsfont = new font("serif", font.italic, 16); italics = new jbutton("i"); italics.setfont(italicsfont); //italics.getmodel().setpressed(true); italics.addactionlistener(this); this.add(italics); font = new jcombobox(fonttypes); font.setselectedindex(0); font.addactionlistener(this); this.add(font); size = new jcombobox(fontsizes); size.setselectedindex(2); size.addactionlistener(this); size.seteditable(true); this.add(size); } public void actionperformed(actionevent e) { string command = e.getactioncommand(); if (command.equals("b")) { boldfont = !boldfont; } else if (command.equals("i")) { italicsfont = !italicsfont; } fontselection = (string) font.getselecteditem(); sizeselection = integer.parseint((string) (size.getselecteditem())); // text.repaint(); // repaints textpanel text class textpane.setfont(boldfont, italicsfont); } } class textpanel extends jpanel // class textpanel text { jtextarea type; public textpanel() { this.setlayout(new borderlayout()); type = new jtextarea(); type.seteditable(true); type.setlinewrap(true); type.setwrapstyleword(true); type.settabsize(4); type.setmargin(new insets(80, 100, 80, 100)); this.add(type, borderlayout.center); } public void setfont(boolean boldfont, boolean italicsfont) { font font = type.getfont(); int style = font.plain; if (boldfont && italicsfont) { style = font.bold | font.italic; } else if (boldfont) { style = font.bold; } else if (italicsfont) { style = font.italic; } font = font.derivefont(style); system.out.println("font"); type.setfont(font); } } } i'd suggest have read of how use scroll panes, performing custom painting , painting in awt , swing
Comments
Post a Comment