Results 1 to 1 of 1
- 08-04-2009, 02:13 AM #1
Member
- Join Date
- Aug 2009
- Posts
- 1
- Rep Power
- 0
JPopupMenu, JTextField, and Canvas not working together
Hello all,
I'm writing a graphical program and am trying to use a Canvas object for the majority of my main window. I have a BorderLayout set with the Canvas placed into the CENTER and a JTextField to the SOUTH. The problem I am getting is that once I click into the JTextArea and type letters, I am unable to right click into my Canvas to retrieve my JPopupMenu. Printing out the location and visibility of the JPopupMenu states that the menu is visible and is at the appropriate location, yet nothing shows. I've included the simplest program I could write that duplicates the bug below. The problem does not occur when a JPanel is used in the CENTER instead.
Also, the JPopupMenu will show when a right click is used that would place the popup outside of the window.
Any help or workarounds would be greatly appreciated. Thanks!Java Code:import java.awt.BorderLayout; import java.awt.Canvas; import java.awt.Dimension; import java.awt.event.MouseEvent; import java.awt.event.MouseListener; import javax.swing.JFrame; import javax.swing.JMenuItem; import javax.swing.JPanel; import javax.swing.JPopupMenu; import javax.swing.JTextField; public class Problem extends Canvas implements MouseListener{ private JFrame window = new JFrame(); private JTextField message = new JTextField(); public static void main(String args[]) { Problem problem = new Problem(); } public Problem() { initMainWindow(); window.pack(); window.setVisible(true); } private void initMainWindow() { JPanel panel = (JPanel)window.getContentPane(); panel.setLayout(new BorderLayout()); setBounds(0,0,200,200); setPreferredSize(new Dimension(200,200)); addMouseListener(this); panel.add(this, BorderLayout.CENTER); panel.add(message, BorderLayout.SOUTH); window.setResizable(false); window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); window.pack(); } public void mousePressed(MouseEvent e) { if(e.getButton() == MouseEvent.BUTTON3) { JPopupMenu menu = new JPopupMenu(); JMenuItem testItem = new JMenuItem("Test"); menu.add(testItem); menu.show(window, e.getXOnScreen() - window.getX(), e.getYOnScreen() - window.getY()); } } public void mouseClicked(MouseEvent e) {} public void mouseEntered(MouseEvent e) {} public void mouseExited(MouseEvent e) {} public void mouseReleased(MouseEvent e) {} }
EDIT: I found a workaround. After accidentally minimizing, then restoring the window, I found the JPopupMenu began to function again. So, I tried a few JFrame methods until I found that calling window.validate() before displaying the JPopupMenu worked. Hopefully this quick fix helps anybody else running into this problem, though I'm still unsure as to why it functions like this in the first place. I suppose it may just be a bug.Last edited by ShinobiAC; 08-04-2009 at 03:31 PM. Reason: workaround found
Similar Threads
-
SWT Canvas drawing realtive to image space not canvas space.
By bobbie in forum SWT / JFaceReplies: 0Last Post: 07-05-2009, 12:31 PM -
JTextField in a JPopupMenu
By baran_khan in forum AWT / SwingReplies: 6Last Post: 05-02-2009, 09:43 PM -
JMenu to JPopupMenu
By carderne in forum New To JavaReplies: 0Last Post: 02-28-2009, 06:07 PM -
how to access jTextField of one JFrame1 from JFrame2 & Modify JTextField contents
By sumit1mca in forum AWT / SwingReplies: 1Last Post: 01-30-2009, 06:44 PM -
JPopupMenu
By hungleon88 in forum NetBeansReplies: 8Last Post: 01-15-2009, 05:00 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks