Win a copy of Event Streams in Action this week in the Java in General forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Devaka Cooray
  • Liutauras Vilda
  • Jeanne Boyarsky
  • Bear Bibeault
Sheriffs:
  • Paul Clapham
  • Knute Snortum
  • Rob Spoor
Saloon Keepers:
  • Tim Moores
  • Ron McLeod
  • Piet Souris
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Tim Holloway
  • Frits Walraven
  • Ganesh Patekar

Right Click in a TextArea problem

 
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What seems to be the problem when you see this error message?

Exception in thread "AWT-EventQueue-0" java.lang.RuntimeException: parent not showing on screen

The error appears when I place the 'textArea.addActionListener(this);' right after 'textArea = new JTextArea(5,10);'

Just like this:

code:
----------------------------------------------------------------------------

textArea = new JTextArea(5,10);
textArea.addMouseListener(this);

----------------------------------------------------------------------------

when they are like this there is no error and no right click.

code:
----------------------------------------------------------------------------

textArea.addMouseListener(this);
textArea = new JTextArea(5,10);

----------------------------------------------------------------------------

I want my textArea to have a right click but it is not working.

Is there anything missing here?

code:
----------------------------------------------------------------------------

public void mousePressed(MouseEvent me)
{
System.out.print("Mouse Right-Clicked!");//Test if the mousePressed is working
if(me.getModifiers() != 0){
popup.show(this, me.getX(), me.getY());

}
}

public void mouseClicked(MouseEvent e){}
public void mouseReleased(MouseEvent e){}
public void mouseEntered(MouseEvent e){}
public void mouseExited(MouseEvent e){}

----------------------------------------------------------------------------



To give you an overview of my code, this is it. My textArea is inside:

code:
----------------------------------------------------------------------------

public Container createContentPane(){

...
textArea = new JTextArea(5,10);
textArea.addMouseListener(this);
...
return contentPane;

}

----------------------------------------------------------------------------

which is called inside:


code:
----------------------------------------------------------------------------

public static void createShowGUI(){

...
notepad notpad = new notepad();//class instance
frame.setContentPane(notpad.createContentPane());

}

----------------------------------------------------------------------------

I already tried to put the 'textArea.addMouseListener(this);' inside the constructor but it is not
working.

Please help me. Thanks!
 
Bartender
Posts: 1561
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
First off, I advise you not to have your GUI class implement the MouseListener. It would be cleaner to just create an anonymous inner class. Next of all, you may have better luck getting decent responses if you show us compilable code. I don't want to see your whole program, but rather you should condense your code into the smallest bit that still compiles, has no extra code that's not relevant to your problem, but still demonstrates your problem, in other words, an SSCCE (Short, Self Contained, Correct (Compilable), Example). For more info on SSCCEs please look here:

http://homepage1.nifty.com/algafield/sscce.html

For example, here is an example of using an anonymous inner class for a MouseListener. I actually use a MouseAdapter so I don't have to have all of those empty mouse methods:


Last, don't forget to use code tags so that your code will retain its formatting and be readable.

Good luck!
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!