简体   繁体   English

Java-根据单元格内容更改行颜色

[英]Java - Change row color based on cell content

I have a Java application that shows things from a database. 我有一个Java应用程序,可以显示数据库中的内容。 I would like to modify the colour of the rows based on the content of a cell. 我想根据单元格的内容修改行的颜色。 I tried to insert an if cycle in the data retrieving process from databse using setBackground property but without succeding in that. 我试图使用setBackground属性在从databse的数据检索过程中插入一个if周期,但没有成功。

Here there is code. 这里有代码。 What I'm doing wrong? 我做错了什么?

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))
        );
    }
}

查看“ 表行渲染” ,它使您可以自定义渲染,而无需为表中的不同数据类型创建多个自定义渲染器。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM