Ejemplo n.º 1
0
  private void prepareGUI() {

    frame = new JFrame("Control Sample");
    frame.setSize(600, 480);
    frame.setLayout(new GridLayout(3, 1));

    headerLabel = new JLabel("", JLabel.CENTER);

    statusLabel = new JLabel("", JLabel.CENTER);

    frame.addWindowFocusListener(
        new WindowAdapter() {
          public void windowClosing(WindowEvent windowEvent) {
            System.exit(0);
          }
        });

    controlPanel = new JPanel();
    controlPanel.setLayout(new FlowLayout());

    frame.add(headerLabel);
    frame.add(statusLabel);
    frame.add(controlPanel);
  }
Ejemplo n.º 2
0
  /** Initialiser. */
  private void init() {

    invokePopupButton = new JToggleButton("Show popup");
    invokePopupButton.addActionListener(
        new ActionListener() {
          public void actionPerformed(ActionEvent e) {
            // set popup window visibility
            if (!popupWindow.isVisible()) {
              // set location relative to button
              Point location = invokePopupButton.getLocation();
              SwingUtilities.convertPointToScreen(location, invokePopupButton.getParent());
              location.translate(
                  0,
                  invokePopupButton.getHeight()
                      + (invokePopupButton.getBorder() == null
                          ? 0
                          : invokePopupButton
                              .getBorder()
                              .getBorderInsets(invokePopupButton)
                              .bottom));
              popupWindow.setLocation(location);

              // show the popup if not visible
              invokePopupButton.setText("Hide popup");
              popupWindow.setVisible(true);
              popupWindow.requestFocus();
            } else {
              // hide it otherwise
              invokePopupButton.setText("Show popup");
              popupWindow.setVisible(false);
            }
          }
        });

    // add components to main panel
    this.setLayout(new BorderLayout());
    this.add(invokePopupButton, BorderLayout.CENTER);

    // use frame
    popupWindow = new JFrame();
    popupWindow.setUndecorated(true);

    popupWindow.addWindowFocusListener(
        new WindowFocusListener() {
          public void windowGainedFocus(WindowEvent e) {}

          public void windowLostFocus(WindowEvent e) {
            SwingUtilities.invokeLater(
                new Runnable() {
                  public void run() {
                    if (popupWindow.isVisible()) invokePopupButton.doClick();
                  }
                });
          }
        });

    // add some components to window
    popupWindow.getContentPane().setLayout(new BorderLayout());
    ((JComponent) popupWindow.getContentPane()).setBorder(BorderFactory.createEtchedBorder());
    JTextField aTextField = new JTextField(10);
    popupWindow.getContentPane().add(new JLabel("Text:"), BorderLayout.WEST);
    popupWindow.getContentPane().add(aTextField);
    popupWindow.pack();
  }