Java JInternalFrame set size

Description

Java JInternalFrame set size

import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.BorderFactory;
import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JDesktopPane;
import javax.swing.JFrame;
import javax.swing.JInternalFrame;
import javax.swing.JPanel;
import javax.swing.WindowConstants;

public class Main extends JFrame implements ActionListener {
  JDesktopPane desktop = new JDesktopPane();
  JInternalFrame displayWindow;//from  w w w  .  j  a  v a 2s.  c  o m
  JInternalFrame listenedToWindow;
  static final String SHOW = "show";
  static final String CLEAR = "clear";
  static final int desktopWidth = 500;
  static final int desktopHeight = 300;

  public Main(String title) {
    super(title);
    desktop.putClientProperty("JDesktopPane.dragMode", "outline");
    desktop.setPreferredSize(new Dimension(desktopWidth, desktopHeight));
    setContentPane(desktop);
    createDisplayWindow();
    desktop.add(displayWindow); 
    Dimension displaySize = displayWindow.getSize();
    displayWindow.setSize(desktopWidth, displaySize.height);
  }

  // Create the window that displays event information.
  protected void createDisplayWindow() {
    JButton b1 = new JButton("Show internal frame");
    b1.setActionCommand(SHOW);
    b1.addActionListener(this);

    JButton b2 = new JButton("Clear event info");
    b2.setActionCommand(CLEAR);
    b2.addActionListener(this);


    displayWindow = new JInternalFrame("Event Watcher", true, // resizable
        false, // not closable
        false, // not maximizable
        true); // iconifiable
    JPanel contentPane = new JPanel();
    contentPane.setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10));
    contentPane.setLayout(new BoxLayout(contentPane, BoxLayout.PAGE_AXIS));
    b1.setAlignmentX(CENTER_ALIGNMENT);
    contentPane.add(b1);
    contentPane.add(Box.createRigidArea(new Dimension(0, 5)));
    contentPane.add(b2);

    displayWindow.setContentPane(contentPane);
    displayWindow.pack();
    displayWindow.setVisible(true);
  }

  // Handle events on the two buttons.
  public void actionPerformed(ActionEvent e) {
    if (SHOW.equals(e.getActionCommand())) {
      if (listenedToWindow == null) {
        listenedToWindow = new JInternalFrame("Event Generator", true, // resizable
            true, // closable
            true, // maximizable
            true); // iconifiable

        listenedToWindow
            .setDefaultCloseOperation(WindowConstants.HIDE_ON_CLOSE);

        desktop.add(listenedToWindow);

        listenedToWindow.setSize(300, 100);
        listenedToWindow.setLocation(desktopWidth / 2
            - listenedToWindow.getWidth() / 2, desktopHeight
            - listenedToWindow.getHeight());
      }
      listenedToWindow.setVisible(true);
    } 
  }

  public static void main(String[] args) {
     JFrame frame = new Main("InternalFrameEventDemo");
     frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
     frame.pack();
     frame.setVisible(true);
  }
}



PreviousNext

Related