| Author |
Constant paintComponent calling
|
Edu Cobi
Greenhorn
Joined: Jan 24, 2004
Posts: 9
|
|
Hello, I have developed a JFrame in swing. I have the problem that the paintComponent function is called more than 50 times a second without a single repaint() anywhere. Please help me to find why it is repainting like crazy. Thanks.
|
 |
Matt Wilcko
Ranch Hand
Joined: Sep 23, 2003
Posts: 65
|
|
|
Need more info, Post your code
|
 |
Edu Cobi
Greenhorn
Joined: Jan 24, 2004
Posts: 9
|
|
Hi Matt, thanks for answering. the code is 400 lines long. I will cut it: public class Chart extends JPanel{ DataCollector dataCollect; BarArray bars1,bars2;... ... public Chart(BarArray bars1,BarArray bars2,OrderProcessor orderProc, DataCollector dataCollect){ this.config = (Config)Config.getConfig(); this.bars1 = bars1; ... public void paintComponent(Graphics g){ System.out.println("paintComponent called at "+sdf.format(new Date())); //this is how I see it is called 50 times/second ... public void main() { JFrame win = new JFrame("Edgar & Eduardo Automated Trader: Chart display software"); Image image = new ImageIcon( "bars.gif" ).getImage(); win.setIconImage(image); Container frameContent=win.getContentPane(); JButton exitB=new JButton("EXIT"); JButton backtestB=new JButton("Hist.data"); JButton performB=new JButton("Perform"); final OnOffIcon OnOffI= new OnOffIcon(); final JButton OnOffB = new JButton(OnOffI); JLabel placeOrdersL = new JLabel("Orders:"); final JButton placeOrdersB = new JButton(orderProc.getPlaceOrders()==true?"True":"False"); ButtonGroup barCandleGroup=new ButtonGroup(); JRadioButton CandleRB=new JRadioButton("CandleStick",true); barCandleGroup.add(barRB); barCandleGroup.add(CandleRB); JButton resetB=new JButton("Reset"); JButton ConB=new JButton("-><-"); // ConB.setIcon(new ImageIcon("rofl.gif")); JButton ExpB=new JButton("<-->"); JPanel top = new JPanel(); top.add(exitB); top.add(backtestB); top.add(performB); top.add(OnOffB); top.add(placeOrdersL); top.add(placeOrdersB); top.add(barRB); top.add(CandleRB); top.add(resetB); top.add(ConB); top.add(ExpB); // RBExpConPanel.setBorder(new TitledBorder("Chart types:")); frameContent.add(top,BorderLayout.NORTH); ... //listeners resetB.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent evt) { if(JOptionPane.showConfirmDialog( Chart.this,"Are you sure yo want to reset?", "Confirm resetting", JOptionPane.YES_NO_OPTION) == JOptionPane.YES_OPTION) { bars1.reset(); bars2.reset(); ((StrategyAnalyzer)config.get("analyzer")).reset(); repaint(); } } }); ... ... win.pack(); win.setVisible(true); repaint(); } PLEASE HELP! THANKS.
|
 |
Craig Wood
Ranch Hand
Joined: Jan 14, 2004
Posts: 1535
|
|
I was suspicious of the repaint call in the code below but now it doesn't look so bad after I formatted it. You might try putting a System.out.println inside this and other event handlers to see if they are being called too much. If you do things right there should not be a need to call repaint at the end (after win.pack). I would go thru your code and comment out every call to repaint in an attempt to stop the repainting. And make sure you don't have a repaint inside any paint or paintComponent methods. [ January 24, 2004: Message edited by: Craig Wood ]
|
 |
Edu Cobi
Greenhorn
Joined: Jan 24, 2004
Posts: 9
|
|
Thank you very much for your help. IT HAS SOLVED THE PROBLEM!!! I have commented out all the repaint() calls and now there is only repaint when I hide and show back the window. I think it was this final repaint call after win.pack; I will check it now by uncommenting the actionlistener repaint calls. It has also solved the problem of my button being not visible. NOW EVERYTHING IS PERFECT. THANKS!!! ps: now the problem is going to be how to refresh when new data comes. ha ha.
|
 |
 |
|
|
subject: Constant paintComponent calling
|
|
|