Zakaria Marrah
Zakaria Marrah

Reputation: 765

How to resolve a Swing GUI error?

i am working on a swing application, everything seems to be working just good, but recently i have faced some problems with GUI. The problem is that when i open a JinternalFrame inside another one it works fine, but when i open it again the child JintenalFrame loses control of itself, the layout is lost or maybe destroyed i got some photos that would explain the problem clearly :

enter image description here

Here it is the first JinternalFrame and when i click on the update button the second one shows up inside of it like:

enter image description here

But when i close the seconde one and open it again the problem appears like that: enter image description here

And the IDE triggers an NPE mentioning:

Hibernate: select modalite0_.id_mod as id1_6_, modalite0_.libele as libele6_ from   
Modalite modalite0_
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at javax.swing.JTable.prepareRenderer(JTable.java:5735)
at javax.swing.plaf.basic.BasicTableUI.paintCell(BasicTableUI.java:2114)
at javax.swing.plaf.basic.BasicTableUI.paintCells(BasicTableUI.java:2016)
at javax.swing.plaf.basic.BasicTableUI.paint(BasicTableUI.java:1812)
at javax.swing.plaf.ComponentUI.update(ComponentUI.java:161)
at javax.swing.JComponent.paintComponent(JComponent.java:778)
at javax.swing.JComponent.paint(JComponent.java:1054)
at javax.swing.JComponent.paintChildren(JComponent.java:887)
at javax.swing.JComponent.paint(JComponent.java:1063)
at javax.swing.JViewport.paint(JViewport.java:731)
at javax.swing.JComponent.paintChildren(JComponent.java:887)
at javax.swing.JComponent.paint(JComponent.java:1063)
at javax.swing.JComponent.paintChildren(JComponent.java:887)
at javax.swing.JComponent.paint(JComponent.java:1063)
at javax.swing.JComponent.paintChildren(JComponent.java:887)
at javax.swing.JComponent.paint(JComponent.java:1063)
at javax.swing.JComponent.paintChildren(JComponent.java:887)
at javax.swing.JComponent.paint(JComponent.java:1063)
at javax.swing.JLayeredPane.paint(JLayeredPane.java:585)
at javax.swing.JComponent.paintChildren(JComponent.java:887)
at javax.swing.JComponent.paint(JComponent.java:1063)
at javax.swing.JComponent.paintChildren(JComponent.java:887)
at javax.swing.JComponent.paint(JComponent.java:1063)
at javax.swing.JComponent.paintChildren(JComponent.java:887)
at javax.swing.JComponent.paint(JComponent.java:1063)
at javax.swing.JLayeredPane.paint(JLayeredPane.java:585)
at javax.swing.JComponent.paintToOffscreen(JComponent.java:5221)

So i am asking if there a way to track this error down?

I am using Netbeans7.2 as an ide with jre 7.

Upvotes: 0

Views: 2041

Answers (1)

mKorbel
mKorbel

Reputation: 109815

@Zakaria Marrah

  • create JDialog for popup window, only one JDialog for whole JVM Instance, by assuming that in one moment can be visible only one popup window, set proper JDialog.setDefaultCloseOperations(HIDE_ON_CLOSE)

  • create JPanel that nested all JComponents

  • add/intialize XxxTableModel to JTable, wrong coded renderer and with empty model causing exceptions from prepareRenderer for all harcoded link, castings, value comparitions, then to have to test if model contains data inside prepareRenderer if passed then lets whatever in renderer works

  • wrong coded renderer and together with updates out of EDT causing exceptions from prepareRenderer for all harcoded link, castings, value comparitions,


  • now you'll play only with visibily for JDialog and its contents

  • hide JDialog,

  • use CardLayout in the case that there are a few views, set, to switch to the proper card,

  • remove all value from JComponent(s), or remove all data from model(s),

  • never to reset, reinitialize or recreate any on Objects,

  • use SwingWorker for data from any external souces, listening by using PropertyChangeListener, have to test, to check for exceptions by using get() in done()

  • set value (or by using batch in process, publish) in done() to the JComponents, its models

  • then call JDialog.pack(), move with JDialog to the proper coordinates on the screeen (relative to the JComponents), to the Point, thenafter to show JDialog wrapped in invokeLater(required)


  • for production code to use Runnable#Thread, but all output to the Swing GUI must be wrapped into invokeLater, you can to ignore that a few methods are declared for Swing as thread safe

Upvotes: 3

Related Questions