Java JTable application cases and jtable application cases

Source: Internet
Author: User

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.