现在有这么个需求,从数据库中查询,将查询结果显示在JTABLE中了,然后人工在JTABLE中根据某些条件将符合条件的行用不同颜色标出来,然后将标示结果存回数据库(数据库中有标识字段)PS:条件随时在变化,不能确定,所以只能手动标颜色,比如,销售计划,但是用户随时可能取消订单,取消的订单需要 黄色 标出
            一周销量不达标的需要 红色 标出
            有库存备货的需要 绿色 标出我用的JPopupMenu弹出JColorChooser实现颜色选择,但是怎么让JTABLE的行动态实现颜色变化呢?类似  https://bbs.csdn.net/topics/390492394  这样的效果,这个参考了下,还是没实现

解决方案 »

  1.   

    import java.awt.BorderLayout;
    import java.awt.Color;
    import java.awt.EventQueue;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;import javax.swing.JButton;
    import javax.swing.JColorChooser;
    import javax.swing.JFrame;
    import javax.swing.JPanel;
    import javax.swing.JScrollPane;
    import javax.swing.JTable;
    import javax.swing.border.EmptyBorder;
    import javax.swing.table.DefaultTableModel;public class TableExample extends JFrame implements ActionListener {
    /**
     * 
     */
    private static final long serialVersionUID = -6091279128857005144L;
    private JScrollPane scrollPane;
    private JPanel contentPane;
    private JTable table;
    private JPanel panel;
    private JButton backgroundButton;
    private JColorChooser jColorChooser;
    // static Vector<Cell> Cellvector = new Vector<Cell>(); public static void main(String[] args) {
    EventQueue.invokeLater(new Runnable() {
    public void run() {
    try {
    TableExample frame = new TableExample();
    frame.setVisible(true);
    } catch (Exception e) {
    e.printStackTrace();
    }
    }
    });
    } /**
     * Create the frame.
     */
    public TableExample() {
    this.setTitle("JTable背景颜色设定练习");
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setBounds(100, 100, 712, 541);
    contentPane = new JPanel();
    contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
    contentPane.setLayout(new BorderLayout(0, 0));
    setContentPane(contentPane); scrollPane = new JScrollPane();
    contentPane.add(scrollPane, BorderLayout.CENTER); table = new JTable();
    scrollPane.setViewportView(table); String[] Column = { "第一列", "第二列", "第三列", "第四列" }; // 设定列名
    DefaultTableModel TableModle = new DefaultTableModel();
    TableModle.setColumnIdentifiers(Column);
    TableModle.setRowCount(20);
    for (int i = 0; i < 20; i++) { // 输入行值
    for (int j = 0; j < 4; j++) {
    TableModle.setValueAt("[" + i + "," + j + "]", i, j);
    }
    }
    table.setModel(TableModle);
    table.setRowSelectionInterval(0,0); jColorChooser = new JColorChooser();
    contentPane.add(jColorChooser, BorderLayout.SOUTH); panel = new JPanel();
    contentPane.add(panel, BorderLayout.NORTH); backgroundButton = new JButton(
    "\u8BBE\u7F6E\u5355\u5143\u683C\u80CC\u666F\u989C\u8272");
    backgroundButton.addActionListener(this);
    panel.add(backgroundButton);
    } public void actionPerformed(ActionEvent e) {
    if (e.getSource() == backgroundButton) {
    int row = table.getSelectedRow(); // 得到所选行索引
    int column = table.getSelectedColumn(); // 得到所选列索引
    System.out.println("row:" + row + "col:" + column);
    Color background = jColorChooser.getColor();// 得到所选颜色
    if (row != -1) { // 判断是否存在单元格
    table.setSelectionBackground(background);
    }
    }
    }
    }
    试试这个。这个可以设置
      

  2.   

    table.getSelectionBackground();
    table.getSelectionForeground();
      

  3.   

    感谢回复,楼上的你的那个方法不是获得某个单元格的前 、背景色吧,而是table设置的选中行的前、背景色
    这个困难已捣鼓出来了,现在在研究进度条……
      

  4.   

    新问题来咯,如何在JTABLE里实现类似EXCEL的冻结前几列的效果?