JTable内の行を操作する(特定行の削除など)-- ListSelectionModel, ListSelectionListenerなどの使い方

戻る



import javax.swing.*;
import javax.swing.event.*;
import javax.swing.table.*;
import java.awt.event.*;
import java.awt.*;
import java.util.*;

public class RowManagement extends JFrame implements ActionListener {

    private JTable table;
    private DefaultTableModel model;
    private String[][] tabledata = {
                                       {"日本", "2勝", "0敗", "1分"},
                                       {"クロアチア", "3勝", "1敗", "0分"},
                                       {"ブラジル", "1勝", "2敗", "1分"},
                                       {"オーストラリア", "0勝", "3敗", "0分"}};

    private String[] columnNames = {"国名", "勝", "負", "引分け"};

    public static void main( String[] args ) {
        RowManagement test = new RowManagement( "RowManagement" );

        test.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
        test.setVisible( true );
    }

    public RowManagement( String title ) {
        setTitle( title );
        setBounds( 10, 10, 300, 200 );

        model = new DefaultTableModel();
        model.setColumnIdentifiers( columnNames );

        for ( int i = 0;i < tabledata.length;i++ ) {
            model.addRow( tabledata[ i ] );
        }

        table = new JTable( model );

        //
        // table.setSelectionMode( ListSelectionModel.SINGLE_SELECTION );
        //

        ListSelectionModel lsModel = table.getSelectionModel();
        lsModel.addListSelectionListener( new ListSelectionListener() {
                                              public void valueChanged( ListSelectionEvent e ) {

                                                  //Ignore extra messages.
                                                  if ( e.getValueIsAdjusting() )
                                                      return ;

                                                  ListSelectionModel lsm =
                                                      ( ListSelectionModel ) e.getSource();
                                                  if ( lsm.isSelectionEmpty() ) {
                                                      //no rows are selected
                                                  } else {
                                                      int selectedRow = lsm.getMinSelectionIndex();
                                                      //selectedRow is selected
                                                      System.out.println( "行番号=" + selectedRow + " が、選択されました!" );
                                                  }

                                              }
                                          }
                                        );


        JScrollPane sp = new JScrollPane( table );
        sp.setPreferredSize( new Dimension( 250, 100 ) );

        JPanel p = new JPanel();
        p.add( sp );

        JButton deleteButton = new JButton( "削除" );
        deleteButton.setActionCommand( "DELETE" );
        deleteButton.addActionListener( this );

        JButton listButton = new JButton( "リスト表示" );
        listButton.setActionCommand( "LIST" );
        listButton.addActionListener( this );

        JPanel lowerPanel = new JPanel();
        lowerPanel.add( deleteButton );
        lowerPanel.add( listButton );

        getContentPane().add( p, BorderLayout.CENTER );
        getContentPane().add( lowerPanel, BorderLayout.SOUTH );
    }

    public void actionPerformed( ActionEvent event ) {
        String command = ( String ) event.getActionCommand();

        if ( "LIST".equals( command ) ) {
            ListSelectionModel lsModel = table.getSelectionModel();
            for ( int i = 0; i < table.getRowCount(); i++ ) {
                System.out.println( lsModel.isSelectedIndex( i ) );

                // System.out.println( model.getColumnCount() );
                for ( int j = 0;j < model.getColumnCount();j++ ) {
                    System.out.print( "\t" + model.getValueAt( i, j ) );
                }

                System.out.println( );
            }
        }

        if ( "DELETE".equals( command ) ) {
            // 選択されている行の削除
            ListSelectionModel lsModel = table.getSelectionModel();
            // modelのrowCountでまわさないこと⇒矛盾が生じる
            for ( int i = 0; i < table.getRowCount(); i++ ) {
                if ( lsModel.isSelectedIndex( i ) ) {
                    model.removeRow( i );
                }
            }

            table.repaint();
        }
    }
}

/**
ListSelectionModel.SINGLE_SELECTION
  1 回に 1 つのリストインデックスのみが選択できます。このモードでは、
  setSelectionInterval および addSelectionInterval メソッドは同等となり
  、2 番目のインデックス引数のみが使用されます。
 
ListSelectionModel.SINGLE_INTERVAL_SELECTION
  連続するインデックス区間を 1 回に 1 つ選択できます。このモードでは、
  setSelectionInterval メソッドと addSelectionInterval メソッドは同等で
  す。
 
ListSelectionModel.MULTIPLE_INTERVAL_SELECTION
  このモードでは、選択対象に制限はありません。これがデフォルトです。
*/


戻る inserted by FC2 system