Tuesday 15 January 2013

java - how to add Jlabel to Jpanel outside the constructor? -


I am able to use AD (new jellybell) to label a label on my Jepelal within the Zipal constructor, but once I am using the second function () adding, the label is not shown on the panel . what I have done? Public class display panel, JPNL (JLABB headfield = new JLab ("Choose a file to prepare the report".) Is expanded; JLabelDefined = new JLabel ("123"); JLabble EarthFilled = New JLab (""); JLab Minfield = New JLab (""); JLab Maxfield = New JLab (""); JLabel stdDevField = New JLab (""); Public DisplayPanel () {super (); setBackground (Color.white); SetLayout (new grid layout (6, 1)); add (headerField); ** // add (new jlabble ("123");; ** This will work ** Public Zero Setfield (Datamanager) {dateField.setText (d.getStartDate) + "-" + d.getEndDate ()); MeanField.setText ("Meaning: "+ D.getMean ()); MinField.setText (" Min: "+ d.getMin ()); MaxField.setText (" Max: "+ d.getMax ()); StdDevField.setText (" STD Dev: " + D.getStdev ()); This.add (New JLabel ("123"); ** // This does not work **}

After the new added component appears after you see the JPNL to get any, you modify () Calling is usually done On repaint () . The reason is that add (new jlabble ("123");

works in the constructor JPanel valid when is added to its container, usually in an JFrame is. Adding labels to the initialization step is easy because you only have to call JLabel # setText and no reviewed / reprint call is not necessary.

No comments:

Post a Comment