Advertisement
Guest User

Untitled

a guest
Nov 2nd, 2014
33
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.50 KB | None | 0 0
  1. import java.awt.*;
  2. import java.awt.event.*;
  3.  
  4. import javax.swing.*;
  5.  
  6. public class PopupCrasher extends JFrame {
  7.     int counter = 0;
  8.  
  9.     public PopupCrasher() {
  10.     setSize(200, 200);
  11.     setLayout(new BorderLayout());
  12.  
  13.     final JTextField inputField = new JTextField();
  14.     add(inputField, BorderLayout.CENTER);
  15.  
  16.     inputField.addKeyListener(new KeyAdapter() {
  17.         public void keyTyped(KeyEvent e) {
  18.  
  19.         // Issue is only reproduceable, when JTable is added to JPopupMenu and it is heavyweight
  20.         final JPopupMenu popup = new JPopupMenu();
  21.         popup.setLightWeightPopupEnabled(false);
  22.         JTable table = new JTable(new Object[][]{{String.valueOf(counter++)}}, new String[]{"CounterValue"});
  23.         popup.add(table);
  24.  
  25.         // Layout-Stuff not critical for exposing the issue
  26.         Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
  27.         int yPos = ((int) inputField.getLocationOnScreen().getY()) + inputField.getHeight() + 2;
  28.         int maxHeight = Math.min(200, screenSize.height - yPos);
  29.         popup.setPreferredSize(new Dimension(table.getPreferredSize().width + 20, maxHeight));
  30.        
  31.         // show popup and immediatly transfer focus back to the textfield to allow contious typing
  32.         popup.show(inputField, 0, inputField.getHeight() + 2);
  33.         inputField.requestFocus();
  34.         }
  35.     });
  36.    
  37.     setVisible(true);
  38.     setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  39.     }
  40.  
  41.     public static void main(String[] args) {
  42.     SwingUtilities.invokeLater(new Runnable() {
  43.         public void run() {
  44.         new PopupCrasher();
  45.         }
  46.     });
  47.     }
  48. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement