// Can Someone Help me to set the background color of a jetxtfield using the result of the query statement
我的代码已编译,但暂时没有结果`public void mouseClicked(MouseEvent arg0){
DefaultTableModel model=(DefaultTableModel) table_Vols_Disponibles.getModel();
// get the selected row index
int selectedRowIndex = table_Vols_Disponibles.getSelectedRow();
// set the selected row data into jtextfields
txt_ID_Vol.setText(model.getValueAt(selectedRowIndex, 0).toString());
txt_ID_Avion.setText(model.getValueAt(selectedRowIndex, 1).toString());
txt_Heure_Depart.setText(model.getValueAt(selectedRowIndex, 2).toString());
txt_Heure_Arrivee.setText(model.getValueAt(selectedRowIndex, 3).toString());
txt_Duree.setText(model.getValueAt(selectedRowIndex, 4).toString());
txt_Ville_Depart.setText(model.getValueAt(selectedRowIndex, 5).toString());
txt_Ville_Arrivee.setText(model.getValueAt(selectedRowIndex, 6).toString());
// int row =table_Vols_Disponibles.getSelectedRow();
// String cell=table_Vols_Disponibles.getModel().getValueAt(row, 0).toString();
try {
Class.forName("com.mysql.jdbc.Driver");
Connection
connection=drivermanager.getConnection(“jdbc:mysql:/localhost:3306/billet_avion”,“root”,“”); 字符串sql=“Select siege from sieges where id_vo='”+txt_id_vol+“‘”; PreparedStatement PreparedStatement=Connection.PrepareStatement(sql); ResultSet ResultSet=PreparedStatement.ExecuteQuery(sql);
String siege1=txtTest.getText();
String NBsiege=resultSet.getString("siege");
if(NBsiege==siege1) {
txtTest.setBackground(Color.green);
}
}
“
当您比较字符串时,您需要比较内容,而不是它们的内存地址。 因为==
是运算符,所以它比较内存地址而不是内容
更改
if( NBsiege == siege1 )
{
txtTest.setBackground( Color.green );
}
至
if( siege1.equals(NBsiege) )
{
txtTest.setBackground( Color.green );
}
一个建议。