Example usage for javax.swing JFrame setLocation

List of usage examples for javax.swing JFrame setLocation

Introduction

In this page you can find the example usage for javax.swing JFrame setLocation.

Prototype

@Override
public void setLocation(Point p) 

Source Link

Document

The method changes the geometry-related data.

Usage

From source file:Main.java

static void createFrameAtLocation(Point p) {
    JFrame frame = new JFrame();
    frame.setTitle("Test frame on two screens");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    JPanel panel = new JPanel(new BorderLayout());
    JTextArea textareaA = new JTextArea(24, 80);
    textareaA.setBorder(BorderFactory.createLineBorder(Color.DARK_GRAY, 1));
    panel.add(textareaA, BorderLayout.CENTER);
    frame.setLocation(p);
    frame.add(panel);//w ww.  j av  a  2s  . c  o m
    frame.pack();
    frame.setExtendedState(Frame.MAXIMIZED_BOTH);
    frame.setVisible(true);
}

From source file:Main.java

public static void centerFrameOnScreen(JFrame frame) {

    Toolkit toolkit = Toolkit.getDefaultToolkit();
    Dimension screenSize = toolkit.getScreenSize();
    Dimension frameSize = frame.getSize();
    if (frameSize.height > screenSize.height) {
        frameSize.height = screenSize.height;
    }//from   ww  w .j  a  v a 2 s . com
    if (frameSize.width > screenSize.width) {
        frameSize.width = screenSize.width;
    }
    Point p = new Point((screenSize.width - frameSize.width) / 2, (screenSize.height - frameSize.height) / 2);
    frame.setLocation(p);
}

From source file:Main.java

public void mouseDragged(MouseEvent e) {
    Point current = this.getScreenLocation(e);
    Point offset = new Point((int) current.getX() - (int) start_drag.getX(),
            (int) current.getY() - (int) start_drag.getY());
    JFrame frame = this.getFrame(target);
    Point new_location = new Point((int) (this.start_loc.getX() + offset.getX()),
            (int) (this.start_loc.getY() + offset.getY()));
    frame.setLocation(new_location);
}

From source file:components.Framework.java

public void makeNewWindow() {
    JFrame frame = new MyFrame(this);
    numWindows++;/*from  ww w.  j av a 2  s  .co  m*/
    System.out.println("Number of windows: " + numWindows);

    if (lastLocation != null) {
        //Move the window over and down 40 pixels.
        lastLocation.translate(40, 40);
        if ((lastLocation.x > maxX) || (lastLocation.y > maxY)) {
            lastLocation.setLocation(0, 0);
        }
        frame.setLocation(lastLocation);
    } else {
        lastLocation = frame.getLocation();
    }

    System.out.println("Frame location: " + lastLocation);
    frame.setVisible(true);
}

From source file:Framework.java

public void makeNewWindow() {
    JFrame frame = new MyFrame(this);
    numWindows++;// w w w  .  j ava 2s  .co m
    System.out.println("Number of windows: " + numWindows);

    if (lastLocation != null) {
        // Move the window over and down 40 pixels.
        lastLocation.translate(40, 40);
        if ((lastLocation.x > maxX) || (lastLocation.y > maxY)) {
            lastLocation.setLocation(0, 0);
        }
        frame.setLocation(lastLocation);
    } else {
        lastLocation = frame.getLocation();
    }

    System.out.println("Frame location: " + lastLocation);
    frame.setVisible(true);
}

From source file:Main.java

void initUI() {
    GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
    int i = 1;/*  w w  w .ja  va 2  s. c  om*/
    for (GraphicsDevice gd : ge.getScreenDevices()) {
        JFrame frame = new JFrame();
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.add(createLabel(String.valueOf(i)));
        frame.getRootPane().getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW)
                .put(KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0), "exit");
        frame.getRootPane().getActionMap().put("exit", new AbstractAction() {
            @Override
            public void actionPerformed(ActionEvent e) {
                System.exit(0);
            }
        });
        frame.setLocation(gd.getDefaultConfiguration().getBounds().getLocation());
        frame.setUndecorated(true);
        frame.setExtendedState(frame.getExtendedState() | JFrame.MAXIMIZED_BOTH);
        frame.setVisible(true);
        gd.setFullScreenWindow(frame);
        i++;
    }
}

From source file:components.FrameDemo2.java

public void showNewWindow() {
    JFrame frame = new MyFrame();

    //Take care of the no window decorations case.
    //NOTE: Unless you really need the functionality
    //provided by JFrame, you would usually use a
    //Window or JWindow instead of an undecorated JFrame.
    if (noDecorations) {
        frame.setUndecorated(true);/*  ww w  . j  a  v  a 2 s.co  m*/
    }

    //Set window location.
    if (lastLocation != null) {
        //Move the window over and down 40 pixels.
        lastLocation.translate(40, 40);
        if ((lastLocation.x > maxX) || (lastLocation.y > maxY)) {
            lastLocation.setLocation(0, 0);
        }
        frame.setLocation(lastLocation);
    } else {
        lastLocation = frame.getLocation();
    }

    //Calling setIconImage sets the icon displayed when the window
    //is minimized.  Most window systems (or look and feels, if
    //decorations are provided by the look and feel) also use this
    //icon in the window decorations.
    if (specifyIcon) {
        if (createIcon) {
            frame.setIconImage(createFDImage()); //create an icon from scratch
        } else {
            frame.setIconImage(getFDImage()); //get the icon from a file
        }
    }

    //Show window.
    frame.setSize(new Dimension(170, 100));
    frame.setVisible(true);
}

From source file:FrameDemo2.java

public void showNewWindow() {
    JFrame frame = new MyFrame();

    // Take care of the no window decorations case.
    // NOTE: Unless you really need the functionality
    // provided by JFrame, you would usually use a
    // Window or JWindow instead of an undecorated JFrame.
    if (noDecorations) {
        frame.setUndecorated(true);/*from w w  w  .  jav  a 2 s.c o m*/
    }

    // Set window location.
    if (lastLocation != null) {
        // Move the window over and down 40 pixels.
        lastLocation.translate(40, 40);
        if ((lastLocation.x > maxX) || (lastLocation.y > maxY)) {
            lastLocation.setLocation(0, 0);
        }
        frame.setLocation(lastLocation);
    } else {
        lastLocation = frame.getLocation();
    }

    // Calling setIconImage sets the icon displayed when the window
    // is minimized. Most window systems (or look and feels, if
    // decorations are provided by the look and feel) also use this
    // icon in the window decorations.
    if (specifyIcon) {
        if (createIcon) {
            frame.setIconImage(createFDImage()); // create an icon from scratch
        } else {
            frame.setIconImage(getFDImage()); // get the icon from a file
        }
    }

    // Show window.
    frame.setSize(new Dimension(170, 100));
    frame.setVisible(true);
}

From source file:SuitaDetails.java

public DefPanel(String descriptions, String button, String id, int width, final int index,
        SuitaDetails container) {//from w  w  w. j a  va  2 s  .c o m
    this.descriptions = descriptions;
    this.id = id;
    reference = this;
    this.container = container;
    this.index = index;
    setBackground(new Color(255, 255, 255));
    setBorder(BorderFactory.createEmptyBorder(2, 20, 2, 20));
    setMaximumSize(new Dimension(32767, 30));
    setMinimumSize(new Dimension(100, 30));
    setPreferredSize(new Dimension(300, 30));
    setLayout(new BoxLayout(this, BoxLayout.LINE_AXIS));
    description = new JLabel(descriptions);
    description.setPreferredSize(new Dimension(width, 20));
    description.setMinimumSize(new Dimension(width, 20));
    description.setMaximumSize(new Dimension(width, 20));
    add(description);
    filedsGap = new JPanel();
    filedsGap.setBackground(new Color(255, 255, 255));
    filedsGap.setMaximumSize(new Dimension(20, 20));
    filedsGap.setMinimumSize(new Dimension(20, 20));
    filedsGap.setPreferredSize(new Dimension(20, 20));
    GroupLayout filedsGapLayout = new GroupLayout(filedsGap);
    filedsGap.setLayout(filedsGapLayout);
    filedsGapLayout.setHorizontalGroup(
            filedsGapLayout.createParallelGroup(GroupLayout.Alignment.LEADING).addGap(0, 20, Short.MAX_VALUE));
    filedsGapLayout.setVerticalGroup(
            filedsGapLayout.createParallelGroup(GroupLayout.Alignment.LEADING).addGap(0, 20, Short.MAX_VALUE));
    add(filedsGap);
    userDefinition = new JTextField();
    doclistener = new DocumentListener() {
        public void changedUpdate(DocumentEvent e) {
            setParentField(userDefinition.getText(), false);
        }

        public void removeUpdate(DocumentEvent e) {
            setParentField(userDefinition.getText(), false);
        }

        public void insertUpdate(DocumentEvent e) {
            setParentField(userDefinition.getText(), false);
        }
    };
    userDefinition.getDocument().addDocumentListener(doclistener);
    userDefinition.setText("");
    userDefinition.setMaximumSize(new Dimension(300, 100));
    userDefinition.setMinimumSize(new Dimension(50, 20));
    userDefinition.setPreferredSize(new Dimension(100, 20));
    add(userDefinition);
    filedsGap = new JPanel();
    filedsGap.setBackground(new Color(255, 255, 255));
    filedsGap.setMaximumSize(new Dimension(20, 20));
    filedsGap.setMinimumSize(new Dimension(20, 20));
    filedsGap.setPreferredSize(new Dimension(20, 20));
    filedsGapLayout = new GroupLayout(filedsGap);
    filedsGap.setLayout(filedsGapLayout);
    filedsGapLayout.setHorizontalGroup(
            filedsGapLayout.createParallelGroup(GroupLayout.Alignment.LEADING).addGap(0, 20, Short.MAX_VALUE));
    filedsGapLayout.setVerticalGroup(
            filedsGapLayout.createParallelGroup(GroupLayout.Alignment.LEADING).addGap(0, 20, Short.MAX_VALUE));
    add(filedsGap);
    if (button.equals("UserSelect")) {
        final JButton database = new JButton("Database");
        database.setMaximumSize(new Dimension(100, 20));
        database.setMinimumSize(new Dimension(50, 20));
        database.setPreferredSize(new Dimension(80, 20));
        add(database);
        database.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent ev) {
                DatabaseFrame frame = new DatabaseFrame(reference);
                frame.executeQuery();
                frame.setLocation((int) database.getLocationOnScreen().getX() - 100,
                        (int) database.getLocationOnScreen().getY());
                frame.setVisible(true);
            }
        });
    } else if (button.equals("UserScript")) {
        JButton script = new JButton("Script");
        script.setMaximumSize(new Dimension(100, 20));
        script.setMinimumSize(new Dimension(50, 20));
        script.setPreferredSize(new Dimension(80, 20));
        add(script);
        script.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent ev) {
                Container c;
                if (RunnerRepository.container != null)
                    c = RunnerRepository.container.getParent();
                else
                    c = RunnerRepository.window;
                try {
                    //                         String passwd = RunnerRepository.getRPCClient().execute("sendFile", new Object[]{"/etc/passwd"}).toString();
                    //                         new MySftpBrowser(RunnerRepository.host,RunnerRepository.user,RunnerRepository.password,userDefinition,c,passwd);
                    new MySftpBrowser(RunnerRepository.host, RunnerRepository.user, RunnerRepository.password,
                            userDefinition, c, false);
                } catch (Exception e) {
                    System.out.println("There was a problem in opening sftp browser!");
                    e.printStackTrace();
                }
            }
        });
        filedsGap = new JPanel();
        filedsGap.setBackground(new Color(255, 255, 255));
        filedsGap.setMaximumSize(new Dimension(10, 10));
        filedsGap.setMinimumSize(new Dimension(10, 10));
        filedsGap.setPreferredSize(new Dimension(10, 10));
        filedsGapLayout = new GroupLayout(filedsGap);
        filedsGap.setLayout(filedsGapLayout);
        filedsGapLayout.setHorizontalGroup(filedsGapLayout.createParallelGroup(GroupLayout.Alignment.LEADING)
                .addGap(0, 20, Short.MAX_VALUE));
        filedsGapLayout.setVerticalGroup(filedsGapLayout.createParallelGroup(GroupLayout.Alignment.LEADING)
                .addGap(0, 20, Short.MAX_VALUE));
        filedsGap.setLayout(filedsGapLayout);
        add(filedsGap);
        final JButton value = new JButton("Value");
        value.setMaximumSize(new Dimension(100, 20));
        value.setMinimumSize(new Dimension(50, 20));
        value.setPreferredSize(new Dimension(80, 20));
        add(value);
        value.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent ev) {
                String script = userDefinition.getText();
                if (script != null && !script.equals("")) {
                    try {
                        String result = RunnerRepository.getRPCClient().execute("runUserScript",
                                new Object[] { script }) + "";
                        JFrame f = new JFrame();
                        f.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
                        f.setLocation(value.getLocationOnScreen());
                        JLabel l = new JLabel("Script result: " + result);
                        f.getContentPane().add(l, BorderLayout.CENTER);
                        f.pack();
                        f.setVisible(true);
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
            }
        });
    } else if (button.equals("UserText")) {
        JPanel database = new JPanel();
        database.setBackground(Color.WHITE);
        database.setMaximumSize(new Dimension(100, 20));
        database.setMinimumSize(new Dimension(50, 20));
        database.setPreferredSize(new Dimension(80, 20));
        add(database);
    }
}

From source file:org.openscience.jmol.app.Jmol.java

Jmol(Splash splash, JFrame frame, Jmol parent, int startupWidth, int startupHeight, String commandOptions,
        Point loc) {//from   www . jav  a  2s . c  o  m
    super(true);
    this.frame = frame;
    this.startupWidth = startupWidth;
    this.startupHeight = startupHeight;
    numWindows++;

    try {
        say("history file is " + historyFile.getFile().getAbsolutePath());
    } catch (Exception e) {
    }

    frame.setTitle("Jmol");
    frame.getContentPane().setBackground(Color.lightGray);
    frame.getContentPane().setLayout(new BorderLayout());

    this.splash = splash;

    setBorder(BorderFactory.createEtchedBorder());
    setLayout(new BorderLayout());
    language = GT.getLanguage();

    status = (StatusBar) createStatusBar();
    say(GT._("Initializing 3D display..."));
    //
    display = new DisplayPanel(status, guimap, haveDisplay.booleanValue(), startupWidth, startupHeight);
    String adapter = System.getProperty("model");
    if (adapter == null || adapter.length() == 0)
        adapter = "smarter";
    if (adapter.equals("smarter")) {
        report("using Smarter Model Adapter");
        modelAdapter = new SmarterJmolAdapter();
    } else if (adapter.equals("cdk")) {
        report("the CDK Model Adapter is currently no longer supported. Check out http://bioclipse.net/. -- using Smarter");
        // modelAdapter = new CdkJmolAdapter(null);
        modelAdapter = new SmarterJmolAdapter();
    } else {
        report("unrecognized model adapter:" + adapter + " -- using Smarter");
        modelAdapter = new SmarterJmolAdapter();
    }
    appletContext = commandOptions;
    viewer = JmolViewer.allocateViewer(display, modelAdapter);
    viewer.setAppletContext("", null, null, commandOptions);

    if (display != null)
        display.setViewer(viewer);

    say(GT._("Initializing Preferences..."));
    preferencesDialog = new PreferencesDialog(frame, guimap, viewer);
    say(GT._("Initializing Recent Files..."));
    recentFiles = new RecentFilesDialog(frame);
    if (haveDisplay.booleanValue()) {
        say(GT._("Initializing Script Window..."));
        scriptWindow = new ScriptWindow(viewer, frame);
    }

    MyStatusListener myStatusListener;
    myStatusListener = new MyStatusListener();
    viewer.setJmolStatusListener(myStatusListener);

    say(GT._("Initializing Measurements..."));
    measurementTable = new MeasurementTable(viewer, frame);

    // Setup Plugin system
    // say(GT._("Loading plugins..."));
    // pluginManager = new CDKPluginManager(
    //     System.getProperty("user.home") + System.getProperty("file.separator")
    //     + ".jmol", new JmolEditBus(viewer)
    // );
    // pluginManager.loadPlugin("org.openscience.cdkplugin.dirbrowser.DirBrowserPlugin");
    // pluginManager.loadPlugin("org.openscience.cdkplugin.dirbrowser.DadmlBrowserPlugin");
    // pluginManager.loadPlugins(
    //     System.getProperty("user.home") + System.getProperty("file.separator")
    //     + ".jmol/plugins"
    // );
    // feature to allow for globally installed plugins
    // if (System.getProperty("plugin.dir") != null) {
    //     pluginManager.loadPlugins(System.getProperty("plugin.dir"));
    // }

    if (haveDisplay.booleanValue()) {

        // install the command table
        say(GT._("Building Command Hooks..."));
        commands = new Hashtable();
        if (display != null) {
            Action[] actions = getActions();
            for (int i = 0; i < actions.length; i++) {
                Action a = actions[i];
                commands.put(a.getValue(Action.NAME), a);
            }
        }

        menuItems = new Hashtable();
        say(GT._("Building Menubar..."));
        executeScriptAction = new ExecuteScriptAction();
        menubar = createMenubar();
        add("North", menubar);

        JPanel panel = new JPanel();
        panel.setLayout(new BorderLayout());
        panel.add("North", createToolbar());

        JPanel ip = new JPanel();
        ip.setLayout(new BorderLayout());
        ip.add("Center", display);
        panel.add("Center", ip);
        add("Center", panel);
        add("South", status);

        say(GT._("Starting display..."));
        display.start();

        //say(GT._("Setting up File Choosers..."));

        /*      pcs.addPropertyChangeListener(chemFileProperty, exportAction);
         pcs.addPropertyChangeListener(chemFileProperty, povrayAction);
         pcs.addPropertyChangeListener(chemFileProperty, writeAction);
         pcs.addPropertyChangeListener(chemFileProperty, toWebAction);
         pcs.addPropertyChangeListener(chemFileProperty, printAction);
         pcs.addPropertyChangeListener(chemFileProperty,
         viewMeasurementTableAction);
         */

        if (menuFile != null) {
            menuStructure = viewer.getFileAsString(menuFile);
        }
        jmolpopup = JmolPopup.newJmolPopup(viewer, true, menuStructure, true);

    }

    // prevent new Jmol from covering old Jmol
    if (loc != null) {
        frame.setLocation(loc);
    } else if (parent != null) {
        Point location = parent.frame.getLocationOnScreen();
        int maxX = screenSize.width - 50;
        int maxY = screenSize.height - 50;

        location.x += 40;
        location.y += 40;
        if ((location.x > maxX) || (location.y > maxY)) {
            location.setLocation(0, 0);
        }
        frame.setLocation(location);
    }
    frame.getContentPane().add("Center", this);

    frame.addWindowListener(new Jmol.AppCloser());
    frame.pack();
    frame.setSize(startupWidth, startupHeight);
    ImageIcon jmolIcon = JmolResourceHandler.getIconX("icon");
    Image iconImage = jmolIcon.getImage();
    frame.setIconImage(iconImage);

    // Repositionning windows
    if (scriptWindow != null)
        historyFile.repositionWindow(SCRIPT_WINDOW_NAME, scriptWindow, 200, 100);

    say(GT._("Setting up Drag-and-Drop..."));
    FileDropper dropper = new FileDropper();
    final JFrame f = frame;
    dropper.addPropertyChangeListener(new PropertyChangeListener() {
        public void propertyChange(PropertyChangeEvent evt) {
            //System.out.println("Drop triggered...");
            f.setCursor(Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR));
            if (evt.getPropertyName().equals(FileDropper.FD_PROPERTY_FILENAME)) {
                final String filename = evt.getNewValue().toString();
                viewer.openFile(filename);
            } else if (evt.getPropertyName().equals(FileDropper.FD_PROPERTY_INLINE)) {
                final String inline = evt.getNewValue().toString();
                viewer.openStringInline(inline);
            }
            f.setCursor(Cursor.getDefaultCursor());
        }
    });

    this.setDropTarget(new DropTarget(this, dropper));
    this.setEnabled(true);

    say(GT._("Launching main frame..."));
}