7 Replies - 655 Views - Last Post: 28 July 2012 - 12:32 PM Rate Topic: -----

#1 Yosemine  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 10
  • Joined: 12-February 12

"New" Swing JPanel Glitch

Posted 27 July 2012 - 12:48 PM

Hello, in creating a projectile motion grapher I came across something that confused me a bit. This is my first "real" project so I realize some things are messy.

My code uses a main class called GUI that assembles all the pieces of the GUI into one main area. So, for example, the graphing JPanel, and the JPanel where you enter your values, are separate classes, which are then added to the frame and updated through the GUI class.

In the GUI class I have an object called "info" which displays information about the trajectory the object followed (time, max height, etc). The class that info is made from extends JPanel and has two constructors, one simply sets the JPanel to a gray box, and another accesses a different object which has all the information about the flight trajectory and draws several JLabels on the panel to display the information.

The GUI class also has an updateGUI() method which looks like this:

public void updateGUI()
	{ 
		if(!(components.isStopText()))
		{    
			info = new DrawInfoPanel(components.getObject()); 
			setInfoPanel();
		} 
		
		grapher.updateGraph(components.getObject(), components.isIdealPath(),
									 components.isRealtime());
	}


This is called when the simulation button is pressed.

Previously, info is declared with an empty constructor to just draw a grey box. The problem is that when I create the new DrawInfoPanel, it seems as though it overlays the previous info box. If I take the window I've created and shake it around, the newly updated DrawInfoPanel disappears and I'm left with an empty gray box. It will also do this if I take the info panel offscreen.

This happens no matter how many times I've updated the info box. I managed to "solve" the issue by using info.setVisible(false) before setting it new, but this appears like a hack job that could lead to wasted memory.

Does anybody know what the issue is? I've attached the program so you can see for yourself what I mean.

Is This A Good Question/Topic? 0
  • +

Replies To: "New" Swing JPanel Glitch

#2 Yosemine  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 10
  • Joined: 12-February 12

Re: "New" Swing JPanel Glitch

Posted 27 July 2012 - 12:54 PM

Sorry nevermind about the program. I tried to attach it but JARs aren't allowed.
Was This Post Helpful? 0
  • +
  • -

#3 Ghlavac  Icon User is offline

  • D.I.C Addict

Reputation: 84
  • View blog
  • Posts: 519
  • Joined: 14-January 09

Re: "New" Swing JPanel Glitch

Posted 27 July 2012 - 01:03 PM

Are you updating the GUI on the Swing thread?

Ie..

SwingUtilities.invokeLater(new Runnable()
{
public void run() {...}
});


This post has been edited by Ghlavac: 27 July 2012 - 01:04 PM

Was This Post Helpful? 1
  • +
  • -

#4 Yosemine  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 10
  • Joined: 12-February 12

Re: "New" Swing JPanel Glitch

Posted 27 July 2012 - 01:26 PM

Good thought. And you were correct, it actually wasn't, it appears I only put my createGUI() method there.

However, even after adding it to the Swing thread, I have the same issue.

The only difference is that my boolean appears to be reversed, as now everything is done in a more logical order (so thanks as this makes more sense).
Was This Post Helpful? 0
  • +
  • -

#5 g00se  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 2779
  • View blog
  • Posts: 11,765
  • Joined: 20-September 08

Re: "New" Swing JPanel Glitch

Posted 27 July 2012 - 03:28 PM

It's usually a good idea to avoid tearing down a gui, as opposed to having it repaint itself. It sounds as though you're doing the former. Try attaching your code as a zip file
Was This Post Helpful? 2
  • +
  • -

#6 farrell2k  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 851
  • View blog
  • Posts: 2,599
  • Joined: 29-July 11

Re: "New" Swing JPanel Glitch

Posted 27 July 2012 - 04:11 PM

View PostGhlavac, on 27 July 2012 - 08:03 PM, said:

Are you updating the GUI on the Swing thread?

Ie..

SwingUtilities.invokeLater(new Runnable()
{
public void run() {...}
});



He is executing updateGui() on the event dispatch thread via his "simulation" button click, so Swingutilities in not required.
Was This Post Helpful? 0
  • +
  • -

#7 Yosemine  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 10
  • Joined: 12-February 12

Re: "New" Swing JPanel Glitch

Posted 27 July 2012 - 05:56 PM

View Postg00se, on 27 July 2012 - 03:28 PM, said:

It's usually a good idea to avoid tearing down a gui, as opposed to having it repaint itself. It sounds as though you're doing the former. Try attaching your code as a zip file


You're correct, I was doing the former. This was my first GUI application so I've learned how to make things better next time by taking your advice and repainting (my graph drawer does just that and doesn't suffer from such glitches).

Since everything works fine with setVisible(false) I'll just leave it that way, as I'm happy with the way things turned out, and it looks like only one extra object was created. So it sounds like what I encountered was a bug that was a result of my own bad design. I really just wanted to see if what I had done was clearly wrong from a syntax perspective.

Thanks everyone for your help.
Was This Post Helpful? 0
  • +
  • -

#8 pbl  Icon User is offline

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

Reputation: 8342
  • View blog
  • Posts: 31,880
  • Joined: 06-March 08

Re: "New" Swing JPanel Glitch

Posted 28 July 2012 - 12:32 PM

public void updateGUI()
	{ 
		if(!(components.isStopText()))
		{    
			info = new DrawInfoPanel(components.getObject()); 
			setInfoPanel();
		} 
		
		grapher.updateGraph(components.getObject(), components.isIdealPath(),
									 components.isRealtime());
	}


what are you doing with this info = new DrawInforPanel ?
Never a good idea top create JComponent like JPanel on the fly, you may screw up many layout quite fast with that if you don't really know what you are doing
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1