本文介绍了如何为JTable列添加JXDatepicker的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用JTable。在那里我有一个日期列,当我点击单元格时,我需要使一个JXDatePicker出现,以便我可以从中选择一个日期。



有人可以显示我怎么办?



谢谢!等待一个答案..

解决方案

你应该可以使用 DatePickerCellEditor 这是一个 CellEditor ,使用 JXDatePicker 作为编辑器组件。例如:

  TableColumn dateColumn = table.getColumnModel()。getColumn(columnIndex); 
dateColumn.setCellEditor(new DatePickerCellEditor());

这是一个演示表:

  import java.util.Date; 

import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.SwingUtilities;
import javax.swing.table.TableColumn;

import org.jdesktop.swingx.table.DatePickerCellEditor;

public class DateColumnDemo {

private static void createAndShowGUI(){
JFrame frame = new JFrame(DateColumnDemo);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

JTable table = new JTable(new Object [] [] {{1,new Date()}},
new Object [] {Id,Time );

TableColumn dateColumn = table.getColumnModel()。getColumn(1);
dateColumn.setCellEditor(new DatePickerCellEditor());

JScrollPane scrollPane = new JScrollPane(table);

frame.add(scrollPane);
frame.pack();
frame.setVisible(true);
}

public static void main(String args []){
SwingUtilities.invokeLater(new Runnable(){
public void run(){
createAndShowGUI();
}
});
}
}


I'm using a JTable. There I have a Date column, where I need to make a JXDatePicker appear when I click on a cell so that I can select a date from it.

Can someone show me how to do this?

Thanks! waiting for an answer..

解决方案

You should probably use DatePickerCellEditor, which is a CellEditor using a JXDatePicker as editor component. For example:

TableColumn dateColumn = table.getColumnModel().getColumn(columnIndex);
dateColumn.setCellEditor(new DatePickerCellEditor());

Here is a demo table:

import java.util.Date;

import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.SwingUtilities;
import javax.swing.table.TableColumn;

import org.jdesktop.swingx.table.DatePickerCellEditor;

public class DateColumnDemo {

    private static void createAndShowGUI() {
        JFrame frame = new JFrame("DateColumnDemo");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        JTable table = new JTable(new Object[][] { { "1", new Date() } },
                new Object[] { "Id", "Time" });

        TableColumn dateColumn = table.getColumnModel().getColumn(1);
        dateColumn.setCellEditor(new DatePickerCellEditor());

        JScrollPane scrollPane = new JScrollPane(table); 

        frame.add(scrollPane);
        frame.pack();
        frame.setVisible(true);
    }

    public static void main(String args[]) {
        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                createAndShowGUI();
            }
        });
    }
}

这篇关于如何为JTable列添加JXDatepicker的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-12 03:40