2012-09-10 4 views
1

JTable을 만들었지 만 어떤 이유로 행 선택이 작동하지 않습니다 (Mac 및 Nimbus L & F를 사용 중입니다).행 선택이 JTable에서 작동하지 않습니다.

/* 
* To change this template, choose Tools | Templates 
* and open the template in the editor. 
*/ 
package nysemarketpick; 

import java.awt.event.ActionEvent; 
import java.awt.event.ActionListener; 
import java.util.Vector; 
import javax.swing.event.TableModelEvent; 
import javax.swing.event.TableModelListener; 
import javax.swing.table.DefaultTableModel; 
import javax.swing.table.TableModel; 
import javax.swing.table.TableRowSorter; 

/** 
* 
* @author Administrator 
*/ 
public class PortfolioForm extends javax.swing.JFrame { 

    /** 
    * Creates new form PortfolioForm 
    */ 
    public PortfolioForm() { 
     initComponents(); 
    } 

    /** 
    * This method is called from within the constructor to initialize the form. 
    * WARNING: Do NOT modify this code. The content of this method is always 
    * regenerated by the Form Editor. 
    */ 
    @SuppressWarnings("unchecked") 
    // <editor-fold defaultstate="collapsed" desc="Generated Code"> 
    private void initComponents() { 

     jPanel1 = new javax.swing.JPanel(); 
     jbtAddRow = new javax.swing.JButton(); 
     jbtDeleteRow = new javax.swing.JButton(); 
     jScrollPane1 = new javax.swing.JScrollPane(); 
     jTable1 = new javax.swing.JTable(); 

     setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE); 

     jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder("Row functions")); 

     jbtAddRow.setText("Add New Row"); 

     jbtDeleteRow.setText("Delete Selected Row"); 

     org.jdesktop.layout.GroupLayout jPanel1Layout = new org.jdesktop.layout.GroupLayout(jPanel1); 
     jPanel1.setLayout(jPanel1Layout); 
     jPanel1Layout.setHorizontalGroup(
      jPanel1Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING) 
      .add(jPanel1Layout.createSequentialGroup() 
       .add(jPanel1Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING) 
        .add(jPanel1Layout.createSequentialGroup() 
         .add(34, 34, 34) 
         .add(jbtAddRow)) 
        .add(jPanel1Layout.createSequentialGroup() 
         .add(16, 16, 16) 
         .add(jbtDeleteRow))) 
       .addContainerGap(40, Short.MAX_VALUE)) 
     ); 
     jPanel1Layout.setVerticalGroup(
      jPanel1Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING) 
      .add(jPanel1Layout.createSequentialGroup() 
       .addContainerGap() 
       .add(jbtAddRow) 
       .add(18, 18, 18) 
       .add(jbtDeleteRow) 
       .addContainerGap(26, Short.MAX_VALUE)) 
     ); 

     jTable1.setAutoCreateRowSorter(true); 
     jTable1.setModel(new javax.swing.table.DefaultTableModel(
      new Object [][] { 
       {null, null, null, null, null} 
      }, 
      new String [] { 
       "Stock Symbol", "Stock Name", "Shares", "Value (in Dollars)", "Total Value" 
      } 
     ) { 
      Class[] types = new Class [] { 
       java.lang.String.class, java.lang.String.class, java.lang.Integer.class, java.lang.Double.class, java.lang.Double.class 
      }; 
      boolean[] canEdit = new boolean [] { 
       true, false, true, true, false 
      }; 

      public Class getColumnClass(int columnIndex) { 
       return types [columnIndex]; 
      } 

      public boolean isCellEditable(int rowIndex, int columnIndex) { 
       return canEdit [columnIndex]; 
      } 
     }); 
     tableModel = (DefaultTableModel)jTable1.getModel(); 
     jTable1.setColumnSelectionAllowed(true); 
     jTable1.setRowSelectionAllowed(true); 
     jTable1.setSelectionMode(javax.swing.ListSelectionModel.SINGLE_SELECTION); 
     jScrollPane1.setViewportView(jTable1); 
     jTable1.getColumnModel().getSelectionModel().setSelectionMode(javax.swing.ListSelectionModel.SINGLE_SELECTION); 
     //add action listeners 
     jbtAddRow.addActionListener(new ActionListener(){ 
      public void actionPerformed(ActionEvent e) 
      { 
       if (jTable1.getSelectedRow() >= 0) 
       tableModel.insertRow(jTable1.getSelectedRow(), new Vector()); 
       else 
       tableModel.addRow(new Vector()); 
      } 
     }); 
     jbtDeleteRow.addActionListener(new ActionListener(){ 
      public void actionPerformed(ActionEvent e) 
      { 
       if (jTable1.getSelectedRow() >= 0) 
       tableModel.removeRow(jTable1.getSelectedRow()); 
      } 
     }); 
     //add other listeners 
     tableModel.addTableModelListener(new TableModelListener(){ 
      public void tableChanged(TableModelEvent e) 
      { 
       DefaultTableModel model = (DefaultTableModel)e.getSource(); 
       //Object data = model.getValueAt(e.getFirstRow(), e.getColumn()); 
       if (e.getColumn() == 0) 
       { 
        Object data = model.getValueAt(e.getFirstRow(), e.getColumn()); 
        String stockSymbol = (String)data; 
        XMLService2 myService = new XMLService2(stockSymbol); 
        String stockName = XMLService2.getStockName(); 
        model.setValueAt(stockName, e.getFirstRow(), e.getColumn() + 1); 
       } 
       if (e.getColumn() != 4 && model.getValueAt(e.getFirstRow(), 2) != null && model.getValueAt(e.getFirstRow(), 3) != null) 
       { 
        Double myDouble =(Integer)model.getValueAt(e.getFirstRow(), 2)*(Double)model.getValueAt(e.getFirstRow(), 3); 
        model.setValueAt(myDouble, e.getFirstRow(), 4); 
       } 
      } 
     }); 

     org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(getContentPane()); 
     getContentPane().setLayout(layout); 
     layout.setHorizontalGroup(
      layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING) 
      .add(layout.createSequentialGroup() 
       .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING) 
        .add(layout.createSequentialGroup() 
         .add(21, 21, 21) 
         .add(jScrollPane1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 645, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)) 
        .add(layout.createSequentialGroup() 
         .add(31, 31, 31) 
         .add(jPanel1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))) 
       .addContainerGap(50, Short.MAX_VALUE)) 
     ); 
     layout.setVerticalGroup(
      layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING) 
      .add(layout.createSequentialGroup() 
       .addContainerGap() 
       .add(jScrollPane1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 399, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE) 
       .add(27, 27, 27) 
       .add(jPanel1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE) 
       .addContainerGap(org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)) 
     ); 

     pack(); 
    }// </editor-fold> 

    /** 
    * @param args the command line arguments 
    */ 
    public static void main(String args[]) { 
     /* Set the Nimbus look and feel */ 
     //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) "> 
     /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel. 
     * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
     */ 
     try { 
      for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) { 
       if ("Nimbus".equals(info.getName())) { 
        javax.swing.UIManager.setLookAndFeel(info.getClassName()); 
        break; 
       } 
      } 
     } catch (ClassNotFoundException ex) { 
      java.util.logging.Logger.getLogger(PortfolioForm.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); 
     } catch (InstantiationException ex) { 
      java.util.logging.Logger.getLogger(PortfolioForm.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); 
     } catch (IllegalAccessException ex) { 
      java.util.logging.Logger.getLogger(PortfolioForm.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); 
     } catch (javax.swing.UnsupportedLookAndFeelException ex) { 
      java.util.logging.Logger.getLogger(PortfolioForm.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); 
     } 
     //</editor-fold> 

     /* Create and display the form */ 
     java.awt.EventQueue.invokeLater(new Runnable() { 
      public void run() { 
       new PortfolioForm().setVisible(true); 
      } 
     }); 
    } 
    // Variables declaration - do not modify 
    private javax.swing.JPanel jPanel1; 
    private javax.swing.JScrollPane jScrollPane1; 
    private javax.swing.JTable jTable1; 
    private DefaultTableModel tableModel; 
    private javax.swing.JButton jbtAddRow; 
    private javax.swing.JButton jbtDeleteRow; 
    // End of variables declaration 
} 

이 하나에 모든 아이디어를 환영보다 더 있습니다 : 여기

는 코드입니다. 감사합니다.

+3

NetBeans 자동 생성 코드를 남겨주세요. 그러면 누군가가 귀하의 질문에 답변 할 수도 있습니다. 고맙습니다! –

+1

동의합니다. 해당 코드는 완전히 불필요하며 문제와 관련이 없습니다. 프로그램을 실행하고 문제를 표시하는 데 필요한 코드 만 사용하여 [sscce] (http://sscce.org)를 작성하고 게시하는 것을 고려하십시오. –

답변

4

문제는 여기에 효과적으로 하나 개의 셀에 선택 범위를 제한

jTable1.setColumnSelectionAllowed(true); 

를 설정 한 것입니다. 기본값을 false로 설정하면 전체 행을 선택할 수 있습니다.

+0

많은 사람에게 고마워! 나는 그것을 몰랐다! – skiabox

관련 문제