Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Jlabel not showing

 
Carl Kabbe
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have a panel with a JLabel. I have a button which when clicked, removes the JLabel and adds a new JLabel. The problem is after the JLabel is removed and readded, it does not show.

I have narrowed the problem to the following code:


/*
* ReservationCalendarTest.java
*
* Created on November 23, 2005, 5:55 PM
*/

package com.tsr.beans.leaguescheduling;

/**
*
* @author Administrator
*/

import java.util.*;


public class ReservationCalendarTest extends javax.swing.JFrame
{

/** Creates new form ReservationCalendarTest */
public ReservationCalendarTest()
{
initComponents();

displayMonths(false);

}


private void displayMonths(boolean removeOld) {

if( removeOld ) {
jPanelCalendars.remove(0);
}

jPanelCalendars.add(new javax.swing.JLabel("HaHaHa"));
repaint();
}




/** This method is called from within the constructor to
* initialize the form.
* WARNING: Do NOT modify this code. The content of this method is
* always regenerated by the Form Editor.
*/
// <editor-fold defaultstate="collapsed" desc=" Generated Code ">
private void initComponents()
{
jPanelDates = new javax.swing.JPanel();
jLabel1 = new javax.swing.JLabel();
jPanelDateEntryAndButtons = new javax.swing.JPanel();
jButtonBackOneMonth = new javax.swing.JButton();
jPanelCalendars = new javax.swing.JPanel();

getContentPane().setLayout(new javax.swing.BoxLayout(getContentPane(), javax.swing.BoxLayout.X_AXIS));

setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
jPanelDates.setLayout(new javax.swing.BoxLayout(jPanelDates, javax.swing.BoxLayout.Y_AXIS));

jLabel1.setText("Dates");
jPanelDates.add(jLabel1);

jButtonBackOneMonth.setText("<");
jButtonBackOneMonth.addActionListener(new java.awt.event.ActionListener()
{
public void actionPerformed(java.awt.event.ActionEvent evt)
{
jButtonBackOneMonthActionPerformed(evt);
}
});

org.jdesktop.layout.GroupLayout jPanelDateEntryAndButtonsLayout = new org.jdesktop.layout.GroupLayout(jPanelDateEntryAndButtons);
jPanelDateEntryAndButtons.setLayout(jPanelDateEntryAndButtonsLayout);
jPanelDateEntryAndButtonsLayout.setHorizontalGroup(
jPanelDateEntryAndButtonsLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
.add(jButtonBackOneMonth)
);
jPanelDateEntryAndButtonsLayout.setVerticalGroup(
jPanelDateEntryAndButtonsLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
.add(jButtonBackOneMonth)
);
jPanelDates.add(jPanelDateEntryAndButtons);

jPanelCalendars.setLayout(new javax.swing.BoxLayout(jPanelCalendars, javax.swing.BoxLayout.X_AXIS));

jPanelCalendars.setMaximumSize(new java.awt.Dimension(999999, 9999999));
jPanelCalendars.setMinimumSize(new java.awt.Dimension(600, 200));
jPanelCalendars.setPreferredSize(new java.awt.Dimension(700, 300));
jPanelDates.add(jPanelCalendars);

getContentPane().add(jPanelDates);

pack();
}// </editor-fold>

private void jButtonBackOneMonthActionPerformed(java.awt.event.ActionEvent evt)
{
// TODO add your handling code here:
displayMonths(true);
}

/**
* @param args the command line arguments
*/
public static void main(String args[])
{
java.awt.EventQueue.invokeLater(new Runnable()
{
public void run()
{
new ReservationCalendarTest().setVisible(true);
}
});
}

// Variables declaration - do not modify
private javax.swing.JButton jButtonBackOneMonth;
private javax.swing.JLabel jLabel1;
private javax.swing.JPanel jPanelCalendars;
private javax.swing.JPanel jPanelDateEntryAndButtons;
private javax.swing.JPanel jPanelDates;
// End of variables declaration

}

Anyone have any ideas?

TIA.

Carl
 
Ulf Dittmer
Rancher
Posts: 42969
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You need to call validate() -which lays out the components anew- instead of repaint() (which merely redraws them).

Of course, the second time you call the displayMonths method, this wouldn't work, because the new label would not be component no. 0.

Why don't you store a reference to the label, and call its setText method to update it?
[ November 24, 2005: Message edited by: Ulf Dittmer ]
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic