Win a copy of Java Concurrency Live Lessons this week in the Threads forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

repaint()  RSS feed

 
dejie lin
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi all,
I try to add a diagram.repaint() command inside a botton actionlistener, but the repaint() seems not works, the programming likes following :
......
w=130;
h=90;
JPanel chart = new drawtables(w,h);
change.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent event)
{
w = Integer.parseInt(width.getText());
h = Integer.parseInt(height.getText());
chart = new drawtables(w,h);
chart.repaint();
}
});

.......
class drawtables extends JPanel
{
public static int tw=130; //table width;
public static int th=90; //table height;
public drawtables(int w, int h)
{
tw = w;
th = h;
this.setPreferredSize(new Dimension(800,600));
this.invalidate();
this.repaint(5);
}
public void paint(Graphics g)
{
super.paint(g);
Graphics2D g2 = (Graphics2D)g;
drawchart(g2);
}
.......
}
how should i make the repaint() work?
thanks a lot
dejie
 
Nathan Pruett
Bartender
Posts: 4121
IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It doesn't work because you are completely replacing the reference to chart in your actionListener... you don't need to create a new drawtables instance; you need to have a methods like setWidth() and setHeight() to modify the reference you already have.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!