11 Replies - 1425 Views - Last Post: 21 March 2009 - 11:30 AM Rate Topic: -----

#1 bmcginnis  Icon User is offline

  • D.I.C Head

Reputation: 8
  • View blog
  • Posts: 130
  • Joined: 21-February 09

Null Layout Problem

Post icon  Posted 19 March 2009 - 06:56 PM

I've been trying to make a GUI that is consistent cross-OS with null layout and have been having trouble. My problem is that you cant just set the frames size for how it looks on your system because the title-bar is different heights from OS to OS. So what I want to do is remove the title-bar and make my own title-bar via placing a jpanel and adding a mouselistener. Does anyone know of any snippets for doing this? Ive tried:

JPanel JPanel_Title = new JPanel();
Container_Main.add(JPanel_Title);
JPanel_Title.setBackground(Color.black);
JPanel_Title.setBounds(0, 0, 200, 20);
JPanel_Title.addMouseListener(new MouseAdapter()
{
	public void mousePressed(MouseEvent e)
	{
		while(true)
		{
			Integer mouseX = MouseInfo.getPointerInfo().getLocation().x;
			Integer mouseY = MouseInfo.getPointerInfo().getLocation().y;
			JFrame_Main.setLocation(mouseX, mouseY);
		}
	}
});


Which basically sets the very top-left corner of the frame to the mouse position when I click. It also keeps moving to the mouse position even after I release the mouse button. I know this is because of my while(true), but i'm unsure of what to put in the while parameters. Is there something like while(mousePressed)? Once that problem would be solved though, I would still need to know the mouses relative location to the frame so that it doesn't just position the windows top left corner to the mouse position. Any ideas of how to achieve this?

Another idea I had to make a consistent GUI would be to set the frame size via positions of elements inside. For example, say the bottom element in a frame is a jbutton test. what I would think would work would be:

mainframe.setSize(200, test.getY() + test.GetHeight() + 5);
// Expected to get the vertical position of test, add the height to that, and then add a 5 pixel space



However, it does not. Any ideas why, I can set the positions of other elements using getWhatever() methods, why can't I use the same methods to set the size of the main frame? Thanks.

This post has been edited by bmcginnis: 19 March 2009 - 06:56 PM


Is This A Good Question/Topic? 0
  • +

Replies To: Null Layout Problem

#2 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8343
  • View blog
  • Posts: 31,890
  • Joined: 06-March 08

Re: Null Layout Problem

Posted 19 March 2009 - 07:09 PM

If the 2 or 4 pixels difference between Windows, Linux and Mac bothers you you can always:

frame.setUndecorated(true);
Was This Post Helpful? 0
  • +
  • -

#3 bmcginnis  Icon User is offline

  • D.I.C Head

Reputation: 8
  • View blog
  • Posts: 130
  • Joined: 21-February 09

Re: Null Layout Problem

Posted 19 March 2009 - 07:13 PM

I am doing that, the problem is that that removes the title bar and then users can no longer move my window. That's why I need to either make my own title-bar with a drag capability or be able to do my second idea which was adjust setSize Y by relative postions of elements.
Was This Post Helpful? 0
  • +
  • -

#4 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8343
  • View blog
  • Posts: 31,890
  • Joined: 06-March 08

Re: Null Layout Problem

Posted 19 March 2009 - 07:24 PM

View Postbmcginnis, on 19 Mar, 2009 - 06:13 PM, said:

I am doing that, (was not shown in the code you posted) the problem is that removes the title bar

that is why it was written to do

Quote

and then users can no longer move my window.

Obviosly

Quote

That's why I need to either make my own title-bar with a drag capability

That is wat I though you did

Quote

or be able to do my second idea which was adjust setSize Y by relative postions of elements.

Never tried it but I doubt you can set it below a minimumPreferredSize and which object is it ? fram.etDecorated() ??
Was This Post Helpful? 0
  • +
  • -

#5 bmcginnis  Icon User is offline

  • D.I.C Head

Reputation: 8
  • View blog
  • Posts: 130
  • Joined: 21-February 09

Re: Null Layout Problem

Posted 20 March 2009 - 06:45 AM

I tried to make a draggable panel but it doesnt work right. I'm unsure what to put in the while loop to make it only loop while the mouse is pressed, or if that is even the right idea.
Was This Post Helpful? 0
  • +
  • -

#6 William_Wilson  Icon User is offline

  • lost in compilation
  • member icon

Reputation: 205
  • View blog
  • Posts: 4,807
  • Joined: 23-December 05

Re: Null Layout Problem

Posted 20 March 2009 - 06:53 AM

Your mousepressed idea should be very doable. By adding a mouse listener to the frame when the mousePressed event is triggered it will run the code and there shouldn't be any need for the while loop. You can also add code to the other mouse triggers in the listener for added features.

http://publib.boulde...seListener.html
Was This Post Helpful? 1
  • +
  • -

#7 bmcginnis  Icon User is offline

  • D.I.C Head

Reputation: 8
  • View blog
  • Posts: 130
  • Joined: 21-February 09

Re: Null Layout Problem

Posted 20 March 2009 - 12:27 PM

Thank you for the legitimate response. I looked into the MouseMotionListener like your link suggested and have gotten a bit closer to what I want.

I now have:
JPanel JPanel_Title = new JPanel();
Container_Main.add(JPanel_Title);
JPanel_Title.setBackground(Color.black);
JPanel_Title.setBounds(0, 0, 200, 20);
JPanel_Title.addMouseMotionListener(new MouseMotionAdapter()
{
	public void mouseDragged(MouseEvent e)
	{
		Integer mouseX = MouseInfo.getPointerInfo().getLocation().x;
		Integer mouseY = MouseInfo.getPointerInfo().getLocation().y;
		JFrame_Main.setLocation(e.getX(), e.getY());
	}
});


Which solves the part about only moving on mouse press, however I'm still having issues moving the frame relatively to the mouse. As you can see, I have tried JFrame_Main.setLocation(e.getX(), e.getY()); which does what I want but it adds a second ghost sort of frame and has horrible flickering when I drag. Any ideas why? Thanks.

This post has been edited by bmcginnis: 20 March 2009 - 12:28 PM

Was This Post Helpful? 0
  • +
  • -

#8 bmcginnis  Icon User is offline

  • D.I.C Head

Reputation: 8
  • View blog
  • Posts: 130
  • Joined: 21-February 09

Re: Null Layout Problem

Posted 20 March 2009 - 08:50 PM

No ideas why this flickering and ghost frame is occuring?
Was This Post Helpful? 0
  • +
  • -

#9 virgul  Icon User is offline

  • D.I.C Regular

Reputation: 44
  • View blog
  • Posts: 269
  • Joined: 18-March 09

Re: Null Layout Problem

Posted 21 March 2009 - 02:24 AM

By horrible second ghost frame do you kind of mean like how it was back in <=windows 95?
Was This Post Helpful? 0
  • +
  • -

#10 William_Wilson  Icon User is offline

  • lost in compilation
  • member icon

Reputation: 205
  • View blog
  • Posts: 4,807
  • Joined: 23-December 05

Re: Null Layout Problem

Posted 21 March 2009 - 06:42 AM

If you post your complete code we may be able to help more.

Most often the problem you are describing occurs when a frame is hidden and shown at a quick rate.
Was This Post Helpful? 0
  • +
  • -

#11 bmcginnis  Icon User is offline

  • D.I.C Head

Reputation: 8
  • View blog
  • Posts: 130
  • Joined: 21-February 09

Re: Null Layout Problem

Posted 21 March 2009 - 08:21 AM

Full code:

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

public class mouseRGB 
{
	public static void main(String[] args) throws AWTException, ClassNotFoundException, InstantiationException, IllegalAccessException, UnsupportedLookAndFeelException
	{
		UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
		
		final Robot robot = new Robot();
		
		final JFrame JFrame_Main = new JFrame();
		Container Container_Main = JFrame_Main.getContentPane();
		Container_Main.setLayout(null);
		
		JLabel JLabel_Exit = new JLabel();
		Container_Main.add(JLabel_Exit);
		JLabel_Exit.setBounds(190, 3, 10, 15);
		JLabel_Exit.setForeground(Color.white);
		JLabel_Exit.setText("X");
		JLabel_Exit.addMouseListener(new MouseAdapter()
		{
			public void mouseClicked(MouseEvent e)
			{
					System.exit(0);
			}
		});
		
		JPanel JPanel_Title = new JPanel();
		Container_Main.add(JPanel_Title);
		JPanel_Title.setBackground(Color.black);
		JPanel_Title.setBounds(0, 0, 200, 20);
		JPanel_Title.addMouseMotionListener(new MouseMotionAdapter()
		{
			public void mouseDragged(MouseEvent e)
			{
					Integer mouseX = MouseInfo.getPointerInfo().getLocation().x;
					Integer mouseY = MouseInfo.getPointerInfo().getLocation().y;
					JFrame_Main.setLocation(mouseX + (mouseX - JFrame_Main.getX()), mouseY + (mouseY - JFrame_Main.getY()));
					robot.delay(1);
			}
		});
		
		JLabel JLabel_Coords = new JLabel();
		Container_Main.add(JLabel_Coords);
		JLabel_Coords.setBounds(3, JPanel_Title.getHeight() + 3, 120, 15);
		
		JLabel JLabel_Red = new JLabel();
		Container_Main.add(JLabel_Red);
		JLabel_Red.setBounds(3, JLabel_Coords.getY() + JLabel_Coords.getHeight() + 3, 120, 15);
		
		JLabel JLabel_Green = new JLabel();
		Container_Main.add(JLabel_Green);
		JLabel_Green.setBounds(3, JLabel_Red.getY() + JLabel_Red.getHeight() + 3, 120, 15);
		
		JLabel JLabel_Blue = new JLabel();
		Container_Main.add(JLabel_Blue);
		JLabel_Blue.setBounds(3, JLabel_Green.getY() + JLabel_Green.getHeight() + 3, 120, 15);
		
		JFrame_Main.setBackground(Color.gray);
		JFrame_Main.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		JFrame_Main.setResizable(false);
		JFrame_Main.setSize(200, 150);
		JFrame_Main.setTitle("Mouse Info");
		JFrame_Main.setUndecorated(true);
		JFrame_Main.setVisible(true);
	}
}

This post has been edited by bmcginnis: 21 March 2009 - 08:22 AM

Was This Post Helpful? 0
  • +
  • -

#12 baavgai  Icon User is offline

  • Dreaming Coder
  • member icon

Reputation: 5901
  • View blog
  • Posts: 12,805
  • Joined: 16-October 07

Re: Null Layout Problem

Posted 21 March 2009 - 11:30 AM

I don't know how to fix your null problem, I'm afraid, except to say it's caused by the "UIManager.setLookAndFeel" thing.

My first response would be never use layout null, but there can be reasons for it. It don't know that you have one, to be honest. Building an entire form dynamically is awkward, extending classes is much cleaner. You should be able to get what you want with just a JPanel with null layout and ignore the JFrame.

Still, it sounded interesting, faking a title bar. I think it's safest to also listen for when the mouse was pressed and key off of that. It seemed to work, anyway. Here's what I did; hope it helps.

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

class TitlePane extends JPanel {
	public TitlePane() {
		setLayout( new BorderLayout() );
		setBackground(Color.black);
		
		JLabel lx = new JLabel("X");
		lx.setForeground(Color.white);
		lx.addMouseListener(new MouseAdapter() {
			public void mouseClicked(MouseEvent e) { System.exit(0); }
		});
		add(lx, BorderLayout.EAST);
	}
}


class MainPane extends JPanel {
	private JLabel labCoords, labRed, labGreen, labBlue;
	private final int labHeight = 15;
	private final int labGutter = 3;
	
	public MainPane() {
		setLayout(null);
		int y = labGutter;
		labCoords = addLab(y);
		y = incY(y);
		labRed = addLab(y); 
		y = incY(y);
		labGreen = addLab(y); 
		y = incY(y);
		labBlue = addLab(y);
	}
	
	private int incY(int y) { return y+labHeight+labGutter; } 
	
	private JLabel addLab(int y) {
		JLabel lab = new JLabel();
		lab.setBounds(labGutter, y, 120, labHeight);
		add(lab);
		return lab;
	}
}


class MainFrame extends JFrame implements MouseMotionListener, MouseListener {
	private MainPane mainPane = new MainPane();
	private Point pressedLocation = null;
	
	public MainFrame() {
		setLayout( new BorderLayout() );
		TitlePane title = new TitlePane();
		title.addMouseMotionListener(this);
		title.addMouseListener(this);
		add(title, BorderLayout.NORTH);
		//JPanel_Title.addMouseMotionListener(this);
		add(mainPane, BorderLayout.CENTER);
		
		setBackground(Color.gray);
		//setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		//setResizable(false);
		setSize(200, 150);
		//setTitle("Mouse Info");
		setUndecorated(true);
	}
	
	public void mouseDragged(MouseEvent e) {
		if (pressedLocation!=null) {
			int x = this.getX() + e.getX() - pressedLocation.x;
			int y = this.getY() + e.getY() - pressedLocation.y;
			setLocation(x,y);
			//robot.delay(1);
		}
	}
	
	public void mouseMoved(MouseEvent e) { }
	public void mousePressed(MouseEvent e) {
		pressedLocation = new Point(e.getX(), e.getY());
	}

	public void mouseReleased(MouseEvent e) { 
		pressedLocation = null;
	}
	public void mouseEntered(MouseEvent e) {  }
	public void mouseExited(MouseEvent e) {  }
	public void mouseClicked(MouseEvent e) {  }

	
}

public class mouseRGB {
	
	public static void main(String[] args) throws Exception {
	
		//System.out.println(UIManager.getSystemLookAndFeelClassName());
	// public static void main(String[] args) throws AWTException, ClassNotFoundException, InstantiationException, IllegalAccessException, UnsupportedLookAndFeelException {
		//UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
		
		// final Robot robot = new Robot();
		new MainFrame().setVisible(true);
	}
}


Was This Post Helpful? 0
  • +
  • -

Page 1 of 1