0
데이터베이스의 내용을 보여주는 Java 응용 프로그램이 있습니다. 셀의 내용을 기반으로 행의 색을 수정하고 싶습니다. setBackground
속성을 사용하여 데이터베이스에서 데이터 검색 프로세스에주기를 삽입하려고 시도했지만 성공하지 못했습니다.Java - 셀 내용을 기반으로 행 색상 변경
여기에 코드가 있습니다. 내가 뭘 잘못하고있어?
public class TableFrame2 extends JFrame {
private JPanel contentPane;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
String user = "root";
String password = "";
TableFrame2 frame = new TableFrame2(user, password);
frame.setVisible(true);
}
});
}
/**
* Create the frame.
* @return
*/
public TableFrame2(String user, String password) {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 399, 300);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
JScrollPane scrollPane = new JScrollPane();
DefaultTableModel model = new DefaultTableModel();
JTable table = new JTable(model);
table.setEditingColumn(0);
table.setEditingRow(0);
table.setSelectionMode(ListSelectionModel.SINGLE_INTERVAL_SELECTION);
table.setFillsViewportHeight(true);
table.setBackground(Color.WHITE);
table.setRowSelectionAllowed(true);
model.addColumn("iD");
model.addColumn("name");
model.addColumn("type");
table.setPreferredScrollableViewportSize(new Dimension(200, 200));
scrollPane.setViewportView(table);
try {
String dateMerged = "2015-01-30";//yearField.getText() + "-" + monthField.getText() + "-" + dayField.getText();
Connection connection = MysqlConnector.dbConnection(user, password);
String query = "SELECT * FROM booking, band WHERE room_idRoom = 2 AND idBand = band_idBand AND dateBooking BETWEEN '" +dateMerged+" 00:00:00' AND '" +dateMerged+" 23:59:59'";
PreparedStatement pst = connection.prepareStatement(query);
ResultSet rs = pst.executeQuery();
while (rs.next()){
String bandName = rs.getString("nameBand");
String bookingType = rs.getString("typeBooking");
String bookingId = rs.getString("idBooking");
model.addRow(new Object[] { bookingId, bandName,bookingType });
if (bookingType == "Recording"){
table.setBackground(Color.RED);
}
}
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (InstantiationException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalAccessException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
GroupLayout gl_contentPane = new GroupLayout(contentPane);
gl_contentPane.setHorizontalGroup(
gl_contentPane.createParallelGroup(Alignment.TRAILING)
.addGroup(Alignment.LEADING, gl_contentPane.createSequentialGroup()
.addContainerGap()
.addComponent(scrollPane, GroupLayout.PREFERRED_SIZE, 114, GroupLayout.PREFERRED_SIZE)
.addContainerGap(89, Short.MAX_VALUE))
);
gl_contentPane.setVerticalGroup(
gl_contentPane.createParallelGroup(Alignment.TRAILING)
.addGroup(gl_contentPane.createSequentialGroup()
.addContainerGap(GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(scrollPane, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
);
}
}
custorm'TableCellRenderer'을 사용해야합니다. 자세한 내용은 [tutoria] (http://docs.oracle.com/javase/tutorial/uiswing/components/table.html#editrender)를 참조하십시오. – alex2410
[예] (http://stackoverflow.com/a/5799016/230513). – trashgod
고마워요! –