单击鼠标时,我尝试从中获取价值,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]);
}
}