Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 195
  • Last Modified:

debugging

Hi

Actually in the below code there is one button in a frame .. if I use tooltip for that button it triggers the graphics repaint()  method (rectangle is drawn once more on the same location) .... I need to disable this action ....
that is the tooltip should show the text without repainting() the graphics the screen

does some have have idea kindly forward me

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class Frame extends JFrame {
  int vijay=0;
  JPanel contentPane;
  JButton jButton1 = new JButton();
  JPanel jPanel1 = new JPanel();

  public Frame() {
 //   enableEvents(AWTEvent.WINDOW_EVENT_MASK);
    try {
      jbInit();

    }
    catch(Exception e) {
      e.printStackTrace();
    }
  }
  //Component initialization
  private void jbInit() throws Exception  {
    contentPane = (JPanel) this.getContentPane();
    contentPane.setLayout(null);
    this.setSize(new Dimension(601, 511));
    this.setTitle("repaint demo");
    jButton1.setBounds(new Rectangle(89, 60, 105, 55));
    jButton1.setToolTipText("button 1");
    jButton1.setText("jButton1");
    jButton1.addActionListener(new Frame_jButton1_actionAdapter(this));
    jPanel1.setBackground(Color.red);
    jPanel1.setBounds(new Rectangle(240, 67, 277, 374));
    jPanel1.setLayout(null);
    contentPane.add(jPanel1, null);
    jPanel1.add(jButton1, null);
  }
  //Overridden so we can exit when window is closed
  protected void processWindowEvent(WindowEvent e) {
    super.processWindowEvent(e);
    if (e.getID() == WindowEvent.WINDOW_CLOSING) {
      System.exit(0);
    }
  }

  public void paint(Graphics g){
    super.paint(g);
    g=this.contentPane.getGraphics();
    vijay++;
    System.out.println("repaints here "+vijay+"   ");
    g.drawRect(15,15,40,40);
  }
void jButton1_actionPerformed(ActionEvent e) {
Graphics g=this.contentPane.getGraphics();

    g.drawRect(15,15,40,40);
  }

}

class Frame_jButton1_actionAdapter implements java.awt.event.ActionListener {
  Frame adaptee;

  Frame_jButton1_actionAdapter(Frame adaptee) {
    this.adaptee = adaptee;
  }
  public void actionPerformed(ActionEvent e) {
    adaptee.jButton1_actionPerformed(e);
  }
}
0
ragavijayvendra
Asked:
ragavijayvendra
  • 9
  • 3
  • 2
  • +1
1 Solution
 
objectsCommented:
it can't really, once the tooltip disappears the are it covered needs to be repainted.
0
 
objectsCommented:
>     g=this.contentPane.getGraphics();

you don't need (or want) that line
0
 
objectsCommented:
void jButton1_actionPerformed(ActionEvent e) {
Graphics g=this.contentPane.getGraphics();
    g.drawRect(15,15,40,40);
  }
}


You shouldn't do that.
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
ragavijayvendraAuthor Commented:
Thanks for your reply !

I understand after using tooltip it has to repaint .. But in my actual frame i have around more than 40 buttons and each has tool tip and also it has complex graphics
so when i use the tool tip it unnecessary repaints the graphics and my frame flickers for the graphics !


0
 
objectsCommented:
add the following method to fix your flickering:

public void update(Graphics g)
{
   paint(g);
}
0
 
objectsCommented:
Actually that mightn't fix your flickering.
0
 
objectsCommented:
Instead of painting to you frame, I'd suggest instead using a JPanel subclass.
0
 
maheshexpCommented:
flickering occurs due to lack of buffering...u try using DoubleBuffering...

JPanel panel = new JPanel(true); /* true enables DoubleBuffering in JPanel */

in this JPanel in can add ur buttons and then further, the panl can be added to the JFrame
0
 
objectsCommented:
double buffering is already being used.
0
 
ragavijayvendraAuthor Commented:
Thanks for your reply !

I have tried but its has the same problem !

is there any possible way to make some flag (checking variable) for the tool tip  in the paint method when the toop tip is trigerred !

for example !!!

 public void paint(Graphics g){
    super.paint(g);
  if(tooltipenabled == false)
  {
    g=this.contentPane.getGraphics();
    vijay++;
    System.out.println("repaints here "+vijay+"   ");
    g.drawRect(15,15,40,40);
  }
  }
0
 
WebstormCommented:
Hi ragavijayvendra,

Try to override the repaint() method:

public void repaint()
{
    Graphics g=getGraphics();
    if (g==null) super.repaint();
    else paint(g);
}
0
 
objectsCommented:
> is there any possible way to make some flag (checking variable) for the tool tip  in the paint method
> when the toop tip is trigerred !

no

and it shouldn't be a problem. We use tooltips all the time without any problems.
Try instead doing your custom painting on a panel and not your frame as I suggested earlier.
0
 
WebstormCommented:
Hi ragavijayvendra,

what's new ? Is your problem solved ?

0
 
ragavijayvendraAuthor Commented:
HI Webstorm ,

Thanks for your message ! Actually I have tried your idea but it is not working !

But instead I have delete the Tooltip for all buttons.. after deleting the tool tip there is no such problem

But I am interested in  finding out the solution !

Hi  Object ,

By the way I cannot make a new paint panel Since I am only enhancing the existing project and it already stands around  10000 LOC..... So I don't have the right to change the existing code for making the Tooltip issue !
Kindly Help me for getting a better solution I mean without a new panel for paint

Thanks Object and Webstorm for your Valuable  Help

Friendly,
Vijay
0
 
objectsCommented:
sorry if u can't change the code then I don't think there is any way to solve your problem.
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

  • 9
  • 3
  • 2
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now