JTable:按下按钮后更改单元格的颜色

时间:2018-07-13 08:01:56

标签: java jframe jtable

我正在尝试通过JTable构建一个日历,该日历每周显示一次事件。通过选择日历上的时间,单击“添加固定事件”并输入名称(https://i.stack.imgur.com/ynEkJ.png),用户可以向日历添加“固定事件”。单击“确定”后,所选的时间段应将颜色更改为黄色(并保持不变)。这怎么可能?

    public class WeekCalendar extends javax.swing.JFrame {

DefaultTableModel model;
//Calendar cal = new GregorianCalendar();
Object[][] matrix = new Object[24][8];


public WeekCalendar() {
    initComponents();
    String[] rows = new String[24];
    String [] columns = {"Hour/Day","Sun","Mon","Tue","Wed","Thu","Fri","Sat"};
    for (int i = 0; i < 24; i++)
    {
        matrix[i][0] = i+":00";
    }

    model = new DefaultTableModel(matrix,columns);
    table.setModel(model);

}

private void initComponents(){//Code ommitted: Auto-genned}

private void addFixedActionPerformed(java.awt.event.ActionEvent evt) {                                         
    // TODO add your handling code here:
    TextFrame.instance.setVisible(true);
}  
void addEvent(String name)
{
    int[] rows = table.getSelectedRows();
    int[] cols = table.getSelectedRows();
    //Change the colour of selected cells to yellow

}
/**
 * @param args the command line arguments
 */
public static void main(String args[]) {

    new WeekCalendar().setVisible(true);
}

private javax.swing.JButton addFixed;
private javax.swing.JScrollPane jScrollPane1;
private javax.swing.JTable table;

TextFrame类:

public class TextFrame extends javax.swing.JFrame {
static TextFrame instance = new TextFrame();
/**
 * Creates new form TextFrame
 */
public TextFrame() {
    initComponents();
}

private void okButtonActionPerformed(java.awt.event.ActionEvent evt) {                                         
    // TODO add your handling code here:
    WeekCalendar.instance.addEvent(txtField.getText());
} 

0 个答案:

没有答案