Hello,

I have worked on a project to make a custom-like Internal Frame.
There are also other classes which extends it, instead of JInternalFrame.
The project works quite good, but there is a problem.

The discription of how the JPanel window works:
_______________________________________________________________
There is a main JPanel which contians 4 JPanels on every corner.
There is also a JPanel called titlebar, Which is the draggable JPanel.
All the JPanels have added MouseListener and MouseMotionListener.

Whenever the mouse has entered any of the four corner panels, the cursor is changed
to NW,NE,SW,SE Resize Cursor.
These are draggable, and sets variables (wSize, hSize) in order to set JPanel size.

The SE_Resize panel is the only one working.

The Problem:

The problem is that when the SW(SouthWest) JPanel is dragged, it sets the frame size to minimum.
Same with the, NW JPanel.
Is there any way to fix this, so the SW, NW JPanel can resize the width and height properly? Like a JInternal Frame is resized.

Any answer will be appreciated.

Edited 4 Years Ago by 117: n/a

Attachments JProblem01.png 7.13 KB

Without seeing your code it's impossible to say what's wrong with it!

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class window extends JPanel 
					implements ActionListener, MouseMotionListener, MouseListener
{
	// Window count and location positioning
	private static int frameCount = 0;
	private static final int xOffset = 30;
	private static final int yOffset = 30;
	
	// Window Location and size
	private static int xPos, yPos, prevX, prevY = 0;
	private static int wSize, hSize = 0;
	
	private Dimension d;
	
	private JPanel titlebar, content;
	private JPanel [] panel  = new JPanel [4];
	private JButton[] button = new JButton[3];
	
	
	// Constructor
	public window()
	{
		frameCount += 1;
		init();
	}
	
	// Initialize and load everthing into memory.
	// Called by the constructor.
	public void init()
	{
		setLayout(null);
		setSize(200,200);
		setBackground(new Color(170,170,170));
		setBorder(BorderFactory.createLineBorder(Color.black));
		addMouseListener(this);
		addMouseMotionListener(this);
		setOpaque(true);
		setVisible(true);
		
		d = new Dimension(
				Toolkit.getDefaultToolkit().getScreenSize());
		double sw = d.getWidth();
		double sh = d.getHeight();
		int dx = (int) sw;
		int dy = (int) sh;
		
		titlebar = new JPanel();
		titlebar.setSize(getWidth()-10, 25);
		titlebar.setBackground(Color.white);
		titlebar.setBorder(BorderFactory.createLineBorder(Color.black));
		titlebar.addMouseListener(this);
		titlebar.addMouseMotionListener(this);
		titlebar.setOpaque(true);
		titlebar.setVisible(true);
		
		content = new JPanel();
		content.setLayout(null);
		content.setBackground(Color.white);
		content.setOpaque(true);
		content.setVisible(true);
		
		for (int i=0; i<panel.length; i++)
		{
			panel[i] = new JPanel();
			panel[i].setLayout(null);
			panel[i].setSize(35,30);
			panel[i].setBackground(getBackground());
			panel[i].setBorder(
					BorderFactory.createLineBorder(Color.black));
			panel[i].addMouseListener(this);
			panel[i].addMouseMotionListener(this);
			panel[i].setOpaque(true);
			panel[i].setVisible(true);
		}
		
		for (int i=0; i<button.length; i++)
		{
			button[i] = new JButton();
			button[i].addActionListener(new ActionListener()
			{
				public void actionPerformed(ActionEvent e)
				{
					button_actionPerformed(e);
				}
			});
			
			button[i].setBackground(null);
			button[i].setSize(getSize());
			button[i].setOpaque(true);
			button[i].setVisible(true);
		}
		
		update();
	}
	
	// Refresh the screen components when dragged
	// Repaint and validate other components
	public void update()
	{
		addComponent(this, titlebar, 5,5, getWidth()-10, titlebar.getHeight());
		addComponent(this, panel[0], 0,0, panel[0].getWidth(), panel[0].getHeight());
		addComponent(this, panel[1], getWidth()-panel[1].getWidth(), 0, panel[1].getWidth(), panel[1].getHeight());
		addComponent(this, panel[2], 0, getHeight()-panel[2].getHeight(), panel[2].getWidth(), panel[2].getHeight());
		addComponent(this, panel[3], getWidth()-panel[3].getWidth(), getHeight()-panel[3].getHeight(), panel[3].getWidth(), panel[3].getHeight());
		repaint();
		validate();
	}
	
	
	// Method for absolute positioning
	public void addComponent(Container container,
		Component c, int x, int y, int width, int height)
	{
		c.setBounds(x,y,width,height);
		container.add(c);
		validate();
	}
	
	
	// Set window size, cant be less than 0
	public void setWindowSize(int x, int y)
	{
		if (x >= 0 && y >= 0)
		{
			setSize(x,y);
			repaint();
		}
	}
	
	
	// Decides if a window can be resized
	// To change width and height
	public void setResizable(boolean paramBoolean)
	{
		if (paramBoolean)
		{
			
		}
		
		else
		{
			
		}
	}
	
	
	
	// ActionListener, actionPerformed (Event)
	public void actionPerformed(ActionEvent e)
	{
		
	}
	
	
	// Secure method which cannot be overriden by outside class
	public void button_actionPerformed(ActionEvent e)
	{
		if (e.getSource() == button[0])
		{
			
		}
		
		if (e.getSource() == button[1])
		{
			
		}
		
		if (e.getSource() == button[2])
		{
			
		}
	}
	
	
	
	// MouseListener and MouseMotionListener events
	// May have to be replaced by another class,
	// as it might get overidden by another mouseClass.
	public void mouseDragged(MouseEvent e)
	{
		if (e.getSource() == this)
		{
			if (e.getX() > getWidth()/2)
			{				
				setCursor(getCursor().getPredefinedCursor(Cursor.E_RESIZE_CURSOR));
				wSize = e.getX();
				hSize = getHeight();
				
				setSize(wSize,hSize);
				update();
				setCursor(getCursor().getPredefinedCursor(Cursor.E_RESIZE_CURSOR));
			}
			
			if (e.getX() < 30)
			{
				setCursor(getCursor().getPredefinedCursor(Cursor.W_RESIZE_CURSOR));
				wSize = e.getX() + prevX;
				hSize = getHeight();
				
				setSize(wSize, hSize);
				update();
			}
			
			if (e.getY() < 30)
			{
				setCursor(getCursor().getPredefinedCursor(Cursor.N_RESIZE_CURSOR));
				wSize = getWidth();
				hSize = getHeight() + e.getY();
				
				setSize(wSize, hSize);
				update();
			}
			
			if (e.getY() > getHeight()-5)
			{
				setCursor(getCursor().getPredefinedCursor(Cursor.S_RESIZE_CURSOR));
				wSize = getWidth();
				hSize = e.getY();
				
				setSize(wSize, hSize);
				update();
			}
		}
		
		if (e.getSource() == titlebar)
		{
			xPos = getX() + e.getXOnScreen() - prevX;
			yPos = getY() + e.getYOnScreen() - prevY;
			
			setLocation(xPos,yPos);
			repaint();
			
			prevX = e.getXOnScreen();
			prevY = e.getYOnScreen();
		}
		
		if (e.getSource() == panel[0])
		{
			
		}
		
		if (e.getSource() == panel[1])
		{
			
		}
		
		if (e.getSource() == panel[2])
		{
			
		}
		
		if (e.getSource() == panel[3])
		{
			wSize = e.getXOnScreen();
			hSize = e.getYOnScreen();
			
			setSize(wSize,hSize);
			update();
		}
			
	}
	
	public void mousePressed(MouseEvent e)
	{
		prevX = e.getXOnScreen();
		prevY = e.getYOnScreen();
		
		if (e.getSource() == this)
		{
			
		}
	}
	
	public void mouseClicked(MouseEvent e)
	{
		
	}
	
	public void mouseReleased(MouseEvent e)
	{
		if (e.getSource() == this)
		{
			setCursor(getCursor().getDefaultCursor());
		}
	}
	
	public void mouseMoved(MouseEvent e)
	{
		
	}
	
	public void mouseEntered(MouseEvent e)
	{
		if (e.getSource() == this)
		{
			if (e.getX() > getWidth()-5)
			{
				setCursor(getCursor().getPredefinedCursor(Cursor.E_RESIZE_CURSOR));
			}
			
			if (e.getX() < 5)
			{
				setCursor(getCursor().getPredefinedCursor(Cursor.W_RESIZE_CURSOR));
			}
			
			if (e.getY() < 5)
			{
				setCursor(getCursor().getPredefinedCursor(Cursor.N_RESIZE_CURSOR));
			}
			
			if (e.getY() > getHeight()-5)
			{
				setCursor(getCursor().getPredefinedCursor(Cursor.S_RESIZE_CURSOR));
			}
		}
		
		if (e.getSource() == panel[0])
		{
			setCursor(getCursor().getPredefinedCursor(Cursor.NW_RESIZE_CURSOR));
		}
		
		if (e.getSource() == panel[1])
		{
			setCursor(getCursor().getPredefinedCursor(Cursor.NE_RESIZE_CURSOR));
		}
		
		if (e.getSource() == panel[2])
		{
			setCursor(getCursor().getPredefinedCursor(Cursor.SW_RESIZE_CURSOR));
		}
		
		if (e.getSource() == panel[3])
		{
			setCursor(getCursor().getPredefinedCursor(Cursor.SE_RESIZE_CURSOR));
		}
	}
	
	public void mouseExited(MouseEvent e)
	{
		if (e.getSource() == this)
		{
			
		}
		
		if (e.getSource() == panel[0])
		{
			
		}
		
		if (e.getSource() == panel[1])
		{
			
		}
		
		if (e.getSource() == panel[2])
		{
			
		}
		
		if (e.getSource() == panel[3])
		{
			
		}
	}
}

Edited 4 Years Ago by 117: n/a

You seem to be saying that the only corner that works is the one where you have some code to make it work. The other three that don't have any code don't work. Or am I missing something?

No, that's pretty much it..

There is no code in the other methods,
I have tried different codes but none of them worked properly,
so I considered removing it.

Is there any way to make the custom window-like panel to resize similar to a JInternalFrame or a JFrame?

Thanks for the help.

Here is more describing picture:

Edited 4 Years Ago by 117: n/a

Attachments JProblem02.png 58.72 KB

Recently I tried different codes.
Still none of them seems to work correctly.

Any Idea of how to make the resizing like a JFrame or JIntenalFrame,
as shown by the picture?

Two stages.
1. track the mouse move as a change in its x and y coordinates - you need to record the position when the mouse is pressed then subtract that from the updated position as the mouse moves.
2. update the size and position of your frame. Each corner works differently, for example: the mouse moves by -10, -10 in the top-left box. This means the top left corner of your frame has to move up by 10 and left by 10 while leaving the bottom right corner unchanged. That means changing the frame's position by -10, -10 and its size by +10. +10. There are similar but slightly different calculations for each corner

Comments
Exactly! =D

Do you think the prevX and prevY variables are the variables that locate where the mouse is pressed?

This is what I tried doing on panel2, (The lower left SW) panel:

public void mouseDragged(MouseEvent e)
   {
       ...

       if(e.getSource() == panel2)
       {
           // If prevX and prevY tracks the mouse pressed location...

           wSize = prevX - e.getXOnScreen();
           hSize = prevY - e.getYOnScreen();
           
           setSize(wSize,ySize);
           update();
          
           // When updated..
           prevX = e.getXOnScreen();
           prevY = e.getYOnScreen();
       }       
   }

I guess, prevX and prevY locate the mousePressed Position of the JPanel.
Else, do you know If I have to make 2 more variables?

If ( prevX and PrevY are mousePressed variables ):
___________________________________________________
When the wSize and hSize variable are less than 0,
I believe that the JPanel is no longer visible..,

How to resize the panel with -1 or -10 values from variables if
the minimum size is 0.
__________________________________________________
Thanks for the help.
I will try to see if I can get this to work by looking at your
stage tutorial..,

Edited 4 Years Ago by 117: n/a

That look OK, except that lines 9/10 calculate the change in mouse position since the last call (that's good), but then you use that to replace the size, when you you should use it to change/increment the current size

Yeah, of Course!

I will have to get the previous size and extend it with the new size.

Thank you very much.

The problem still exists.

The problem is: there probably has to be a way to reposition the window while it is
getting resized from left while expanding.

Anyone know how to set this values so the windows resizing appears just like in any operating system, such as Windows or Linux.

A code-snippet would be very much appreciated.

Thanks for any help.

Just change the position as well as the size - setLocation(...)
eg when dragging the top left corner up/leftwards increase the size and decrease the location (x,y) by the same amount

hi all,
I have 2 questions here.

How do I change the text in the header titles such as "featured", "latest reviews", and "top rated"? I've tried looking at the stylecss, but I can't seem to figure it out.

Also, how can I change the style of slider? I would like to use a different slider by perhaps using a widget, but it looks this theme isn't widget-friendly.

Any advice or help?

Much appreciated!

This article has been dead for over six months. Start a new discussion instead.