0

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.

Joop Eggen
  • 107,315
  • 7
  • 83
  • 138
Zakaria Marrah
  • 755
  • 6
  • 15
  • 28
  • plz paste ur interframe code.I want to see how u create jinternalframe – Biswajit Mar 15 '13 at 11:52
  • 1
    Stuff like that is usually updating the GUI outside of the event dispatch thread (EDT). Basically, if you're updating in any thread other than Swing's dedicated EDT, you can't guarantee it will work. Does you code contain any other threads? – Ash Mar 15 '13 at 11:53
  • please show the code, but are you setting DISPOSE_ON_CLOSE option ??? – Bruce Martin Mar 15 '13 at 11:55
  • 1
    It seems you assigned a null renderer to one of the columns of the table, or that one of the column classes returned by the model of the table is null. – JB Nizet Mar 15 '13 at 11:55
  • @JB Nizet this is quite common issue about prepareRenderer and updates to the model out of EDT, – mKorbel Mar 15 '13 at 12:27
  • And what am i suppose to do, to get rid of this issue? – Zakaria Marrah Mar 15 '13 at 12:30
  • Try calling setVisible(false) instead of invoking dispose. Also, try recreating a new frame as required instead of trying to reuse it – MadProgrammer Mar 15 '13 at 12:31
  • About invoking the JinternalFrame, a new instance is created of course, and i did what you just told me about calling the setVisible(false) in the closeOperation, but the situation remains the same. – Zakaria Marrah Mar 15 '13 at 12:35
  • When i select a checkBox that is listed in the middle of the JTable the NPE is triggered, if i select all of the rows or the first one it works fine, does it mean that table model that causes the problem?. – Zakaria Marrah Mar 15 '13 at 12:40
  • 1
    The second frame should be a JDialog. Anything that pops up and is dismissed with a button press is a dioalog. The first frame should be a JPanel. Your application should have one and only one JFrame. – Gilbert Le Blanc Mar 15 '13 at 12:52
  • Okay thank you, i am working on it. – Zakaria Marrah Mar 15 '13 at 12:57
  • See also [The Use of Multiple JFrames, Good/Bad Practice?](http://stackoverflow.com/a/9554657/418556) – Andrew Thompson Mar 16 '13 at 02:48

1 Answers1

3

@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
Community
  • 1
  • 1
mKorbel
  • 109,525
  • 20
  • 134
  • 319