Label dynamic resizingTag(s): AWT


If there is no Layout Manager installed, try something like this:
aLabel.setText ("A very long label");
aLabel.resize (aLabel.preferredSize());
With a Layout Manager, you have to validate() the layout to redraw the components invalidated.
Label aLabel = new Label("short label");
aLabel.setText ("A very long label");
this.validate();
While this method works in Netscape or the Appletviewer, on IE4/5 there is no resizing. You may want to try this instead (thanks to Dan for the tip):
Label aLabel = new Label("short label");
aLabel.setText(""A very long label");
aLabel.invalidate(); // make sure the component is marked as non-valid
this.validate();
This example how to change a Label or a String drawn with the drawString method by clicking on a button.
import java.awt.*;
import java.awt.event.*;
import java.applet.*;

 public class TestPaint extends Applet
   implements ActionListener {
   MyPanel p;
   Label l;  
   Button b1, b2;
   TextField t1, t2;

   public void init() {
     setLayout(new FlowLayout());
     t1 = new TextField(10);
     b1 = new Button("Change Label");
     add(t1); add(b1);
     t2 = new TextField(10);
     b2 = new Button("Change drawString");
     add(t2); add(b2);   
     l = new Label("label text");
     add(l);

     // a Panel with a drawString call
     p = new MyPanel();
     add(p);
     b1.addActionListener(this);
     b2.addActionListener(this);
     }

   public void actionPerformed(ActionEvent e) {
     if (e.getSource() == b1) {
       l.setText(t1.getText());
       l.invalidate();
       validate();
       }
     if (e.getSource() == b2) {
       p.someString = t2.getText();
       p.repaint();
       }
     }
   }
   
   class MyPanel extends Panel {
     String someString = "drawstring";

     MyPanel() { super(); }

     public void paint (Graphics g) {
       g.drawString(someString, 10,50);
       }
       
     public Dimension getPreferredSize() {
       return new Dimension (100,100);
       }
   }

blog comments powered by Disqus