当前位置: 技术问答>java相关
JTable中如何让它双击改变颜色?附代码~
来源: 互联网 发布时间:2015-11-02
本文导语: package calendar; /** * Title: * Description: * Copyright: Copyright (c) 2002 * Company: * @author unascribed * @version 1.0 */ import java.awt.*; import java.awt.event.*; import javax.swing.table.*; import java.util.Calendar; import java.text.Si...
package calendar;
/**
*
*
*
*
* @author unascribed
* @version 1.0
*/
import java.awt.*;
import java.awt.event.*;
import javax.swing.table.*;
import java.util.Calendar;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.GregorianCalendar;
import javax.swing.*;
import com.borland.jbcl.layout.*;
import java.beans.*;
import javax.swing.border.*;
import javax.swing.event.*;
public class CalendarDlg extends JDialog implements Runnable
{
private GregorianCalendar calendar = new GregorianCalendar();//日期
private int intYear=2000;//年
private int intMonth=1;//月
private int intDay=1;//日
private String FinalyDate="";//输出的日期字符窜
private boolean isRun=true;//是否在运行
//构造一个不可编辑的表模型 model
private DefaultTableModel model=new DefaultTableModel(new Object[6][7],
new Object[]{"日","一","二","三","四","五","六"})
{
public boolean isCellEditable(int row,int column)
{
return false;
}
};
private TitledBorder titledBorder1;
private JPanel jPanel1 = new JPanel();
private JPanel panel2 = new JPanel();
private JComboBox MonthChoice = new JComboBox();//月份选择框
private JLabel label3 = new JLabel();
private JLabel label2 = new JLabel();
private JScrollBar YearScrollbar = new JScrollBar();
private JTable table = new JTable(model)
{//日期表格
public TableCellRenderer getCellRenderer(int row,int column)
{
return new DefaultTableCellRenderer()
{//日期表格填充并设定组件
public Component getTableCellRendererComponent(JTable table,
Object value,
boolean isSelected,
boolean hasFocus,
int row,
int column)
{
JLabel label=null;
if(value!=null)
{
label = new JLabel(value.toString(),JLabel.CENTER);
label.setOpaque(true);
label.setFont(table.getFont());
if(isSelected)//选择日期
{
label.setForeground(Color.white);
label.setBackground(Color.green);
label.setBorder(BorderFactory.createLineBorder(Color.yellow));
}
else//没有选择
{
label.setForeground(Color.black);
label.setBackground(Color.white);
}
}
return label;
}
};
}
};
private JScrollPane jScrollPane1 = new JScrollPane(table);
private JPanel jPanel2 = new JPanel();
private JLabel TimeLabel = new JLabel();
private BorderLayout borderLayout2 = new BorderLayout();
private BorderLayout borderLayout3 = new BorderLayout();
private BorderLayout borderLayout1 = new BorderLayout();
private XYLayout xYLayout1 = new XYLayout();
private JTextField YearTextField = new JTextField();//年
private boolean isConfirm=false;
public CalendarDlg(Frame parentFrame)
{
super(parentFrame,"",true);
try
{
jbInit();
enableEvents(AWTEvent.WINDOW_EVENT_MASK);
}
catch(Exception e)
{
e.printStackTrace();
}
Dimension d = Toolkit.getDefaultToolkit().getScreenSize();
this.setSize(350,500);
setLocation((d.width -getWidth())/2,(d.height-getHeight())/2) ;
}
//给 model 添加日期
private void fillDate(int pYear,int pMonth,int pDay)
{
calendar = new GregorianCalendar(pYear,pMonth,1);
SimpleDateFormat sdf = new SimpleDateFormat("yyyy'-'MM'-'dd");
FinalyDate = sdf.format(new GregorianCalendar(pYear,pMonth,pDay).getTime());//获得当前日期
TimeLabel.setText(FinalyDate);//下面显示日期 label
int maxDayCount = calendar.getActualMaximum(Calendar.DAY_OF_MONTH);
if(pDay>maxDayCount)
pDay=maxDayCount;
int dayOfWeek = calendar.get(Calendar.DAY_OF_WEEK);
int selectRow = 0;
int selectCol = dayOfWeek;
int count=0;
int num=0;
for(int i=0;i
/**
*
Title:
*
Description:
*
Copyright: Copyright (c) 2002
*
Company:
* @author unascribed
* @version 1.0
*/
import java.awt.*;
import java.awt.event.*;
import javax.swing.table.*;
import java.util.Calendar;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.GregorianCalendar;
import javax.swing.*;
import com.borland.jbcl.layout.*;
import java.beans.*;
import javax.swing.border.*;
import javax.swing.event.*;
public class CalendarDlg extends JDialog implements Runnable
{
private GregorianCalendar calendar = new GregorianCalendar();//日期
private int intYear=2000;//年
private int intMonth=1;//月
private int intDay=1;//日
private String FinalyDate="";//输出的日期字符窜
private boolean isRun=true;//是否在运行
//构造一个不可编辑的表模型 model
private DefaultTableModel model=new DefaultTableModel(new Object[6][7],
new Object[]{"日","一","二","三","四","五","六"})
{
public boolean isCellEditable(int row,int column)
{
return false;
}
};
private TitledBorder titledBorder1;
private JPanel jPanel1 = new JPanel();
private JPanel panel2 = new JPanel();
private JComboBox MonthChoice = new JComboBox();//月份选择框
private JLabel label3 = new JLabel();
private JLabel label2 = new JLabel();
private JScrollBar YearScrollbar = new JScrollBar();
private JTable table = new JTable(model)
{//日期表格
public TableCellRenderer getCellRenderer(int row,int column)
{
return new DefaultTableCellRenderer()
{//日期表格填充并设定组件
public Component getTableCellRendererComponent(JTable table,
Object value,
boolean isSelected,
boolean hasFocus,
int row,
int column)
{
JLabel label=null;
if(value!=null)
{
label = new JLabel(value.toString(),JLabel.CENTER);
label.setOpaque(true);
label.setFont(table.getFont());
if(isSelected)//选择日期
{
label.setForeground(Color.white);
label.setBackground(Color.green);
label.setBorder(BorderFactory.createLineBorder(Color.yellow));
}
else//没有选择
{
label.setForeground(Color.black);
label.setBackground(Color.white);
}
}
return label;
}
};
}
};
private JScrollPane jScrollPane1 = new JScrollPane(table);
private JPanel jPanel2 = new JPanel();
private JLabel TimeLabel = new JLabel();
private BorderLayout borderLayout2 = new BorderLayout();
private BorderLayout borderLayout3 = new BorderLayout();
private BorderLayout borderLayout1 = new BorderLayout();
private XYLayout xYLayout1 = new XYLayout();
private JTextField YearTextField = new JTextField();//年
private boolean isConfirm=false;
public CalendarDlg(Frame parentFrame)
{
super(parentFrame,"",true);
try
{
jbInit();
enableEvents(AWTEvent.WINDOW_EVENT_MASK);
}
catch(Exception e)
{
e.printStackTrace();
}
Dimension d = Toolkit.getDefaultToolkit().getScreenSize();
this.setSize(350,500);
setLocation((d.width -getWidth())/2,(d.height-getHeight())/2) ;
}
//给 model 添加日期
private void fillDate(int pYear,int pMonth,int pDay)
{
calendar = new GregorianCalendar(pYear,pMonth,1);
SimpleDateFormat sdf = new SimpleDateFormat("yyyy'-'MM'-'dd");
FinalyDate = sdf.format(new GregorianCalendar(pYear,pMonth,pDay).getTime());//获得当前日期
TimeLabel.setText(FinalyDate);//下面显示日期 label
int maxDayCount = calendar.getActualMaximum(Calendar.DAY_OF_MONTH);
if(pDay>maxDayCount)
pDay=maxDayCount;
int dayOfWeek = calendar.get(Calendar.DAY_OF_WEEK);
int selectRow = 0;
int selectCol = dayOfWeek;
int count=0;
int num=0;
for(int i=0;i