简体   繁体   English

创建JTable行标题

[英]Creating JTable Row Header

I'm new to JTable.I'm working in Swings using JTable & Toplink(JPA). 我是JTable的新手,正在使用JTable和Toplink(JPA)在Swings中工作。 I have two buttons "add Row", "Del Row" and I have some records displayed from db. 我有两个按钮“添加行”,“删除行”,并且有一些从db显示的记录。 when ever "add row" is clicked a new record, row header should be added to JTable with an auto increment number displayed in sequential order to the JTable Row Header. 当单击“添加行”的新记录时,应将行标头添加到JTable,并在JTable行标头上按顺序显示自动递增编号。

During deletion using "Del row " button the record has to be deleted & not its corresponding header so that next rows got updated to the previous headers & the auto increment number remain unchanged and always be in sequence. 在删除过程中,使用“ Del row”按钮必须删除记录,而不是删除其对应的标题,以便使下一行更新为先前的标题,并且自动递增编号保持不变,并且始终保持顺序。

please help me on this regard. 请在这方面帮助我。

Thanks in advance, Chandu 预先感谢,Chandu

As you are new to JTable , I'd start with How to Use Tables . 当您不熟悉JTable ,我将从如何使用表开始 As suggested there, I'd extend AbstractTableModel , overriding getValueAt() , setValueAt() , getRowCount() , etc. to use the corresponding JPA entity and controller classes and methods. 如此处所建议,我将扩展AbstractTableModel ,覆盖getValueAt()setValueAt()getRowCount() 等,以使用相应的JPA实体和控制器类和方法。 The NetBeans IDE, for example, can generate such classes automatically. 例如,NetBeans IDE可以自动生成此类。 There is also a feature to bind the classes to Swing components, including JTable and JList , but I haven't tried it. 还有一个将类绑定到Swing组件的功能,包括JTableJList ,但我还没有尝试过。

If the row id is an auto increment number managed by the database, I'm not sure there's a reliable way to maintain the appearance of sequential order in a multi-user environment. 如果行ID是数据库管理的自动递增数字,则我不确定在多用户环境中是否存在可靠的方式来维持顺序顺序的外观。 If the field is displayed at all, isCellEditable() should probably return false for that column. 如果完全显示该字段,则isCellEditable()应该对该列返回false Is that what you mean by Row Header? 那是行标题的意思吗?

Addendum: See also What's the best way get the ID of the item just inserted? 附录:另请参见获取刚刚插入的商品ID的最佳方法是什么?

Do you mean like this? 你是这个意思吗

likeThis http://img705.imageshack.us/img705/8920/screenshot7ry.png 像这样http://img705.imageshack.us/img705/8920/screenshot7ry.png

When a new record is added its id is set as a row header ( as in records, 1,2,3 ) and when a record is deleted, the row header remains, but the information is gone ( as in records 4,5 which use to have data, but after being removed only leave the id ) 当添加新记录时,其id设置为行标题 (如记录1,2,3中),而删除记录时, 行标题保留,但信息消失了(如记录4,5中,用于拥有数据,但在删除后仅保留id)

IF that's what you mean. 如果那是你的意思。

What you have to do is create two table models, one wrapping the other. 您要做的是创建两个表模型,一个表模型包装另一个。

  • The only functionality of the wrapper will be to show an extra column ( the id ) and keep an internal "copy" of the data. 包装器的唯一功能是显示额外的列(id)并保留数据的内部“副本”。

  • When a row is added, the wrapper will get a new record, it will assign a new id and then it will take all the fields and copy the value into his own data and then will forward it to the wrapped table model. 当添加一行时,包装器将获得一个新记录,它将分配一个新的ID,然后它将获取所有字段并将值复制到自己的数据中,然后将其转发给包装的表模型。

  • When a row is removed, the wrapper will not remove its own record, it will only clean up ( set to "" ) the column values and will keep the id column. 当删除一行时,包装器将不会删除其自己的记录,只会清理(设置为“”)列值,并保留id列。 Then it will forward the event to the wrapped column which in turn will actually remove the row. 然后它将事件转发到包装的列,包装的列实际上将删除该行。

Something like the following noncompiling-java 类似于以下非编译java

 // Wrapper table model   
 class IdTableModel implements TableModelListener {
      TableModel original;

      // Analog to: Object[][]
      List<List<Object> data = new ArrayList<List<Object>();

      // will take a copy of the data in original
      public IdTableModel( TableModel original ) {
          this.original = original;
          // fillData with values from original
         for( int row = 0 ; row < original.getRowCount(); row++ ) {
              List<Object> shadowRow = new ArrayList<Object>();
              shadowRow.add( row ); // the id
              // copy the values from original to this shadow
              for( int column = 0 ; column < original.getColumnCount(); column++ ) {
                  shadowRow.add( original.getValueAt(row,i));
              }
              // add the shadow to the data
              data.add( shadowRow );
          }
          original.addTableModelListener( this );
      }

      // for Object getValueAt( x, y ) get the data from your "data" not from the original

       ....
       //Here's the magic. this wapper will be a listener of the original
       // then this method will be invoked when the original gets more data 
       // or when it is removed.....
       public void tableChanged( TableEvent e )  {
           int row = e.getFirstRow();
           List<Object> shadowRow = data.get( row);
           switch( e.getType() ) {
               case TableEvent.INSERT:
                    if( ( shadowRow =) == null )  {
                        // there wasn't shadow data at that position
                        data.add( ( shadowRow = new ArrayList<Object>()));
                     }
                     // takevalues from original 
                     shadowRow.add( row ); // the id
                     for( int i = 0 ; i < original.getColumnCount();i++ ) {
                          shadowRow.add( original.getValueAt(row,i));
                     }
               break;
               case TableEvent.UPDATE:
                      // similar to insert, but you don't create a new row        
                     // takevalues from original 
                     for( int i = 0 ; i < original.getColumnCount();i++ ) {
                          shadowRow.set( i+1, original.getValueAt(row,i));
                     }
               break;
               case TableEvent.DELETE:
                      // You don't delete your shadow rows, but just clean it up
                     for( int i = 0 ; i < original.getColumnCount();i++ ) {
                          shadowRow.set( i+1, "");
                     }

               break;

       } 


       // forward calls to the original
      public int getColumnCount(){
           return original.getColumnCount()+1;
      }
      public String getColumnName( int columnIndex ) {
          if( columnIndex == 0 ) {
              return "id";
           } else {
               return original.getColumnName( columnIndex -1 );
           }
       }

       // and so on for, isEditable etc. etc

 }

Well that was much more code that what I initially wanted. 嗯,那是我最初想要的更多代码。

The idea is to have a table model wrapper that adds that fictional id column for you and don't delete it, you just use it like this: 这个想法是要有一个表模型包装器,为您添加该虚构id列,并且不要删除它,您可以像这样使用它:

  JTable yourTable = new JTable( new IdTableModel( originalTableModel ));

If nothing of what I said make sense to you, start reading this: How to use tables 如果我所说的话对您没有意义,请开始阅读以下内容: 如何使用表格

您可能正在寻找此页面: http : //www.chka.de/swing/table/row-headers/JTable.html

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM