我点击鼠标时尝试从JTable
获取值JTextField
,但它不起作用。例如,当用户从我的表中选择一行时,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]);
}
}
答案 0 :(得分: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);
答案 1 :(得分:1)
如果您使用MouseListener执行任务,则此代码可能会有所帮助。创建一个CustomMouseListener
类,当点击发生时,只需调用getSelectedData(..)方法,使其将值设置为JTextField
。只能在鼠标为pressed
,clicked
或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);
}
}
答案 2 :(得分: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);
}
}
答案 3 :(得分: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);
现在只需将textField
和textField1
设置为jTextField1
和jTextField2
jTextField1.setText(textField);
jTextField2.setText(textField1);