jtable冻结列

原文:https://community.oracle.com/thread/1360505

package gpt.presentation.gui.test;


import java.awt.*;
import java.awt.event.*;
import java.awt.image.BufferedImage;

import javax.swing.*;
import javax.swing.table.*;
import javax.swing.event.*;
/**
* @author RONAS1
*
* To change the template for this generated type comment go to
* Window>Preferences>Java>Code Generation>Code and Comments
*/
public class FreezeColumnsTest extends JFrame {



     Object[][] data;
     Object[] column;
     TTable table;
     

     public FreezeColumnsTest() {
          super( "Fixed Column Example" );

          data = new Object[][]{
          { "a","b","c","","","", "" , ""},
          { "a","b","c","","","", "", ""},
          { "a","b","c","","","", "", ""},
          { "a","b","c","d","","","", ""},
          { "a","b","c","","e","","", ""},
          { "a","b","c","","","f","", ""},
          { "a","b","c","","","","g",""},
          { "a","b","c","","","","","h"}};
          column = new Object[]{"A","B","C","D","E","F", "G", "H"};

     AbstractTableModel model = new AbstractTableModel() {
          public int getColumnCount() { return column.length; }
          public int getRowCount() { return data.length  ;}
          public String getColumnName(int col) {
          return (String)column[col]; 
          }
          public Object getValueAt(int row, int col) { 
          return data[row][col]; 
          }
          public void setValueAt(Object obj, int row, int col) { 
          data[row][col] = obj; 
          }
          public boolean CellEditable(int row, int col) { 
          return true; 
          }
     };
     
     table = new TTable( model );
     
     JPanel p1 = new JPanel();
     p1.setLayout(new BorderLayout());
     p1.add(table,BorderLayout.CENTER );
     
     JPanel p2 = new JPanel();
          p2.setLayout(new BorderLayout());
          p2.add(table,BorderLayout.CENTER );
     
     
     


     table.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
     table.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);

     
     JScrollPane scroll = new JScrollPane( table);

     scroll.getViewport().setScrollMode(JViewport.SIMPLE_SCROLL_MODE);



     getContentPane().add( scroll, BorderLayout.CENTER);

     }

     public static void main(String[] args) {
          FreezeColumnsTest frame = new FreezeColumnsTest();
          frame.addWindowListener( new WindowAdapter() {
          public void windowClosing( WindowEvent e ) {
               System.exit(0);
               }
               });
          frame.pack();
          frame.setVisible(true);
     }
}







class TTable extends JTable{
     
     public TTable(TableModel dm) {
          super(dm);
          setTableHeader(new TTableHeader(getColumnModel()));
          
     }
     
     
     public void paintComponent(Graphics g) {

          super.paintComponent(g);
          
          int w = this.getColumnModel().getColumn(0).getWidth();
          
          Point pt = getLocation();
          
          if(pt.x < 0){
               
               BufferedImage image = new BufferedImage(w, getHeight(), BufferedImage.TYPE_INT_RGB);
               Graphics2D g2d = image.createGraphics();
               g2d.setClip( 0,0,w,getHeight());
               super.paintComponent(g2d);
               int ox = Math.abs(pt.x);
               g.drawImage(image,ox,0,null);
               g2d.dispose();
               int lx = ox + w;
               g.drawLine(lx, 0, lx, getHeight());
               
               
          }
          else{
               int ox = Math.abs(pt.x);
               int lx = ox + w;
               g.drawLine(lx, 0, lx, getHeight());
     
          }
          

     }
     
}

class TTableHeader extends JTableHeader{
     
     
     TTableHeader(TableColumnModel md){
          super(md);
     }
     
     public void paintComponent(Graphics g) {

               super.paintComponent(g);
          
               int w = this.getColumnModel().getColumn(0).getWidth();
          
               Point pt = getLocation();
          
               if(pt.x < 0){
               
                    BufferedImage image = new BufferedImage(w, getHeight(), BufferedImage.TYPE_INT_RGB);
                    Graphics2D g2d = image.createGraphics();
                    g2d.setClip( 0,0,w,getHeight());
                    super.paintComponent(g2d);
                    int ox = Math.abs(pt.x);
                    g.drawImage(image,ox,0,null);
                    g2d.dispose();
          
                    int lx = ox + w;
                    g.drawLine(lx, 0, lx, getHeight());
               
               }
               else{
                    int ox = Math.abs(pt.x);
                    int lx = ox + w;
                    g.drawLine(lx, 0, lx, getHeight());
                    
               }

          }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

AI算法网奇

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值