10 Replies - 2609 Views - Last Post: 09 November 2010 - 06:19 AM Rate Topic: -----

#1 giuseppe105  Icon User is offline

  • D.I.C Regular

Reputation: 9
  • View blog
  • Posts: 444
  • Joined: 15-May 08

[SOLVED] JPanel not drawing

Posted 08 November 2010 - 12:50 PM

i have a JDialog and inside that JDialog i have a JPanel and inside the JPanel i add the Jpanel im drawing on. but its not drawing my picture when i run it.

here is my code.

Jdialog
public class test1 extends JDialog implements ActionListener
{
	private boolean closeState;
	
	private JButton Cancel = new JButton("Cancel");
	private JButton OK = new JButton("Ok");
	
	public test1()
	{
		this.setLayout(null);
		this.setModal(true);
		
		test2 tc = new test2();
		
		tc.setBounds(0,0,300,550);
		JPanel pan = new JPanel();
		pan.add(tc);
		this.add(pan);
		
		this.setLocationRelativeTo(null);
		this.setSize(500,550);
		this.setTitle("New");
		this.setResizable(false);
		this.setLocation(0,0);
	}
	
	public boolean showNewWindow()
	{
		setVisible(true);
		
		return closeState;
	}
	
	public void actionPerformed(ActionEvent e) 
	{
	    if(e.getSource() == this.OK)
	    {	    	
            	
	    	closeState = true;
	    }
	    else if(e.getSource() == this.Cancel)
	    {
	    	closeState = false;
	    }
	    dispose();
	}
	
}


my custom Jpanel i draw on.
import javax.swing.JPanel;
import java.awt.Graphics;            //To draw images to screen or buffers

public class test2 extends JPanel
{
	public test2()
	{
		this.setLayout(null);
		System.out.println("im here");
		repaint();
	}
	
	public void paint(Graphics g)
	{
		super.paint(g);
		g.drawRect(50, 50, 32, 32);  //draws a rectangle on screen at mouse
	}
}


all i get is a grey window.

This post has been edited by giuseppe105: 09 November 2010 - 06:20 AM


Is This A Good Question/Topic? 0
  • +

Replies To: [SOLVED] JPanel not drawing

#2 g00se  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2675
  • View blog
  • Posts: 11,296
  • Joined: 20-September 08

Re: [SOLVED] JPanel not drawing

Posted 08 November 2010 - 01:22 PM

You need to avoid null layouts - it won't do you any favours. See the attached

Attached File(s)

  • Attached File  src.zip (795bytes)
    Number of downloads: 86

Was This Post Helpful? 0
  • +
  • -

#3 giuseppe105  Icon User is offline

  • D.I.C Regular

Reputation: 9
  • View blog
  • Posts: 444
  • Joined: 15-May 08

Re: [SOLVED] JPanel not drawing

Posted 08 November 2010 - 05:54 PM

Thank you for your reply that does fix the problem... but it only causes a million more.

I have been told a million times that null layout is horrible and i shouldn't use it.

but i use null layout because it does exactly what i want. I don't need to spend hours on end trying to place a button where i want it to be because of a random layout.

null layout exists because it is usefull. There has to be a way to make the null layout work just like any other Layout manager. I am willing to learn how to accomplish that can anyone point me in the direction of where i might find this information?

Don't point to the door...
Was This Post Helpful? 0
  • +
  • -

#4 cfoley  Icon User is offline

  • Cabbage
  • member icon

Reputation: 1948
  • View blog
  • Posts: 4,048
  • Joined: 11-December 07

Re: [SOLVED] JPanel not drawing

Posted 08 November 2010 - 06:19 PM

I've not tested this but I think you forgot to set JPanel pan to a null layout. This means it is a FlowLayout which will laugh in the face of any bounds you try to set.

Again, I've not tested it. In fact, I'm such a stickler for doing things the right way I've never even coded a null layout in ten years of programming Java. ;)
Was This Post Helpful? 0
  • +
  • -

#5 giuseppe105  Icon User is offline

  • D.I.C Regular

Reputation: 9
  • View blog
  • Posts: 444
  • Joined: 15-May 08

Re: [SOLVED] JPanel not drawing

Posted 08 November 2010 - 07:33 PM

thank you cfoley but i tried that when g00se told me it was a layout problem

I didn't tell you guys.

But i did this earlier in my program with a JFrame A JPanel and another Jpanel

this made the same problem as now.

I fixed it by changing the middle Jpanel to a JLayeredPane.

I have no explanation as to why it works but it doesn't work for this new problem.
Was This Post Helpful? 0
  • +
  • -

#6 pbl  Icon User is offline

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

Reputation: 8327
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: [SOLVED] JPanel not drawing

Posted 08 November 2010 - 07:43 PM

That one should do it

http://www.dreaminco...snippet5843.htm
Was This Post Helpful? 0
  • +
  • -

#7 g00se  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2675
  • View blog
  • Posts: 11,296
  • Joined: 20-September 08

Re: [SOLVED] JPanel not drawing

Posted 09 November 2010 - 03:05 AM

Quote

I don't need to spend hours on end trying to place a button where i want it to be because of a random layout.


It isn't random. What you mean is you don't understand how the layout(s) work(s). When you've mastered layout managers, you can then use a null layout, as you'll understand exactly how they work and the intricacies they allow you to avoid. And when you've mastered them, you won't want to use a null layout.
Was This Post Helpful? 0
  • +
  • -

#8 cfoley  Icon User is offline

  • Cabbage
  • member icon

Reputation: 1948
  • View blog
  • Posts: 4,048
  • Joined: 11-December 07

Re: [SOLVED] JPanel not drawing

Posted 09 November 2010 - 03:08 AM

I just put your code in my IDE and sorted it. You do need to set pan's layout to null but you also need to set it's bounds. I missed the latter. Fixed code is below. I added some background colours to help me troubleshoot and I've left the code for that in. It's a useful kind of thing to do. Final point is that some of your panels are bigger than your window. Seems kinda silly.

Also, congratulations on making me code my first null layout. :)

import java.awt.Color;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JPanel;
import javax.swing.border.Border;

public class test1 extends JDialog implements ActionListener
{
	private boolean closeState;

	private JButton Cancel = new JButton("Cancel");
	private JButton OK = new JButton("Ok");

	public test1()
	{
		this.setLayout(null);
		this.setModal(true);

		test2 tc = new test2();

		tc.setBounds(0,0,300,550);
		JPanel pan = new JPanel();
		pan.setBounds(0, 0, 400, 550);
		pan.setLayout(null); 
		pan.add(tc);
		this.add(pan);

		this.setLocationRelativeTo(null);
		this.setSize(500,550);
		this.setTitle("New");
		this.setResizable(false);
		this.setLocation(0,0);

		// Debug colours
		{
			pan.setBackground(Color.RED);
			tc.setBackground(Color.BLUE);
			pan.setOpaque(true);
			tc.setOpaque(true);
		}


	}

	public boolean showNewWindow()
	{
		setVisible(true);

		return closeState;
	}

	public void actionPerformed(ActionEvent e) 
	{
		if(e.getSource() == this.OK)
		{	    	

			closeState = true;
		}
		else if(e.getSource() == this.Cancel)
		{
			closeState = false;
		}
		dispose();
	}

	public static void main(String[] args) {
		new test1().setVisible(true);
	}

}


class test2 extends JPanel
{
	public test2()
	{
		this.setLayout(null);
		System.out.println("im here");
		repaint();
	}

	public void paint(Graphics g)
	{
		super.paint(g);
		g.drawRect(50, 50, 32, 32);  //draws a rectangle on screen at mouse
	}
}


Was This Post Helpful? 1
  • +
  • -

#9 g00se  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2675
  • View blog
  • Posts: 11,296
  • Joined: 20-September 08

Re: [SOLVED] JPanel not drawing

Posted 09 November 2010 - 03:18 AM

Yes, but guis should be resizable, otherwise one is making assumptions about hardware, which normally shouldn't be done. That's what layout managers allow
Was This Post Helpful? 0
  • +
  • -

#10 cfoley  Icon User is offline

  • Cabbage
  • member icon

Reputation: 1948
  • View blog
  • Posts: 4,048
  • Joined: 11-December 07

Re: [SOLVED] JPanel not drawing

Posted 09 November 2010 - 03:22 AM

Yes, I do understand. :) There are also issues like the font size on a button in different look and feels. A LayoutManager can be more easily configured to ensure components are displayed at a sensible size. Most null layout will probably break when you use SuperChunkyAccessibleLookAndFeel.
Was This Post Helpful? 0
  • +
  • -

#11 giuseppe105  Icon User is offline

  • D.I.C Regular

Reputation: 9
  • View blog
  • Posts: 444
  • Joined: 15-May 08

Re: [SOLVED] JPanel not drawing

Posted 09 November 2010 - 06:19 AM

Thank you for your help.

cfoley post is correct. You all make an excellent point null layouts are a bad choice. But i have avoided them since i started working with swing. I will most likely be covering layout managers in my second semester of my programing class in college.

I will put an effort to use layout managers before i resort to null layout.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1