Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.awt.*;
- import java.awt.event.*;
- import javax.swing.*;
- public class PopupCrasher extends JFrame {
- int counter = 0;
- public PopupCrasher() {
- setSize(200, 200);
- setLayout(new BorderLayout());
- final JTextField inputField = new JTextField();
- add(inputField, BorderLayout.CENTER);
- inputField.addKeyListener(new KeyAdapter() {
- public void keyTyped(KeyEvent e) {
- // Issue is only reproduceable, when JTable is added to JPopupMenu and it is heavyweight
- final JPopupMenu popup = new JPopupMenu();
- popup.setLightWeightPopupEnabled(false);
- JTable table = new JTable(new Object[][]{{String.valueOf(counter++)}}, new String[]{"CounterValue"});
- popup.add(table);
- // Layout-Stuff not critical for exposing the issue
- Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
- int yPos = ((int) inputField.getLocationOnScreen().getY()) + inputField.getHeight() + 2;
- int maxHeight = Math.min(200, screenSize.height - yPos);
- popup.setPreferredSize(new Dimension(table.getPreferredSize().width + 20, maxHeight));
- // show popup and immediatly transfer focus back to the textfield to allow contious typing
- popup.show(inputField, 0, inputField.getHeight() + 2);
- inputField.requestFocus();
- }
- });
- setVisible(true);
- setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
- }
- public static void main(String[] args) {
- SwingUtilities.invokeLater(new Runnable() {
- public void run() {
- new PopupCrasher();
- }
- });
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement