Java JTable-布尔单元格类型-背景
我使用的是Nimbus L&F,它有一个Java JTable-布尔单元格类型-背景,java,swing,checkbox,jtable,nimbus,Java,Swing,Checkbox,Jtable,Nimbus,我使用的是Nimbus L&F,它有一个JTable,其中一个列有一个布尔(复选框)元素 我遇到的问题是布尔列没有遵循Nimbus L&F中存在的自然行背景交替。hmmm,我有相反的问题,请检查,如果没有代码示例,真的没有人可以帮助您,也许有人可以使用TableCellRenderer使用未选择的AlternateTableColor import javax.swing.*; 导入javax.swing.table.*; 导入java.util.Date; 导入java.util.Vect
JTable
,其中一个列有一个布尔(复选框)元素
我遇到的问题是布尔列没有遵循Nimbus L&F中存在的自然行背景交替。hmmm,我有相反的问题,请检查,如果没有代码示例,真的没有人可以帮助您,也许有人可以使用TableCellRenderer使用未选择的AlternateTableColor
import javax.swing.*;
导入javax.swing.table.*;
导入java.util.Date;
导入java.util.Vector;
导入java.awt.*;
导入java.awt.event.*;
导入java.text.DateFormat;
导入java.text.simpleDataFormat;
导入javax.swing.UIManager.LookAndFeelInfo;
公共类TableTestPanel扩展了JPanel{
私有静态最终字符串[]列名称={“列表ID”、“过期日期”、“状态”、“创建日期”};
私有静态最终日期格式日期\格式=新的SimpleDataFormat(“dd/MM/yyyy”);
私有静态最终长serialVersionUID=1L;
私有静态类StatusPanel扩展了JPanel{
私有静态最终长serialVersionUID=1L;
私人JRadioButton单选按钮;
结婚时的私人约会;
专用JRadioButton选项;
状态面板(){
超级(新网格布局(3,1));
set不透明(true);
ButtonGroup ButtonGroup=新建ButtonGroup();
singleoption=新的JRadioButton(“单个”);
设置不透明(假);
添加(单选项);
按钮组。添加(单选选项);
theMarriedOption=新的JRadioButton(“已婚”);
theMarriedOption.set不透明(false);
添加(婚姻介绍);
按钮组。添加(婚姻介绍);
DevorcedOption=新的JRadioButton(“离婚”);
设置不透明(false);
添加(选项);
按钮组。添加(删除选项);
}
公共状态getStatus(){
if(theMarriedOption.isSelected()){
返回状态。已婚;
}else if(devorcedoption.isSelected()){
返回状态。离婚;
}否则{
返回状态为.SINGLE;
}
}
公共无效设置状态(状态){
如果(状态==状态.已婚){
选择的婚姻记录设置(真);
}else if(状态==状态.离婚){
DevorDecedOption.setSelected(真);
}否则{
singleoption.setSelected(true);
}
}
}
私有静态类状态{
静态最终状态单一=新状态(“单一”);
静态最终状态已婚=新状态(“已婚”);
静态最终离婚状态=新状态(“离婚”);
私有最终字符串myName;//仅用于调试
私有状态(字符串名称){
我的名字=名字;
}
@凌驾
公共字符串toString(){
返回我的名字;
}
}
私有静态类表条目{
私有静态int instanceNumber;
私人Long-theId;
私有日期-扩张日期;
私人身份状态;
创建日期的私人日期;
TableEntry(){
instanceNumber++;
theId=新长(instanceNumber);
expirationdate=新日期();
状态=Status.SINGLE;
创建日期=新日期();
}
表格条目(长anId、日期anExpirationDate、状态Status、日期aCreationDate){
theId=anId;
expirationdate=expirationdate;
状态=无状态;
创建日期=创建日期;
}
公共长getId(){
返回theId;
}
公共日期getExpirationDate(){
返回ExpirationDate;
}
公共状态getStatus(){
返回状态;
}
公开日期getCreationDate(){
返回创建日期;
}
公共无效设置ID(长anId){
theId=anId;
}
公共作废设置过期日期(日期anExpirationDate){
expirationdate=expirationdate;
}
公共无效设置状态(状态设置状态){
状态=无状态;
}
公共无效设置创建日期(创建日期){
创建日期=创建日期;
}
}
私有静态类MyTableModel扩展了AbstractTableModel{
私有静态最终长serialVersionUID=1L;
私有向量入口;
MyTableModel(){
entries=新向量();
}
@抑制警告(“未选中”)
公共作废添加(表格条目输入){
int index=entries.size();
输入。添加(输入);
fireTableRowsInserted(索引,索引);
}
公共无效删除(int AROVINDEX){
如果(aRowIndex<0 | | aRowIndex>=entries.size()){
返回;
}
入口移除元件(Arovindex);
fireTableRowsDeleted(Arovindex,Arovindex);
}
public int getRowCount(){
返回entries.size();
}
@凌驾
公共字符串getColumnName(int列){
返回列名称[列];
}
@凌驾
公共类getColumnClass(int columnIndex){
开关(列索引){
案例0:
返回Long.class;
案例1:
返回日期.class;
案例2:
返回状态.class;
案例3:
返回日期.class;
}
返回Object.class;
}
@凌驾
public void setValueAt(对象有效、整数行索引、整数列
import javax.swing.*;
import javax.swing.table.*;
import java.util.Date;
import java.util.Vector;
import java.awt.*;
import java.awt.event.*;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import javax.swing.UIManager.LookAndFeelInfo;
public class TableTestPanel extends JPanel {
private static final String[] COLUMN_NAMES = {"List ID", "Expiration Date", "Status", "Date Created"};
private static final DateFormat DATE_FORMAT = new SimpleDateFormat("dd/MM/yyyy");
private static final long serialVersionUID = 1L;
private static class StatusPanel extends JPanel {
private static final long serialVersionUID = 1L;
private JRadioButton theSingleOption;
private JRadioButton theMarriedOption;
private JRadioButton theDivorcedOption;
StatusPanel() {
super(new GridLayout(3, 1));
setOpaque(true);
ButtonGroup buttonGroup = new ButtonGroup();
theSingleOption = new JRadioButton("Single");
theSingleOption.setOpaque(false);
add(theSingleOption);
buttonGroup.add(theSingleOption);
theMarriedOption = new JRadioButton("Married");
theMarriedOption.setOpaque(false);
add(theMarriedOption);
buttonGroup.add(theMarriedOption);
theDivorcedOption = new JRadioButton("Divorced");
theDivorcedOption.setOpaque(false);
add(theDivorcedOption);
buttonGroup.add(theDivorcedOption);
}
public Status getStatus() {
if (theMarriedOption.isSelected()) {
return Status.MARRIED;
} else if (theDivorcedOption.isSelected()) {
return Status.DIVORCED;
} else {
return Status.SINGLE;
}
}
public void setStatus(Status status) {
if (status == Status.MARRIED) {
theMarriedOption.setSelected(true);
} else if (status == Status.DIVORCED) {
theDivorcedOption.setSelected(true);
} else {
theSingleOption.setSelected(true);
}
}
}
private static class Status {
static final Status SINGLE = new Status("Single");
static final Status MARRIED = new Status("Married");
static final Status DIVORCED = new Status("Divorced");
private final String myName; // for debug only
private Status(String name) {
myName = name;
}
@Override
public String toString() {
return myName;
}
}
private static class TableEntry {
private static int instanceNumber;
private Long theId;
private Date theExpirationDate;
private Status theStatus;
private Date theCreationDate;
TableEntry() {
instanceNumber++;
theId = new Long(instanceNumber);
theExpirationDate = new Date();
theStatus = Status.SINGLE;
theCreationDate = new Date();
}
TableEntry(Long anId, Date anExpirationDate, Status aStatus, Date aCreationDate) {
theId = anId;
theExpirationDate = anExpirationDate;
theStatus = aStatus;
theCreationDate = aCreationDate;
}
public Long getId() {
return theId;
}
public Date getExpirationDate() {
return theExpirationDate;
}
public Status getStatus() {
return theStatus;
}
public Date getCreationDate() {
return theCreationDate;
}
public void setId(Long anId) {
theId = anId;
}
public void setExpirationDate(Date anExpirationDate) {
theExpirationDate = anExpirationDate;
}
public void setStatus(Status aStatus) {
theStatus = aStatus;
}
public void setCreationDate(Date aCreationDate) {
theCreationDate = aCreationDate;
}
}
private static class MyTableModel extends AbstractTableModel {
private static final long serialVersionUID = 1L;
private Vector<Object> theEntries;
MyTableModel() {
theEntries = new Vector<Object>();
}
@SuppressWarnings("unchecked")
public void add(TableEntry anEntry) {
int index = theEntries.size();
theEntries.add(anEntry);
fireTableRowsInserted(index, index);
}
public void remove(int aRowIndex) {
if (aRowIndex < 0 || aRowIndex >= theEntries.size()) {
return;
}
theEntries.removeElementAt(aRowIndex);
fireTableRowsDeleted(aRowIndex, aRowIndex);
}
public int getRowCount() {
return theEntries.size();
}
@Override
public String getColumnName(int column) {
return COLUMN_NAMES[column];
}
@Override
public Class<?> getColumnClass(int columnIndex) {
switch (columnIndex) {
case 0:
return Long.class;
case 1:
return Date.class;
case 2:
return Status.class;
case 3:
return Date.class;
}
return Object.class;
}
@Override
public void setValueAt(Object aValue, int rowIndex, int columnIndex) {
TableEntry entry = (TableEntry) theEntries.elementAt(rowIndex);
switch (columnIndex) {
case 0:
try {
entry.setId(new Long(Long.parseLong(aValue.toString())));
} catch (NumberFormatException nfe) {
return;
}
break;
case 1:
entry.setExpirationDate((Date) aValue);
break;
case 2:
entry.setStatus((Status) aValue);
break;
case 3:
entry.setCreationDate((Date) aValue);
break;
default:
return;
}
fireTableCellUpdated(rowIndex, columnIndex);
}
@Override
public boolean isCellEditable(int rowIndex, int columnIndex) {
return true;
}
@Override
public int getColumnCount() {
return 4;
}
@Override
public Object getValueAt(int rowIndex, int columnIndex) {
TableEntry entry = (TableEntry) theEntries.elementAt(rowIndex);
switch (columnIndex) {
case 0:
return entry.getId();
case 1:
return entry.getExpirationDate();
case 2:
return entry.getStatus();
case 3:
return entry.getCreationDate();
}
return null;
}
}
private static class DateRenderer extends DefaultTableCellRenderer {
private static final long serialVersionUID = 1L;
@Override
public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) {
super.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column);
if (!(value instanceof Date)) {
return this;
}
setText(DATE_FORMAT.format((Date) value));
return this;
}
}
private static class DateEditor extends AbstractCellEditor implements TableCellEditor {
private static final long serialVersionUID = 1L;
private JSpinner theSpinner;
private Object value;
DateEditor() {
theSpinner = new JSpinner(new SpinnerDateModel());
theSpinner.setOpaque(true);
theSpinner.setEditor(new JSpinner.DateEditor(theSpinner, "dd/MM/yyyy"));
}
@Override
public Object getCellEditorValue() {
return theSpinner.getValue();
}
@Override
public Component getTableCellEditorComponent(JTable table, Object value, boolean isSelected, int row, int column) {
theSpinner.setValue(value);
if (isSelected) {
theSpinner.setBackground(table.getSelectionBackground());
} else {
theSpinner.setBackground(table.getBackground());
}
return theSpinner;
}
}
private static class StatusEditor extends AbstractCellEditor implements TableCellEditor {
private static final long serialVersionUID = 1L;
private StatusPanel theStatusPanel;
StatusEditor() {
theStatusPanel = new StatusPanel();
}
@Override
public Object getCellEditorValue() {
return theStatusPanel.getStatus();
}
@Override
public Component getTableCellEditorComponent(JTable table, Object value, boolean isSelected, int row, int column) {
theStatusPanel.setStatus((Status) value);
if (isSelected) {
theStatusPanel.setBackground(table.getSelectionBackground());
} else {
theStatusPanel.setBackground(table.getBackground());
}
return theStatusPanel;
}
}
private static class StatusRenderer extends StatusPanel implements TableCellRenderer {
private static final long serialVersionUID = 1L;
@Override
public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) {
setStatus((Status) value);
if (isSelected) {
setBackground(table.getSelectionBackground());
} else {
setBackground(table.getBackground());
}
return this;
}
}
private MyTableModel theTableModel;
private JTable theTable;
public TableTestPanel() {
super(new BorderLayout(0, 5));
setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));
theTableModel = new MyTableModel();
theTable = new JTable(theTableModel);
theTable.setDefaultEditor(Date.class, new DateEditor());
theTable.setDefaultRenderer(Date.class, new DateRenderer());
theTable.setDefaultEditor(Status.class, new StatusEditor());
theTable.setDefaultRenderer(Status.class, new StatusRenderer());
// comment out the two preceding lines and uncomment the following one if you want a more standard editor
// theTable.setDefaultEditor(Status.class, new DefaultCellEditor(new JComboBox(new Status[]{Status.SINGLE, Status.MARRIED, Status.DIVORCED})));
add(new JScrollPane(theTable), BorderLayout.CENTER);
JToolBar toolBar = new JToolBar();
toolBar.setFloatable(false);
toolBar.add(new AbstractAction("Add new") {
private static final long serialVersionUID = 1L;
@Override
public void actionPerformed(ActionEvent e) {
theTableModel.add(new TableEntry());
packTable();
}
});
toolBar.add(new AbstractAction("Remove") {
private static final long serialVersionUID = 1L;
@Override
public void actionPerformed(ActionEvent e) {
theTableModel.remove(theTable.getSelectedRow());
}
});
add(toolBar, BorderLayout.NORTH);
}
private void packTable() {
TableColumnModel columnModel = theTable.getColumnModel();
int columnCount = theTable.getColumnCount();
int rowCount = theTable.getRowCount();
int[][] preferredHeights = new int[columnCount][rowCount];
TableCellRenderer renderer;
Component comp;
for (int col = 0; col < columnCount; col++) {
renderer = columnModel.getColumn(col).getCellRenderer();
if (renderer == null) {
renderer = theTable.getDefaultRenderer(theTableModel.getColumnClass(col));
}
for (int row = 0; row < rowCount; row++) {
comp = renderer.getTableCellRendererComponent(theTable, theTableModel.getValueAt(row, col), false, false, row, col);
preferredHeights[col][row] = (int) comp.getPreferredSize().getHeight();
}
}
for (int row = 0; row < rowCount; row++) {
int pref = 0;
for (int col = 0; col < columnCount; col++) {
pref = Math.max(pref, preferredHeights[col][row]);
}
theTable.setRowHeight(row, pref);
}
}
public static void main(String[] args) {
try {
// UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
for (LookAndFeelInfo info : UIManager.getInstalledLookAndFeels()) {
if (info.getName().equals("Nimbus")) {
UIManager.setLookAndFeel(info.getClassName());
break;
}
}
} catch (Exception e1) {
e1.printStackTrace();
}
final JFrame frame = new JFrame("TestRadioButtonRenderer");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setContentPane(new TableTestPanel());
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
frame.setSize(400, 300);
frame.setVisible(true);
}
});
}
}
((JComponent) table.getDefaultRenderer(Boolean.class)).setOpaque(true);