0

I am trying to create a autoclicker but i encountered a random problem.
I have make the "Exit" button to spawn on the bottom right by using .getContentPane().getSize()
but on my "Basic" Jframe that value some times changes (i have added a system print when it opens.
The first value is java.awt.Dimension[width=284,height=462]
and the second is java.awt.Dimension[width=294,height=472] (this is the correct one)
Can someone explain why this happens because i got my mind blown.
I am using NetBeans.
Here is not my full code:
Main

/*
 * To change this license header, choose License Headers in Project 
Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
 package test;

 import java.awt.AWTException;
 import java.awt.Color;
 import java.awt.Dimension;
 import java.awt.Toolkit;
 import java.awt.event.ActionEvent;
 import java.awt.event.ActionListener;
 import java.io.IOException;
 import javax.swing.JButton;
 import javax.swing.JComponent;
 import javax.swing.JFrame;
 import javax.swing.JPanel;

 /**
  *
  * @author Xaralabos
  */
 public class Test {

/**
 * @param args the command line arguments
 */



static void placeAtThe (JComponent a, String where, Dimension framesize){

    if((where == "bottom")||(where == "Bottom")||(where == "bot")||(where == "Bot")){
        a.setLocation(a.getX(),(framesize.height - a.getHeight()));
    }
    else if ((where == "right")||(where == "Right")){
        a.setLocation(framesize.width - a.getWidth(), a.getY());
    }
}

    // Center Align - Width (needs width of frame and of component)
static int CW (int framew, int compw){
    int c = (((framew) / 2) - (compw / 2));
    return c;
}


// CREATE NEW FRAME FOR Basic Clicker
 static class BasicClickerFrame implements ActionListener{
private JFrame Homef;
private boolean StopCopies;

public BasicClickerFrame(JFrame Homef){
    this.Homef = Homef;
    StopCopies = false;
}

public void actionPerformed(ActionEvent e) {
    Homef.dispose();
    if (StopCopies == false){
        StopCopies = true;
        Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
        double width = screenSize.getWidth();
        double height = screenSize.getHeight();
        int basicw = 300;
        int basich = 500;

        // FRAME , SIZE - SPAWN LOCATION
        JFrame Basic = new JFrame("Basic Auto Clicker");
        Basic.setResizable(false);
        Basic.setSize(basicw, basich);
        Basic.setLocation(((int) (width / 4)) - (Basic.getWidth() / 2), ((int) height / 10));
        Basic.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);


            // COLORING THE BACKGROUND
        JPanel contentPane = new JPanel();
        contentPane.setBackground(Color.WHITE);
        contentPane.setLayout(null);
            // add more code here
        Basic.setContentPane(contentPane);


            // EXIT BUTTON (CLOSES WINDOW, NOT PROGRAM)
        JButton Exit = new JButton("Exit");
        Exit.setSize(Exit.getPreferredSize());
            // add more code here
        contentPane.add(Exit);


            // button for getting keybind for starting
        JButton Getme = new JButton("Click Me");
        Getme.setSize(Getme.getPreferredSize());
            // add more code here
        contentPane.add(Getme);


        // SPAWN EVERYTHING
        Basic.setVisible(true);

        // ADD LOCATIONS HERE
        placeAtThe(Exit, "right", Basic.getContentPane().getSize());
        placeAtThe(Exit, "bot", Basic.getContentPane().getSize());
        Getme.setLocation(CW(Basic.getContentPane().getSize().width, Getme.getWidth()), 50);
        System.out.println(Basic.getContentPane().getSize());
    }
   }
  }


public static void main(String[] args) throws IOException, AWTException {
    int frame1w = 600;
    int frame1h = 400;
        // MAIN WINDOW - SIZE - SPAWN LOCATION
    JFrame Homef = new JFrame("Auto Clicker");
    Homef.setResizable(false); // stops resizing
    Homef.setSize(frame1w, frame1h);
    Homef.setLocationRelativeTo(null);
    Homef.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);


    // COLORING BACKGROUND
    JPanel contentPane = new JPanel();
    contentPane.setBackground(Color.WHITE);
    contentPane.setLayout(null);
    // Add more code here
    Homef.setContentPane(contentPane);


    // EXIT BUTTON (CLOSES PROGRAM)
    JButton Exit = new JButton("Exit");
    Exit.setSize(Exit.getPreferredSize());
        // add code here
    contentPane.add(Exit);

        // BUTTON FOR CREATING A NEW FRAME FOR Basic AUTO CLICKER
    JButton BasicClicker = new JButton("Basic Clicker");
    BasicClicker.setSize(BasicClicker.getPreferredSize());
    BasicClicker.addActionListener(new BasicClickerFrame(Homef));
    // ADD MORE STUFF HERE
    contentPane.add(BasicClicker);

    Homef.setVisible(true);

    // ADD LOCATIONS HERE
    placeAtThe(Exit, "right", Homef.getContentPane().getSize());
    placeAtThe(Exit, "bot", Homef.getContentPane().getSize());
    BasicClicker.setLocation(CW(Homef.getContentPane().getSize().width, BasicClicker.getWidth()), BasicClicker.getY());   
  }
 }  

NOTE i tried removing the first frame and just open the basic one right away.
When i did, the problem is fixed(i think,i runned it like 30 times without the problem appearing) but yeah... i dont know why....HELP!

  • Instead of `Homef.setLocation(...)` use `JFrame#setLocationRelativeTo` and pass it `null` - it will centre the window within the screen taking to account other UI elements, like the task bar – MadProgrammer Jul 05 '17 at 00:29
  • 1
    A generally better solution would be make use of appropriate layout managers and possibly the `glassPane` – MadProgrammer Jul 05 '17 at 00:31
  • thanks for the tip :P ... but my problem isnt this – Xaralabos Nikolaidhs Jul 05 '17 at 00:32
  • Neither are they answers – MadProgrammer Jul 05 '17 at 00:33
  • `contentPane.setLayout(null);` 1) Java GUIs have to work on different OS', screen size, screen resolution etc. using different PLAFs in different locales. As such, they are not conducive to pixel perfect layout. Instead use layout managers, or [combinations of them](http://stackoverflow.com/a/5630271/418556) along with layout padding and borders for [white space](http://stackoverflow.com/a/17874718/418556). 2) For better help sooner, post a [MCVE] or [Short, Self Contained, Correct Example](http://www.sscce.org/). – Andrew Thompson Jul 05 '17 at 00:45
  • Also note, calling `setResizable` can change the size of the frame, which will affect the size of the `contentPane` – MadProgrammer Jul 05 '17 at 00:48
  • yeah, thats why i am using contentpanel.getsize ... i realized by using it, i can place the components where i want compare to just .getsize – Xaralabos Nikolaidhs Jul 05 '17 at 00:56
  • i edited, removed whatever could think of, problem still works – Xaralabos Nikolaidhs Jul 05 '17 at 00:59
  • There are other ways to get the location of components within a layout, from memory you might be able to use the accessibility API or walk the component hierarchy looking for certain conditions (like the type, name or other properties) - just saying – MadProgrammer Jul 05 '17 at 00:59
  • Swap `Basic.setSize(basicw, basich); Basic.setResizable(false);` to be `Basic.setResizable(false); Basic.setSize(basicw, basich);` which will reduce the risk of the call to `setResizable` changing the size of the window – MadProgrammer Jul 05 '17 at 01:00
  • did it.. problem still there... after like 10 tries – Xaralabos Nikolaidhs Jul 05 '17 at 01:02
  • `where == "down"` :/ – MadProgrammer Jul 05 '17 at 01:09
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/148364/discussion-between-xaralabos-nikolaidhs-and-madprogrammer). – Xaralabos Nikolaidhs Jul 05 '17 at 01:10

0 Answers0