-2

I was wondering how I would be able to resize a window when the user clicks and drags a button across the screen. I looked around and I can't find any answers.

My window code:

package Dashboard;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.BorderFactory;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JPanel;
import javax.swing.JProgressBar;
import javax.swing.JTextArea;
import javax.swing.SpringLayout;

public class DashboardWindow extends JFrame {

    JMenuBar menuBar;
    JMenuItem menuItem;
    static JMenu menu;
    static JPanel Desc = new JPanel();
    static JMenu dm = new JMenu("Dashboard");
    static JLabel slabel = new JLabel();
    static JPanel status = new JPanel();
    static JPanel frame = new JPanel();
    static JPanel games = new JPanel();
    static JPanel apps = new JPanel();
    static JMenuItem Exit = new JMenuItem("Exit");
    static JMenuBar menubar = new JMenuBar();
    static JTextArea descText = new JTextArea();
    static JButton run = new JButton("run");

    public static JProgressBar pbar;
    /**
     *
     */
    private static final long serialVersionUID = 1L;

    public DashboardWindow(String title, int operations) {
        super(title);

        pbar = new JProgressBar(0, operations);

        Color gray = new Color(41, 39, 39);
        Color white = new Color(199, 197, 197);
        Color ngray = new Color(32, 32, 32);

        Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
        int width = (int) screenSize.getWidth();
        int height = (int) screenSize.getHeight();
        System.out.println("screen width : " + width + ", screen height : " + height);

        SpringLayout springLayout = new SpringLayout();
        Desc.setLayout(springLayout);

        menubar.setBackground(gray);
        menubar.setForeground(gray);
        menubar.setBorderPainted(false);
        menubar.setBorder(BorderFactory.createEmptyBorder());
        pbar.setBackground(gray);

        slabel.setText("  :   No Tasks Right Now");
        slabel.setSize(278, 15);
        slabel.setBackground(gray);
        slabel.setLocation(0, 253);
        slabel.setForeground(white);

        dm.setBackground(gray);
        dm.setForeground(white);
        dm.setBorderPainted(false);
        dm.setBorder(BorderFactory.createEmptyBorder());

        Exit.setBackground(gray);
        Exit.setForeground(white);
        Exit.setBorderPainted(false);
        Exit.setBorder(BorderFactory.createEmptyBorder());

        Exit.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                System.exit(0);
            }
        });

        dm.add(Exit);

        menubar.add(dm);

        setJMenuBar(menubar);

        Dimension prefSize = pbar.getPreferredSize();
        prefSize.height = 10;
        pbar.setPreferredSize(prefSize);

        frame.setBackground(gray);
        this.add(frame);
        status.setBackground(gray);
        status.setBorder(BorderFactory.createDashedBorder(null));
        status.setPreferredSize(new Dimension(200, 80));
        status.add(pbar);
        status.add(slabel, BorderLayout.PAGE_END);
        add(status, BorderLayout.PAGE_END);

        games.setBackground(white);
        games.setBorder(BorderFactory.createEmptyBorder());
        games.setPreferredSize(new Dimension((int) (width / 6.5), (int) (height / 9.5)));

        run.setBorderPainted(false);
        run.setBackground(gray);
        run.setFont(new Font("Arial", Font.PLAIN, 18));
        run.setForeground(white);
        //run.setAlignmentX(Component.CENTER_ALIGNMENT);
        run.setVisible(false);

        springLayout.putConstraint(SpringLayout.WEST, run, (int) (width / 3), SpringLayout.WEST, this);
        springLayout.putConstraint(SpringLayout.NORTH, run, (int) (height / 15), SpringLayout.NORTH, this);
        Desc.add(run);

        apps.setLayout(new BoxLayout(apps, BoxLayout.Y_AXIS));
        apps.setBackground(ngray);
        apps.setBorder(BorderFactory.createEtchedBorder());
        apps.setPreferredSize(new Dimension(300, 0));

        Desc.setBackground(ngray);
        Desc.setBorder(BorderFactory.createEtchedBorder());
        Desc.setPreferredSize(new Dimension((int) (width / 1.65), 0));

        //apps.setAlignmentX(CENTER_ALIGNMENT);
        //descText.setAlignmentX(Component.CENTER_ALIGNMENT);
        descText.setBackground(ngray);
        springLayout.putConstraint(SpringLayout.WEST, descText, (int) (width / 20), SpringLayout.WEST, this);
        springLayout.putConstraint(SpringLayout.NORTH, descText, (int) (height / 20), SpringLayout.NORTH, this);
        Desc.add(descText);

        add(Desc, BorderLayout.LINE_END);
        add(apps, BorderLayout.LINE_START);
        add(games, BorderLayout.CENTER);

        this.setUndecorated(true);
        this.setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE);
        this.setSize(new Dimension((int) (width / 1.35), (int) (height / 1.3)));
        this.setLocationRelativeTo(null);
        this.setVisible(true);
        Dashboard.initializerTasks();
    }

    public static void PBarValue(int Value) {
        pbar.setValue(Value);
    }

    public static void ChangeText(String text) {
        slabel.setText(text);
    }

    public static void PWaitTrue() {
        pbar.setIndeterminate(true);
    }

    public static void PWaitFalse() {
        pbar.setIndeterminate(false);
    }

    public static void PSetMax(int max) {
        pbar.setMaximum(max);
    }

}

Any help is appreciated :D

trashgod
  • 203,806
  • 29
  • 246
  • 1,045
UltraTechX
  • 15
  • 4
  • Did you just look around? Solutions don't just fly in the air you know. What did you try and why is this code not doing what you want to achieve? – moffeltje Jun 30 '15 at 13:22
  • 1
    Also, you've posted a lot of code, most of it unrelated to your actual problem at hand. A useful tool that would help both you and us, would be for you to pare your code down to the minimal code that compiles, runs, and either tries to do what you're attempting or shows us a but that you're trying to debug. This will help both you and us to see your problem much more clearly and distraction-free. – Hovercraft Full Of Eels Jun 30 '15 at 13:36
  • 3
    Don't use static variable for your components. This is the sign of poorly designed class. Variable names should NOT start with an upper case character. Most of your variable names are correct but some are not. Be consistent and follow Java naming conventions!!! – camickr Jun 30 '15 at 13:51
  • Possible [duplicate](http://stackoverflow.com/q/2561690/230513). – trashgod Jun 30 '15 at 14:05
  • 1
    Also, I really don't understand your requirement. If you want to resize a frame you drag the borders of the frame. Why would you click and drag a button? What about other components, can you drag them as well? Are you trying to create a panel with components that you can drag around the panel? If so then you would make the panel resizable and add the panel to a scroll pane so scrollbars appear if needed. Your question is really not clear at all to me. – camickr Jun 30 '15 at 14:14

1 Answers1

0

The below example may help you.

How to use this :-

Drag Method : Press and hold the button and move the mouse some where in the screen and release. The window(jframe) will get resized. This example will handle only the right side and the bottom side from the window starting point (x,y). You can modify the code as you needed for any direction and 'on the way' resize.

Clcik Method : Click the button and move the mouse any where and re click to release.This have the 'on the way' resize

import java.awt.MouseInfo;
import javax.swing.JButton;
import javax.swing.JFrame;

public class WindowResize {
    boolean isMoving = false;
    JFrame objFrm = new JFrame("Resize Demo");
    JButton ctrlButton = new JButton("Press and hold");
    public void fnShow() {
        objFrm.setSize(500, 500);
        objFrm.add(ctrlButton, java.awt.BorderLayout.PAGE_END);
        ctrlButton.setLocation(9, 0);
        objFrm.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        objFrm.setVisible(true);
        ctrlButton.addMouseListener(new java.awt.event.MouseAdapter() {
            @Override
            public void mouseReleased(java.awt.event.MouseEvent evt) {
                fnSetScreenSize(evt, true);
            }
        });
        ctrlButton.addMouseListener(new java.awt.event.MouseAdapter() {
            @Override
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                isMoving = !isMoving;
                if (isMoving) {
                    ctrlButton.setText("Resizing now");
                } else {
                    ctrlButton.setText("Window size just fine");
                }
            }
            @Override
            public void mouseExited(java.awt.event.MouseEvent evt) {
                fnSetScreenSize(evt, isMoving);
            }
        });
        ctrlButton.addMouseMotionListener(new java.awt.event.MouseMotionAdapter() {
            @Override
            public void mouseMoved(java.awt.event.MouseEvent evt) {
                fnSetScreenSize(evt, isMoving);
            }
        });
        objFrm.addMouseMotionListener(new java.awt.event.MouseMotionAdapter() {
            @Override
            public void mouseMoved(java.awt.event.MouseEvent evt) {
                fnSetScreenSize(evt, isMoving);
            }
        });
    }
    private void fnSetScreenSize(java.awt.event.MouseEvent evt, boolean _isMoving) {
        if (_isMoving) {
            int intWidth = MouseInfo.getPointerInfo().getLocation().x - objFrm.getLocationOnScreen().x + ((ctrlButton.getHeight() / 2) * 2);
            int intHeight = MouseInfo.getPointerInfo().getLocation().y - objFrm.getLocationOnScreen().y + ((ctrlButton.getHeight() / 2) * 2);
            objFrm.setSize(intWidth, intHeight);
        }
    }
    public static void main(String[] args) {
        new WindowResize().fnShow();
    }
}
Jeet
  • 1,006
  • 1
  • 14
  • 25