1

Iám facing a litte problem here.

I tried to save the actual position of an jFrame, and restore this position after creating this frame again.

Heres the "save"-procedure:

private void formWindowClosing(java.awt.event.WindowEvent evt) {                                   
    Parent.Throttle_vorhanden = 0;
    root.Hauptfenster.LastPositionThrottlePlate=this.getLocation();
    this.dispose();
} 

And here I try to set the old location again:

public Throttle(Hauptfenster parent, root.rs232.rs232Connection Conn, Integer throttlePosition) {
    super();
    Parent = parent;
    conn = Conn;
    actualThrottlePosition = throttlePosition;
    initComponents();

    jScrollBar2.setValue(root.Hauptfenster.MPR);
    this.setTitle("Throttle");

    this.setVisible(true);
    if (root.Hauptfenster.LastPositionThrottlePlate!=null) {
        this.setLocation(root.Hauptfenster.LastPositionThrottlePlate); 
    }

For a very little bit a secound, I see the Frame on its old position, but then it "jumps" in the middle of the screen...

Can anyone imagine why jFrame behavior like this? sorry for my english...

Java Man
  • 1,854
  • 3
  • 21
  • 43
ÂlexBay
  • 103
  • 9

3 Answers3

0

Set the JFrame as visible after you have correctly positioned it.

It is just a matter of inverting the order of your lastoperations:

if (root.Hauptfenster.LastPositionThrottlePlate!=null) {
    this.setLocation(root.Hauptfenster.LastPositionThrottlePlate); 
}

this.setVisible(true);
Bruno Toffolo
  • 1,504
  • 19
  • 24
0

It's probably because you're displaying your window before moving the JFrame.

Your code says:

this.setVisible(true);
if (root.Hauptfenster.LastPositionThrottlePlate!=null) {
    this.setLocation(root.Hauptfenster.LastPositionThrottlePlate); 
}

I would suggest reversing the order:

if (root.Hauptfenster.LastPositionThrottlePlate!=null) {
    this.setLocation(root.Hauptfenster.LastPositionThrottlePlate); 
}
this.setVisible(true);

This will set it to it's "final" position before showing it.

ashes999
  • 9,925
  • 16
  • 73
  • 124
0

Why don't you just setLocationRelativeTo(parent) instead of trying to set the location manually.


Side Notes:

  • Use Java naming convention. variable names start with lowecase letters. I see you have Parent and parent. That's where this comes in handy

    this.parent = parent
    

    instead of

    Parent = parent
    
  • It looks like you're trying to use two JFrame. Why? There other more user friendly options like a model JDialog or a CardLayout. See The Use of Multiple JFrames, Good/Bad Practice?


UPDATE

Though I'm against the use of multiple frames, I will still give you an answer, Only because I took my time to actually test this out. Here are the steps I took in GUI Builder, which it looks like that's what you're using.

  1. I made two JFrame forms FrameOne and FrameTwo.
  2. I changed the defaultCloseOperation of FrameOne to DISPOSE
  3. In FrameTwo I deleted the main method because the application is already running from the FrameOne class. I kept the look and feel code though. Just cop and pasted it to the constructor.
  4. I Did this, set the location relative to the first frame

    FrameOne frameOne;
    
    public FrameTwo(FrameOne frameOne) {
        initComponents();
        this.frameOne = frameOne;
        // look and feel code here
        setLocationRelativeTo(frameOne);
        setVisible(true);
    }
    
  5. Added a WindowListener to the FrameOne

    public FrameOne() {
        initComponents();
    
        addWindowListener(new WindowAdapter(){
            @Override
            public void windowClosing(WindowEvent e) {
                new FrameTwo(FrameOne.this);
                FrameOne.this.dispose();
           }
        });
    }
    

It works fine.


FrameOne.java

UPATE with code from GU Builder

I added no components to FrameOne, just close the frame. The only things I did was what I mentioned in the steps above

import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import javax.swing.GroupLayout;
import javax.swing.WindowConstants;

public class FrameOne extends javax.swing.JFrame {

    public FrameOne() {
        initComponents();

        addWindowListener(new WindowAdapter() {
            @Override
            public void windowClosing(WindowEvent e) {
                new FrameTwo(FrameOne.this);
                FrameOne.this.dispose();
            }
        });
    }

    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">                          
    private void initComponents() {

        setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);

        GroupLayout layout = new GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(GroupLayout.Alignment.LEADING)
            .addGap(0, 400, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(GroupLayout.Alignment.LEADING)
            .addGap(0, 300, Short.MAX_VALUE)
        );

        pack();
    }// </editor-fold>                        

    public static void main(String args[]) {

        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(FrameOne.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(FrameOne.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(FrameOne.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(FrameOne.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new FrameOne().setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify                     
    // End of variables declaration                   
}

FrameTwo.java

import java.awt.Font;
import javax.swing.GroupLayout;
import javax.swing.JLabel;
import javax.swing.SwingConstants;
import javax.swing.WindowConstants;


public class FrameTwo extends javax.swing.JFrame {

    private FrameOne frameOne;

    public FrameTwo(final FrameOne frameOne) {
        initComponents();
        this.frameOne = frameOne;
        setLocationRelativeTo(frameOne);
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(FrameTwo.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(FrameTwo.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(FrameTwo.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(FrameTwo.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        setVisible(true);
    }

    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">                          
    private void initComponents() {

        jLabel1 = new JLabel();

        setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);

        jLabel1.setFont(new Font("Impact", 0, 36)); // NOI18N
        jLabel1.setHorizontalAlignment(SwingConstants.CENTER);
        jLabel1.setText("frame two");

        GroupLayout layout = new GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(118, 118, 118)
                .addComponent(jLabel1, GroupLayout.PREFERRED_SIZE, 161, GroupLayout.PREFERRED_SIZE)
                .addContainerGap(121, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(90, 90, 90)
                .addComponent(jLabel1, GroupLayout.PREFERRED_SIZE, 89, GroupLayout.PREFERRED_SIZE)
                .addContainerGap(121, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>                        



        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */

        //</editor-fold>



    // Variables declaration - do not modify                     
    private JLabel jLabel1;
    // End of variables declaration                   
}

enter image description here

Community
  • 1
  • 1
Paul Samsotha
  • 205,037
  • 37
  • 486
  • 720
  • I need the jFrame in exact this position again. So i have to set the location manually. – ÂlexBay Jan 30 '14 at 16:16
  • Exact position as what? The frame before? – Paul Samsotha Jan 30 '14 at 16:17
  • Yes, thats why I save the location on private void formWindowClosing(java.awt.event.WindowEvent evt) – ÂlexBay Jan 30 '14 at 16:19
  • Thank you for your effort, but unfortunately this did not solve my problem. For clearance: If the user opens the secound frame, and drag it to the upper right corner of the screen, after closing and reopen the frame, it should be opened on the same location. In my example, i see the secound frame for a bit of a secound on the correct location an then it jumps back to the middle of the screen. Side note: User friendlyness is not my problem. If i have a problem on starting my car, the hint "why not take the bike?" is not very helpful. – ÂlexBay Jan 31 '14 at 10:37
  • I'll post the code I used. It works fine for me. The dragging and all. – Paul Samsotha Jan 31 '14 at 11:19
  • Wait, you mean you want to save the location, it was closed from, then when you open again, it will be in the same location? So you're not trying to close one frame and have to other open in same location as the first frame? I thought that's what you meant. – Paul Samsotha Jan 31 '14 at 11:35
  • Yes you are right now, this is exactly what I meant. Sorry for the misunderstanding. And have a big thank for your effort! – ÂlexBay Jan 31 '14 at 13:47