swing - Java JButton not updating -
i have jbutton when call settext(), text not update. hover on button, updates. how can fix when call settext(), button updates? post methods edt takes it. goes in order :)
public void mouseclicked(mouseevent e) {//this differn't event. not executed clicking on same button. it's differn't one. click(new point(e.getx() + gridscrollpaneviewport.getviewposition().x, e.gety() + gridscrollpaneviewport.getviewposition().y)); } public void click(point point) { map.selectplot((plotgui) map.getcomponentat(point)); } public void selectplot(plotgui plot) { chickentycoon.getinstance().getscreen().getplayingscreen().sidebar .setplot(plot); selectedplot = plot; } public void setplot(plotgui plot) { title.settext(plot.getname() + plot.getx() + plot.gety()); (component comp : stuffpanel.getcomponents()) { stuffpanel.remove(comp); } (component comp : plot.getcomponentlist()) { stuffpanel.add(comp); } update(); } public void update() { if (chickentycoon.getinstance().getscreen().getplayingscreen().map .getselectedplot() != null) { chickentycoon.getinstance().getscreen().getplayingscreen().map .getselectedplot().update(); } } public void update() { log.m("updating landgui"); ((cbutton) componentlist[0]).settext("buy $ "//i know casting cbutton, it's custom version of jbutton. tried jbutton , same thing happens. + ((land) parentplot).getpurchaseprice()); }
cbutton.java
package gui; import game.gameconfiguration; import java.awt.basicstroke; import java.awt.color; import java.awt.dimension; import java.awt.font; import java.awt.fontmetrics; import java.awt.frame; import java.awt.graphics; import java.awt.graphics2d; import java.awt.insets; import java.awt.renderinghints; import java.awt.shape; import java.awt.stroke; import java.awt.event.componentevent; import java.awt.event.componentlistener; import java.awt.geom.area; import java.awt.geom.rectangle2d; import java.awt.geom.roundrectangle2d; import javax.swing.jbutton; import javax.swing.joptionpane; import javax.swing.swingutilities; import chicken.globalconfiguration; public class cbutton extends jbutton implements componentlistener { protected static final int border_width = 5; private static final font font = globalconfiguration.font; private static final insets insets_margin = new insets(2, 5, 2, 5); private static final long serialversionuid = 1l; protected area m_areadraw = null; private area m_areafill = null; private double m_dheightdraw = 0d; private double m_dheightfill = 0d; private double m_dwidthdraw = 0d; private double m_dwidthfill = 0d; private int m_nminheight = 0; private int m_nminwidth = 0; private int m_nstringheightmax = 0; private int m_nstringwidthmax = 0; private roundrectangle2d m_rrect2ddraw = null; private roundrectangle2d m_rrect2dfill = null; private shape m_shape = null; public cbutton(string strlabel) { setcontentareafilled(false); setmargin(cbutton.insets_margin); setfocuspainted(false); addcomponentlistener(this); setfont(cbutton.font); settext(strlabel); } @override public void componenthidden(componentevent e) { } @override public void componentmoved(componentevent e) { } // needed if want button resize @override public void componentresized(componentevent e) { m_shape = new rectangle2d.float(0, 0, getbounds().width, getbounds().height); m_dwidthfill = (double) getbounds().width - 1; m_dheightfill = (double) getbounds().height - 1; m_dwidthdraw = ((double) getbounds().width - 1) - (cbutton.border_width - 1); m_dheightdraw = ((double) getbounds().height - 1) - (cbutton.border_width - 1); setshape(); repaint(); } @override public void componentshown(componentevent e) { } @override public boolean contains(int nx, int ny) { if ((null == m_shape) || m_shape.getbounds().equals(getbounds())) { m_shape = new rectangle2d.float(0, 0, this.getbounds().width, this.getbounds().height); } return m_shape.contains(nx, ny); } @override public void paintborder(graphics g) { graphics2d g2 = (graphics2d) g; renderinghints hints = new renderinghints( renderinghints.key_antialiasing, renderinghints.value_antialias_on); g2.setrenderinghints(hints); g2.setcolor(color.black); stroke strokeold = g2.getstroke(); g2.setstroke(new basicstroke(cbutton.border_width, basicstroke.cap_round, basicstroke.join_round)); g2.draw(m_areadraw); if (getmodel().isrollover()) { g2.setcolor(color.gray); g2.draw(m_areadraw); } g2.setstroke(strokeold); }; @override public void paintcomponent(graphics g) { graphics2d g2 = (graphics2d) g; renderinghints hints = new renderinghints( renderinghints.key_antialiasing, renderinghints.value_antialias_on); g2.setrenderinghints(hints); if (getmodel().isarmed()) { g2.setcolor(color.cyan.darker()); } else { g2.setcolor(color.cyan); } g2.fill(m_areafill); super.paintcomponent(g2); } private void setshape() { // area double darclengthfill = math.min(m_dwidthfill, m_dheightfill); m_rrect2dfill = new roundrectangle2d.double(0d, 0d, m_dwidthfill, m_dheightfill, darclengthfill, darclengthfill); // warning: arclength , archeight divided 2 // when roundedrectangle shape m_areafill = new area(m_rrect2dfill); // border double darclengthdraw = math.min(m_dwidthdraw, m_dheightdraw); m_rrect2ddraw = new roundrectangle2d.double( (cbutton.border_width - 1) / 2, (cbutton.border_width - 1) / 2, m_dwidthdraw, m_dheightdraw, darclengthdraw, darclengthdraw); m_areadraw = new area(m_rrect2ddraw); } @override public void settext(final string strtext) { cbutton.super.settext(strtext); frame frame = joptionpane.getrootframe(); fontmetrics fm = frame.getfontmetrics(font); m_nstringwidthmax = fm.stringwidth(gettext()); m_nstringwidthmax = math.max(m_nstringwidthmax, fm.stringwidth(gettext())); // warning: use getmargin. refers dist btwn text , border. // use getinsets. refers width of border int nwidth = math.max(m_nminwidth, m_nstringwidthmax + getmargin().left + getinsets().left + getmargin().right + getinsets().right); m_nstringheightmax = fm.getheight(); // warning: use getmargin. refers dist btwn text , border. // use getinsets. refers width of border int nheight = math.max(m_nminheight, m_nstringheightmax + getmargin().left + getinsets().left + getmargin().right + getinsets().right); setpreferredsize(new dimension( nwidth + ((2 * getfont().getsize()) / 5), nheight + ((2 * getfont().getsize()) / 5))); // set initial draw , fill dimensions setshape(); } }
i fixed it. needed add repaint container button. simple that!
button.settext("some text"); buttoncontainer.repaint();
Comments
Post a Comment