1

单击鼠标时,我尝试从中获取价值,JTableJTextField它不起作用。例如,当用户从我的表JTextField中选择一行时,会动态填充所选行的信息。

JTable也在一个JScrollPane. 我的桌子也有一个DefaultTableModel.

DefaultTableModel model = new DefaultTableModel(){
    public boolean isCellEditable(int row, int column){
        return false;
        }
    };
private JTable jTable1 = new JTable(model);
public Reservation() {
    try {
        jbInit();
    } catch (Exception e) {
        e.printStackTrace();
    }
}

private void jbInit() throws Exception {
    this.getContentPane().setLayout( null );
    this.setSize( new Dimension(400, 300) );
    this.setTitle( "Rezervasyon" );
    jLabel1.setText("Kitap No: ");
    jLabel1.setBounds(new Rectangle(0, 185, 60, 15));
    jTextField1.setBounds(new Rectangle(60, 180, 70, 20));
    jLabel2.setText("Ogrenci No: ");
    jLabel2.setBounds(new Rectangle(165, 180, 65, 20));
    jTextField2.setBounds(new Rectangle(250, 180, 75, 20));
    jButton1.setText("Gonder");
    jButton1.setBounds(new Rectangle(135, 210, 75, 21));
    jButton1.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                jButton1_actionPerformed(e);
            }
        });
    jLabel3.setText("Ogrenci ID: ");
    jLabel3.setBounds(new Rectangle(5, 30, 100, 15));
    jTextField3.setBounds(new Rectangle(80, 25, 120, 20));
    jScrollPane1.setBounds(new Rectangle(5, 50, 350, 115));
    jScrollPane1.addMouseListener(new MouseAdapter() {
            public void mouseClicked(MouseEvent e) {
                int row = jTable1.getSelectedRow();
                String book = model.getValueAt(row, 0).toString();
                jTextField1.setText(book);
                System.out.println(book);
                String stuID = model.getValueAt(row, 1).toString();
                jTextField2.setText(stuID);
            }
        });
    jButton2.setText("Ara");
    jButton2.setBounds(new Rectangle(285, 25, 75, 21));
    jButton2.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                jButton2_actionPerformed(e);
            }
        });
    jScrollPane1.getViewport().add(jTable1, null);
    this.getContentPane().add(jButton2, null);
    this.getContentPane().add(jScrollPane1, null);
    this.getContentPane().add(jTextField3, null);
    this.getContentPane().add(jLabel3, null);
    this.getContentPane().add(jButton1, null);
    this.getContentPane().add(jTextField2, null);
    this.getContentPane().add(jLabel2, null);
    this.getContentPane().add(jTextField1, null);
    this.getContentPane().add(jLabel1, null);
    String header [] ={"Kitap No", "Ogrenci No"};
    for(int i = 0; i < header.length; i++){
        model.addColumn(header[i]);
        }
}
4

4 回答 4

1

我推荐使用TableCellListener类。然后你只需去:

//Creating an action for the Listener to use
Action action = new AbstractAction()
{
public void actionPerformed(ActionEvent e)
{
    //The code to do when the action is performed
    TableCellListener tcl = (TableCellListener)e.getSource();
    <yourTextField>.setText(tcl.getNewValue());
}
};
//Nothing else needed! Listens automatically
TableCellListener tcl = new TableCellListener(table, action);
于 2012-12-06T20:14:46.727 回答
1

如果您使用MouseListener执行任务,那么此代码可能会有所帮助。创建一个CustomMouseListener类,当点击发生时,只需调用 getSelectedData(..) 方法,它将值设置为JTextField. 设置应仅在鼠标位于pressedclicked时进行entered

class TableMouseListener implements MouseListener {  

   private JTable table; 
   private JTextField textField;   

   public TableMouseListener(JTable table, JTextField textField) {
  this.table = table;
      this.textField = textField;           
   }

 public void mousePressed(MouseEvent e) { 
          getSelectedData(JTable table);
 }  

 public void mouseReleased(MouseEvent e) {}  

 public void mouseEntered(MouseEvent e) {
           getSelectedData(JTable table);
 }  

 public void mouseExited(MouseEvent e) {}  

 public void mouseClicked(MouseEvent e) {
          getSelectedData(JTable table);
 } 

/**
 *  Get the selected row using selectedRow(..) method. Append entire row to the String and set this string to the JTextFIeld.  
*/
public getSelectedData(JTable table) {
        // Get the selected row from the table.
          int selectedRow = table.getSelectedRow();
          String setToTextField = new String();
          for(int i = 0; i < table.getColumnCount(); i++) {
              setToTextField += table.getValueAt(selectedRow, i) + "  ";
          }
          // Finally set this to your JTextField.
         textField.setText(setToTextField);
 }
} 
于 2012-12-07T04:23:49.767 回答
0

可能最简单的方法是使用 JTable 的 MouseClicked 事件。使用以下代码:

private void jTable1MouseClicked(java.awt.event.MouseEvent evt) {                                     
    DefaultTableModel dtm = (DefaultTableModel) jTable1.getModel(); //create a model
    int selectedRowIndex = jTable1.getSelectedRow(); //get selected row
    String id = (String) dtm.getValueAt(selectedRowIndex, 0); //get the primary key to fetch data.
    try {
        ResultSet rs = DBCon.getData("Select * from stock where pd_id= '"+id+"'"); //dont worry about dbcon.getdata, its my class to get data.
        while(rs.next()){    //u can use normal query
            pd_id.setText(rs.getString("pd_id"));  //these are my textfields
            pd_name.setText(rs.getString("pd_name"));
            pd_qtys.setText(rs.getString("pd_qtys"));
            pd_qtyw.setText(rs.getString("pd_qtyw"));
            pd_price.setText(rs.getString("pd_price"));
            pd_pkg.setText(rs.getString("pd_pkg"));
        }
    } catch (Exception ex) {
        Logger.getLogger(ExistingStock.class.getName()).log(Level.SEVERE, null, ex);
    }
}
于 2017-07-19T11:27:02.837 回答
0

只需您必须转换model.getValueAt(jTable1.getSelectedRow(),0);String

DefaultTableModel model = (DefaultTableModel) jTable1.getModel();

String textField = (String) model.getValueAt(jTable1.getSelectedRow(),0);  
String textField1 = (String) model.getValueAt(jTable1.getSelectedRow(),1);  

现在只需设置textFieldand textField1to jTextField1andjTextField2

jTextField1.setText(textField);
jTextField2.setText(textField1);
于 2018-03-22T16:51:47.900 回答