Java JTable application cases and jtable application cases
The Code is as follows:
Import Java. awt. component; import java. awt. dimension; import java. awt. fontMetrics; import javax. swing. JLabel; import javax. swing. JTable; import javax. swing. UIManager; import javax. swing. table. defaultTableCellRenderer; import javax. swing. table. JTableHeader; import javax. swing. table. tableColumnModel; public class Main extends DefaultTableCellRenderer {private static final long serialVersionUID = 1L; public Component getTableCellRendererComponent (JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) {JTableHeader header = table. getTableHeader (); setForeground (header. getForeground (); setBackground (header. getBackground (); setFont (header. getFont (); setOpaque (true); setBorder (UIManager. getBorder ("TableHeader. cellBorder "); // obtain the column width TableColumnModel columnModel = table. getColumnModel (); int width = columnModel. getColumn (column ). getWidth (); value = getShowValue (value. toString (), width); setText (value. toString (); setSize (new Dimension (width, this. getHeight (); setHorizontalAlignment (JLabel. CENTER); return this;} private Object getShowValue (String value, int colWidth) {FontMetrics fm = this. getFontMetrics (this. getFont (); int width = fm. stringWidth (value. toString (); int test = fm. stringWidth ("good"); System. out. println (test * value. length (); System. out. println (width); if (width <colWidth) {return value;} StringBuffer sb = new StringBuffer ("
Running result
<Ignore_js_op>
Http://java.662p.com/thread-3683-1-1.html