Mudkip
Mudkip

Reputation: 373

Getting new bound values from JFrame (Java)

I am using the following code to test the bound values of a JFrame of mine:

Rectangle r1 = new Rectangle();
Rectangle r2 = new Rectangle();
r1 = this.getBounds();
this.setExtendedState(JFrame.MAXIMIZED_BOTH); //this == JFrame
r2 = this.getBounds();

At first, I thought that the values of r2 (regarding x, y, width and height) would be different, since the frame is automatically resized. However, the variables r1 and r2 seems to be (almost) equal, according to the Debugger.

Why does this happen? What should I do in order to get the new values of the maximized JFrame?

import java.awt.Color;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Graphics;
import java.awt.Insets;
import java.awt.List;
import java.awt.Rectangle;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
import javax.swing.BorderFactory;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.border.Border;
import javax.swing.border.EmptyBorder;

public class NewGUI extends JFrame {

    private JPanel contentPane;
    private List list;
    private JButton btnSelectAgent;
    private JTextField textField;
    private JButton btnBrowse;
    private JButton btnPreviousStep2;
    private JButton btnLoad;
    private JButton btnPreviousStep3;
    private JButton btnStart;
    private File file = null;
    ImageIcon icon = new ImageIcon("image.gif");
    Border blackline = BorderFactory.createLineBorder(Color.black);
    private JPanel firstStepPanel;
    private JPanel secondStepPanel;
    private JPanel thirdStepPanel;

    /**
     * Launch the application.
     */
    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    NewGUI frame = new NewGUI();
                    frame.setVisible(true);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }

    /**
     * Create the frame.
     */
    public NewGUI() {
        super("GUI");
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setBounds(100, 100, 681, 422);
        contentPane = new JPanel();
        contentPane.setForeground(Color.BLACK);
        contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
        setContentPane(contentPane);
        setLocationRelativeTo(null); //centered location
        contentPane.setLayout(null);

        //test codes begin
        Insets insets = this.getInsets();
        Rectangle r = new Rectangle();
        Dimension d = new Dimension();
        r.x = 0;
        r.y = 0;
        r.width = 1280;
        r.height = 1024;
        r = this.getMaximizedBounds();
        d = this.getMaximumSize();
        Rectangle r1 = new Rectangle();
        Rectangle r2 = new Rectangle();
        Dimension d1 = new Dimension();
        Dimension d2 = new Dimension();
        d1 = this.getSize();
        r1 = this.getBounds();
        this.setExtendedState(JFrame.MAXIMIZED_BOTH);
        r = this.getMaximizedBounds();
        r2 = r1;
        this.setMaximizedBounds(r);
        r2.width = this.getWidth();
        r2.height = this.getHeight();
        d2 = this.getSize();

        //test codes end
        /*        pane.setLayout(null); 
         JButton b1 = new JButton("one"); 
         JButton b2 = new JButton("two"); 
         JButton b3 = new JButton("three"); 
         pane.add(b1); 
         pane.add(b2); 
         pane.add(b3); 
         JPanel pane = new JPanel();

         Insets insets = this.getInsets();
         Rectangle r = new Rectangle();
         r.x = insets.left;
         r.y = insets.top;



         JFrame frame = new JFrame();
         frame.getInsets();
         Dimension size = b1.getPreferredSize();
         b1.setBounds(25 + insets.left, 5 + insets.top, size.width, size.height);
         Rectangle r = new Rectangle();
         r.x = 1;
         r.y = 2;
         b1.setBounds
         size = b2.getPreferredSize();
         b2.setBounds(55 + insets.left, 40 + insets.top, size.width, size.height); 
         size = b3.getPreferredSize(); 
         b3.setBounds(150 + insets.left, 15 + insets.top, size.width + 50, size.height + 20);
         ...//In the main method: 
         Insets insets = frame.getInsets(); 
         frame.setSize(300 + insets.left + insets.right, 125 + insets.top + insets.bottom);        

         */
        firstStepPanel = new JPanel();
        firstStepPanel.setBounds(10, 10, 194, 363);
        contentPane.add(firstStepPanel);
        //this.add(firstStepPanel, BorderLayout.WEST);
        firstStepPanel.setLayout(null);
        firstStepPanel.setBorder(
                BorderFactory.createTitledBorder(blackline, "Agent selection"));

        //makeFrameFullSize(contentPane); //aparentemente nao funcionando
        list = new List();
        list.setBounds(10, 31, 169, 203);
        firstStepPanel.add(list);
        list.setMultipleSelections(true);

        btnSelectAgent = new JButton("Select Agent");
        btnSelectAgent.setBounds(10, 274, 169, 34);
        firstStepPanel.add(btnSelectAgent);

        secondStepPanel = new JPanel();
        secondStepPanel.setBorder(
                BorderFactory.createTitledBorder(blackline, "file selection"));
        secondStepPanel.setBounds(214, 11, 266, 362);
        contentPane.add(secondStepPanel);
        //this.add(secondStepPanel, BorderLayout.CENTER);
        secondStepPanel.setLayout(null);

        btnLoad = new JButton("Load");
        btnLoad.setBounds(78, 317, 118, 34);
        secondStepPanel.add(btnLoad);
        btnLoad.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent arg0) {
                if (file == null) {
                    showMessage("Please, select a file on the upper options");
                } else {
                    disableSecondStep();
                    enableThirdStep();
                }

            }
        });
        btnLoad.setEnabled(false);

        textField = new JTextField();
        textField.setBounds(10, 29, 157, 34);
        secondStepPanel.add(textField);
        textField.setEnabled(false);
        textField.setColumns(10);
        textField.setText("P:\\\\");

        btnBrowse = new JButton("Browse");
        btnBrowse.setBounds(167, 29, 89, 35);
        secondStepPanel.add(btnBrowse);
        btnBrowse.setEnabled(false);

        btnPreviousStep2 = new JButton("<< Previous");
        btnPreviousStep2.setBounds(78, 272, 118, 34);
        secondStepPanel.add(btnPreviousStep2);
        btnPreviousStep2.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                enableFirstStep();
                disableSecondStep();
            }
        });
        btnPreviousStep2.setEnabled(false);

        thirdStepPanel = new JPanel();
        thirdStepPanel.setBorder(
                BorderFactory.createTitledBorder(blackline, "Process initialization"));
        thirdStepPanel.setBounds(490, 10, 165, 362);
        contentPane.add(thirdStepPanel);
        //this.add(thirdStepPanel, BorderLayout.EAST);
        thirdStepPanel.setLayout(null);
        //thirdStepPanel.setSize(100, r.height);
        //r.x += 100;
        //thirdStepPanel.setBounds(r);
        //thirdStepPanel.setBounds(r.x,r.y, r.width, r.height);
        //thirdStepPanel.setAlignmentX(r.x);
        //thirdStepPanel.setAlignmentY(r.y);

        btnPreviousStep3 = new JButton("<< Previous");
        btnPreviousStep3.setBounds(7, 56, 151, 34);
        thirdStepPanel.add(btnPreviousStep3);
        btnPreviousStep3.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                enableSecondStep();
                disableThirdStep();
            }
        });
        btnPreviousStep3.setEnabled(false);

        btnStart = new JButton(icon);
        btnStart.setBounds(7, 101, 151, 159);
        thirdStepPanel.add(btnStart);
        btnStart.setEnabled(false);
        btnStart.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                showMessage("Process started!");
            }
        });
        //btnStart.setBackground(Color.RED);
        btnStart.setForeground(Color.BLACK);
        btnStart.setOpaque(false);
        btnStart.setBorderPainted(false);
        btnStart.setContentAreaFilled(false);
        btnBrowse.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                JFileChooser fc = new JFileChooser("some folder blablabla");
                int returnVal = fc.showOpenDialog(null);

                if (returnVal == JFileChooser.APPROVE_OPTION) {
                    file = fc.getSelectedFile();
                    //System.out.println(file.getPath());
                    textField.setText(file.getPath());
                } else if (returnVal == JFileChooser.CANCEL_OPTION) {

                    //System.out.println("File dialog cancelled");
                } else if (returnVal == JFileChooser.ERROR_OPTION) {

                    showMessage("Error in the file dialog!");
                }
            }
        });
        btnSelectAgent.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent arg0) {
                if (list.getSelectedItems().length == 0) {
                    showMessage("Please, select an Agent in the Agent's list");
                } else {
                    disableFirstStep();
                    enableSecondStep();

                }
            }
        });
        list.add("Zemax");
        list.add("Roundspot");
        list.add("CCDCamera");
        list.add("HexapodXYTable");

        /*        this.setExtendedState(JFrame.MAXIMIZED_BOTH);
         this.add(firstStepPanel, BorderLayout.WEST);
         this.add(secondStepPanel, BorderLayout.CENTER);
         this.add(thirdStepPanel, BorderLayout.EAST);*/
    }

    private void showMessage(String msg) {
        JOptionPane.showMessageDialog(this, msg);
    }

    private void enableFirstStep() {
        list.setEnabled(true);
        btnSelectAgent.setEnabled(true);
    }

    private void enableSecondStep() {
        textField.setEnabled(true);
        btnBrowse.setEnabled(true);
        btnPreviousStep2.setEnabled(true);
        btnLoad.setEnabled(true);

    }

    private void enableThirdStep() {
        btnPreviousStep3.setEnabled(true);
        btnStart.setEnabled(true);

    }

    private void disableFirstStep() {
        list.setEnabled(false);
        btnSelectAgent.setEnabled(false);
    }

    private void disableSecondStep() {
        textField.setEnabled(false);
        btnBrowse.setEnabled(false);
        btnPreviousStep2.setEnabled(false);
        btnLoad.setEnabled(false);
    }

    private void disableThirdStep() {
        btnPreviousStep3.setEnabled(false);
        btnStart.setEnabled(false);
    }

    private void makeFrameFullSize(JPanel panel) {
        Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
        panel.setSize(screenSize.width, screenSize.height);
    }

    BufferedImage image;

    public void nextButton() {
        try {
            image = ImageIO.read(new File("C:\\Users\\BAYGONCALVE\\Desktop\\red_button.png"));
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    protected void paintComponent(Graphics g) {
        super.paintComponents(g);
        g.drawImage(image, 0, 0, null);
    }

    public Dimension getPreferredSize() {
        return new Dimension(image.getWidth(), image.getHeight());
    }
}

Upvotes: 0

Views: 958

Answers (2)

MadProgrammer
MadProgrammer

Reputation: 347314

The main problem is, the frame isn't actually visible.

For example, after I cleaned up your code a little I get...

r1 = java.awt.Rectangle[x=1280,y=820,width=0,height=0]
r2 = java.awt.Rectangle[x=1280,y=820,width=0,height=0]
d1 = java.awt.Dimension[width=0,height=0]
d2 = java.awt.Dimension[width=0,height=0]

(took out the call to setBounds and few other things). Based on the fact that the window hasn't been realised yet (not shown), it's not surprising to me that the sizes don't actually change, because until you show the window, it has no context of which GraphicsDevice it will be shown on, therefore no means by which it can determine what the maximum size actually would be...

If you call setVisible(true) in the constructor, before calling setExtendedState(JFrame.MAXIMIZED_BOTH) I get...

r1 = java.awt.Rectangle[x=1214,y=801,width=132,height=38]
r2 = java.awt.Rectangle[x=-8,y=32,width=2576,height=1576]
d1 = java.awt.Dimension[width=132,height=38]
d2 = java.awt.Dimension[width=2576,height=1576]

Side notes...

  • Don't use null layouts. Pixel perfect layouts are an illusion in modern UI design, you have no control over fonts, DPI, rendering pipelines or other factors that will change the way that you components will be rendered on the screen. Swing was designed to work with layout managers to overcome these issues. If you insist on ignoring these features and work against the API design, be prepared for a lot of headaches and never ending hard work...
  • Don't mix heavy and light weight components, AWT and Swing components don't play well together, instead of using java.awt.List try using javax.swing.JList instead

Upvotes: 1

Stunner
Stunner

Reputation: 1131

Try this

frame.getWidth() and frame.getHeight()

To get size of JFrame without the title and other borders

frame.getContentPane().getSize();

Upvotes: 0

Related Questions