• Post Reply Bookmark Topic Watch Topic
  • New Topic

JFrame positioning

 
Alex Kravets
Ranch Hand
Posts: 476
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
I have a JTable in which you right-click on a cell a JFrame pops-up. It always pops-up in a right-top corner. How do I make it pop-up where mouse was clicked? I remember doing something like this in the past using relativeTo method of some sort, but can't remember correct name nor how to use it.
thanks,
Alex
 
Nathan Pruett
Bartender
Posts: 4121
IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
First, you'll need to get the Point where the mouse was clicked. Using a mouse listener this is recieved using event.getPoint(). This point is the point on the component that was clicked, and you'll have to translate this to a point on the screen. Use the SwingUtilities method convertPointToScreen(), then use the point you got from this method to position the frame...

the code below illustrates this process, but is not meant to compile...
 
Ashish Mahajan
Ranch Hand
Posts: 77
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Alex,
As Nathan has suggested that is the sol. Note that he has assumed that u r using AWT. In case u r using Swing then use JFrame and not frame. So perhaps it would be better to state what app. u r trying to develop.
But still wondering why do u need to use frame to show as popup. Have u taken look at JPopupMenu yet. For simle menu items there r straight forward methods. If u want to add moderate sized component then :- new JMenu().add(mycomp).getPopupMenu();
Also few points worth noting :-
- If the popup menu contains components that also have popups like comboboxes then :- create dialog and add the component u want in the dialog's contentpane as usual and then call setUndecoarted(true) which will remove the title pane of the dialog and will exactly like popup with no probs Since in 1.3 there is no method to remove the title pane. i don;t know the sol. May be it's better to left the system title pane as it is. well looking at the probs created by popup menu in such cases u'll definitely choose this sol.
- Do not use Box in the component in the popup. It raises exception on Mac OS X. Use border layout or other combination of layouts.
 
Ashish Mahajan
Ranch Hand
Posts: 77
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
One more popup probs worth noting :-
- In JDK 1.4 and the Windows LAF do not put JButton in the component that is to be put in the popup. At first glance everything will look fine. But once that button is clicked every popup component (like JComboBox) in all the application won't work. A possible sol could be to put JLabel in JPanel and it look like JButton by setting label's fg, bg and panel's border etc.
Note that this prob is only in JDK 1.4 and that too only for Windows LAF.
 
Alex Kravets
Ranch Hand
Posts: 476
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks guys!
I tried using your code:
public void mousePressed(MouseEvent me){
if(me.getModifiers() == InputEvent.BUTTON3_MASK){
if(!isFrameVisible){
boolean isInq,isAdd,isMod,isDel;
row = table.rowAtPoint(me.getPoint());
col = table.columnAtPoint(me.getPoint());
progName = (String)programs.elementAt(row);
progDesc = (String)rowNames.elementAt(row);
groupName = columns[col];
Component c = (Component)me.getSource();
Point p = me.getPoint();
p = SwingUtilities.convertPointToScreen(p,c);
But when I compile it, I get error for p = SwingUtilities.convertPointToScreen(p,c) :
AccessMaintenance.java:393: incompatible types
found : void
required: java.awt.Point
p = SwingUtilities.convertPointToScreen(p,c);
thanks,
Alex
 
Alex Kravets
Ranch Hand
Posts: 476
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok, I saw your comment about this not compiling. I looked at SwingUtilities.convertPointToScreen(Point p, Component c) API, and it has return type of public static void, so when it "converts" coordinates, how does it return new coordinates?
thanks,
Alex
 
Alex Kravets
Ranch Hand
Posts: 476
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ok, it works if I do this:
Component c = (Component)me.getSource();
Point p = me.getPoint();
SwingUtilities.convertPointToScreen(p,table);
 
Nathan Pruett
Bartender
Posts: 4121
IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sorry... that example code was rushed... I had a meeting to go to...

(But I did put in the "the code below illustrates this process, but is not meant to compile..." line... )

Yes, unlike the majority of Java methods, SwingUtilities.convertPointToScreen( Point, Component ) actually changes the values in the parameter that is passed to it rather than returning a new Point object... sorry again for the bad code and confusion...
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!