1 2 3 package com.sshtools.ui.swing; 4 5 import java.util.StringTokenizer ; 6 7 import java.awt.Font ; 8 import java.awt.GridBagConstraints ; 9 import java.awt.GridBagLayout ; 10 import javax.swing.JLabel ; 11 import javax.swing.JPanel ; 12 13 19 20 public class MultilineLabel extends JPanel { 21 22 24 private GridBagConstraints constraints; 25 26 private String text; 27 28 31 32 public MultilineLabel() { 33 34 this(""); 36 } 37 38 43 44 public MultilineLabel(String text) { 45 46 super(new GridBagLayout ()); 47 48 constraints = new GridBagConstraints (); 49 50 constraints.anchor = GridBagConstraints.NORTHWEST; 51 52 constraints.fill = GridBagConstraints.NONE; 53 54 setText(text); 55 56 } 57 58 63 64 public void setFont(Font f) { 65 66 super.setFont(f); 67 68 for (int i = 0; i < getComponentCount(); i++) { 69 70 getComponent(i).setFont(f); 71 72 } 73 74 } 75 76 81 82 public void setText(String text) { 83 84 this.text = text; 85 86 removeAll(); 87 88 StringTokenizer tok = new StringTokenizer (text, "\n"); 90 constraints.weighty = 0.0; 91 92 constraints.weightx = 1.0; 93 94 while (tok.hasMoreTokens()) { 95 96 String t = tok.nextToken(); 97 98 if (!tok.hasMoreTokens()) { 99 100 constraints.weighty = 1.0; 101 102 } 103 104 UIUtil.jGridBagAdd(this, new JLabel (t), constraints, 105 106 GridBagConstraints.REMAINDER); 107 108 } 109 110 revalidate(); 111 112 repaint(); 113 114 } 115 116 121 122 public String getText() { 123 124 return text; 125 126 } 127 128 } 129 | Popular Tags |