0

So I have this weird thing happening to one of my JFrames in my Java app, it seems like after I reopen the layout the positioning of my components just change out of no where. I'm used to work with springLayout to position my components, and out of about 12 frames in my application this is the first time I see something like this happening.

This is my code:

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.text.DecimalFormat;
import java.text.NumberFormat;

public class Edits extends JFrame implements ActionListener{
    //change price
    JLabel currentPrice = new JLabel("Current Price");
    JLabel changePrice = new JLabel("New Price");
    JFormattedTextField oldprice = new JFormattedTextField(new DecimalFormat("#0.000"));
    JFormattedTextField newprice = new JFormattedTextField(new DecimalFormat("#0.000"));
    JButton okPrice = new JButton("Save Changes");
    JButton closePrice = new JButton("Close");

    //change user info
    JLabel currentUser = new JLabel();
    JLabel changeUser = new JLabel();
    JLabel currentPass = new JLabel();
    JLabel changePass = new JLabel();
    JLabel confirmPass = new JLabel();
    JTextField oldUser = new JTextField();
    JTextField newUser = new JTextField();
    JPasswordField oldPass = new JPasswordField();
    JPasswordField newPass = new JPasswordField();
    JPasswordField confirmNewPass = new JPasswordField();
    JButton okUser = new JButton("Save Changes");
    JButton closeUser = new JButton("Close");

    //change purchased quantity
    JLabel currentQte = new JLabel();
    JLabel changeQte = new JLabel();
    JFormattedTextField oldQte = new JFormattedTextField(NumberFormat.getIntegerInstance());
    JFormattedTextField newQte = new JFormattedTextField(NumberFormat.getIntegerInstance());
    JButton okQte = new JButton("Save Changes");
    JButton closeQte = new JButton("Close");

    public Edits() {
        super();
    }

    public void changePrice() {
        //init frame
        this.setTitle("Chnage Medicine's Price");
        this.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        JFrame.setDefaultLookAndFeelDecorated(false);
        Dimension dim = Toolkit.getDefaultToolkit().getScreenSize();
        this.setLocation(dim.width/6-this.getSize().width/2, dim.height/16-this.getSize().height/2);
        this.setSize(350, 170);
        this.setResizable(false);
        this.setVisible(true);
        //init sizes
        Dimension labels = new Dimension(100,25);
        Dimension inputs = new Dimension(200,25);
        Dimension btn = new Dimension(80,35);
        Dimension btn1 = new Dimension(120,35);
        //Labels size
        currentPrice.setSize(labels);
        currentPrice.setMaximumSize(labels);
        currentPrice.setMinimumSize(labels);
        currentPrice.setPreferredSize(labels);
        changePrice.setSize(labels);
        changePrice.setMaximumSize(labels);
        changePrice.setMinimumSize(labels);
        changePrice.setPreferredSize(labels);
        //inputs size
        oldprice.setSize(inputs);
        oldprice.setMaximumSize(inputs);
        oldprice.setMinimumSize(inputs);
        oldprice.setPreferredSize(inputs);
        newprice.setSize(inputs);
        newprice.setMaximumSize(inputs);
        newprice.setMinimumSize(inputs);
        newprice.setPreferredSize(inputs);
        //button sizes
        okPrice.setSize(btn1);
        okPrice.setMaximumSize(btn1);
        okPrice.setMinimumSize(btn1);
        okPrice.setPreferredSize(btn1);
        closePrice.setSize(btn);
        closePrice.setMaximumSize(btn);
        closePrice.setMinimumSize(btn);
        closePrice.setPreferredSize(btn);
        this.add(currentPrice);
        this.add(oldprice);
        this.add(changePrice);
        this.add(newprice);
        this.add(okPrice);
        this.add(closePrice);
        //set layout
        SpringLayout layout = new SpringLayout();
        this.setLayout(layout);
        //old label
        layout.putConstraint(SpringLayout.NORTH, currentPrice, 15, SpringLayout.NORTH, this);
        layout.putConstraint(SpringLayout.WEST, currentPrice, 20, SpringLayout.WEST, this);
        //old input
        layout.putConstraint(SpringLayout.NORTH, oldprice, 15, SpringLayout.NORTH, this);
        layout.putConstraint(SpringLayout.WEST, oldprice, 10, SpringLayout.EAST, currentPrice);
        //new label
        layout.putConstraint(SpringLayout.NORTH, changePrice, 15, SpringLayout.SOUTH, currentPrice);
        layout.putConstraint(SpringLayout.WEST, changePrice, 20, SpringLayout.WEST, this);
        //new input
        layout.putConstraint(SpringLayout.NORTH, newprice, 15, SpringLayout.SOUTH, currentPrice);
        layout.putConstraint(SpringLayout.WEST, newprice, 10, SpringLayout.EAST, changePrice);
        //OK 
        layout.putConstraint(SpringLayout.NORTH, okPrice, 15, SpringLayout.SOUTH, newprice);
        layout.putConstraint(SpringLayout.WEST, okPrice, 65, SpringLayout.WEST, this);
        //close
        layout.putConstraint(SpringLayout.NORTH, closePrice, 15, SpringLayout.SOUTH, newprice);
        layout.putConstraint(SpringLayout.WEST, closePrice, 20, SpringLayout.EAST, okPrice);
    }

    @Override
    public void actionPerformed(ActionEvent e) {

    }

}

This is an image of the layout I want:

the layout i want

And this is what keeps happening:

a weird layout

another weird layout

Does anyone know what's wrong?

fin444
  • 725
  • 2
  • 14
  • 27
chawki challadia
  • 434
  • 7
  • 19
  • 1
    1) All Swing/AWT GUIs should be started on the EDT. 2) Setting the frame visible should be done after adding all components and calling `pack()` on the frame. 3) See [Should I avoid the use of set(Preferred|Maximum|Minimum)Size methods in Java Swing?](http://stackoverflow.com/q/7229226/418556) (Yes.) Sizes for many text components can be suggested by specifying the number of columns & (if applicable) rows . 4) `this.setLocation(dim.width/6-this.getSize().width/2, dim.height/16-this.getSize().height/2);` that mess would be better replaced by `setLocationRelativeTo(null)` .. – Andrew Thompson Feb 02 '18 at 01:46
  • .. or `setLocationByPlatform(true)` – Andrew Thompson Feb 02 '18 at 01:47
  • the location is just to center it in the middle of the screen and as far as calling before and after i've done almost the same order of code in this class as about 12 others yet no problems occured, that's what's puzzling me – chawki challadia Feb 02 '18 at 02:49
  • 1
    *"the location is just to center it in the middle of the screen"* If you'd checked the docs on the first method I mentioned, you'd find it does the same thing, a lot shorter. *"and as far as calling before and after i've done almost the same order of code in this class as about 12 others yet no problems occured"* That's the problem with transitory errors, sometimes it works, sometimes it doesn't. Have you actually (even once) tried my suggestion? I'm beginning to feel I'm wasting my time trying to help you. – Andrew Thompson Feb 02 '18 at 08:21

0 Answers0