我写了两个类,上面类的按钮来处理下面表格的更新,
但是我不知道如何来处理表格的更新,哪位高手写个实例借我参考一下,
或者在我的代码上改也可以,不胜感激,
答对结帖给分
class ShareCalendar{
public Calendar calendar;
public int year, month, today;
public ShareCalendar(){
calendar=Calendar.getInstance();
calendar.setTime(new Date());
year=calendar.get(Calendar.YEAR);
month=calendar.get(Calendar.MONTH);
today=calendar.get(Calendar.DAY_OF_MONTH);
}
}
class NorthPanel extends JPanel{
private JLabel label;
private JButton button1,button2,button3,button4,button5;
public ShareCalendar calendar;
public CenterPanel center;
private Color color;
public NorthPanel(final ShareCalendar calendar,final CenterPanel center){
this.calendar=calendar;
this.center=center;
setLayout(new FlowLayout());
color=new Color(100,149,237);
// color=Color.white;
setBackground(color);
label=new JLabel(" : "+calendar.year+" "+calendar.month+" : ");
button1=new JButton(" << ");
button1.setBorder(null);
button1.setBackground(color);
button1.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e) {
calendar.year--;
label.setText(" : "+calendar.year+" "+calendar.month+" : ");
}
});
button2=new JButton(" < ");
button2.setBorder(null);
button2.setBackground(color);
button2.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e) {
calendar.month--;
if(calendar.month==0){
calendar.year--;
calendar.month=12;
}
label.setText(" : "+calendar.year+" "+calendar.month+" : ");
}
});
button3=new JButton(" > ");
button3.setBorder(null);
button3.setBackground(color);
button3.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e) {
calendar.month++;
if(calendar.month==13){
calendar.month=1;
calendar.year++;
}
label.setText(" : "+calendar.year+" "+calendar.month+" : ");
}
});
button4=new JButton(" >> ");
button4.setBorder(null);
button4.setBackground(color);
button4.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e) {
calendar.year++;
label.setText(" : "+calendar.year+" "+calendar.month+" : ");
}
});
button5=new JButton(" today");
button5.setBorder(null);
button5.setBackground(color);
button5.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e) {
calendar.calendar.setTime(new Date());
label.setText(" : "+calendar.year+" "+calendar.month+" : ");
}
});
add(button1);
add(button2);
add(label);
add(button3);
add(button4);
add(button5);
}
}
class CenterPanel extends JPanel{
Calendar calendar;
public static Object cell[][];
String a[];
JTable table;
AbstractTableModel daysModel;
public static final String b[]={"Sun","Mon","Tue","Wed","Thu","Fri","Sat"};
public CenterPanel(ShareCalendar calendar){
setLayout(new BorderLayout());
int hweek=calendar.calendar.get(Calendar.DAY_OF_WEEK)-1;
String a[]=new String[hweek+31];
for(int i=0; i<hweek; i++){
a[i]=" ";
}
for(int i=hweek, n=1; i<hweek+31; i++){
if(n<=9){
a[i]=String.valueOf(n)+" ";
}
else{
a[i]=String.valueOf(n);
}
n++;
}
System.out.println(a.length);
cell=new Object[6][7];
int m=0;
for(int i=0; i<5; i++){
for(int j=0; j<7; j++){
if((m=i*7+j)<a.length){
cell[i][j]=a[m];
}
else{
cell[i][j]="";
}
}
}
table=new JTable(cell,b);
table.setCellSelectionEnabled(true);
table.setSelectionMode(ListSelectionModel.SINGLE_INTERVAL_SELECTION);
table.setBackground(Color.pink);
JScrollPane pane=new JScrollPane(table);
add(pane,BorderLayout.NORTH);
}
}
在actionListener里面添加更新的事件,
在CenterPanel里面写更新table的方法...
但是我不知道如何来处理表格的更新,哪位高手写个实例借我参考一下,
或者在我的代码上改也可以,不胜感激,
答对结帖给分
class ShareCalendar{
public Calendar calendar;
public int year, month, today;
public ShareCalendar(){
calendar=Calendar.getInstance();
calendar.setTime(new Date());
year=calendar.get(Calendar.YEAR);
month=calendar.get(Calendar.MONTH);
today=calendar.get(Calendar.DAY_OF_MONTH);
}
}
class NorthPanel extends JPanel{
private JLabel label;
private JButton button1,button2,button3,button4,button5;
public ShareCalendar calendar;
public CenterPanel center;
private Color color;
public NorthPanel(final ShareCalendar calendar,final CenterPanel center){
this.calendar=calendar;
this.center=center;
setLayout(new FlowLayout());
color=new Color(100,149,237);
// color=Color.white;
setBackground(color);
label=new JLabel(" : "+calendar.year+" "+calendar.month+" : ");
button1=new JButton(" << ");
button1.setBorder(null);
button1.setBackground(color);
button1.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e) {
calendar.year--;
label.setText(" : "+calendar.year+" "+calendar.month+" : ");
}
});
button2=new JButton(" < ");
button2.setBorder(null);
button2.setBackground(color);
button2.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e) {
calendar.month--;
if(calendar.month==0){
calendar.year--;
calendar.month=12;
}
label.setText(" : "+calendar.year+" "+calendar.month+" : ");
}
});
button3=new JButton(" > ");
button3.setBorder(null);
button3.setBackground(color);
button3.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e) {
calendar.month++;
if(calendar.month==13){
calendar.month=1;
calendar.year++;
}
label.setText(" : "+calendar.year+" "+calendar.month+" : ");
}
});
button4=new JButton(" >> ");
button4.setBorder(null);
button4.setBackground(color);
button4.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e) {
calendar.year++;
label.setText(" : "+calendar.year+" "+calendar.month+" : ");
}
});
button5=new JButton(" today");
button5.setBorder(null);
button5.setBackground(color);
button5.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e) {
calendar.calendar.setTime(new Date());
label.setText(" : "+calendar.year+" "+calendar.month+" : ");
}
});
add(button1);
add(button2);
add(label);
add(button3);
add(button4);
add(button5);
}
}
class CenterPanel extends JPanel{
Calendar calendar;
public static Object cell[][];
String a[];
JTable table;
AbstractTableModel daysModel;
public static final String b[]={"Sun","Mon","Tue","Wed","Thu","Fri","Sat"};
public CenterPanel(ShareCalendar calendar){
setLayout(new BorderLayout());
int hweek=calendar.calendar.get(Calendar.DAY_OF_WEEK)-1;
String a[]=new String[hweek+31];
for(int i=0; i<hweek; i++){
a[i]=" ";
}
for(int i=hweek, n=1; i<hweek+31; i++){
if(n<=9){
a[i]=String.valueOf(n)+" ";
}
else{
a[i]=String.valueOf(n);
}
n++;
}
System.out.println(a.length);
cell=new Object[6][7];
int m=0;
for(int i=0; i<5; i++){
for(int j=0; j<7; j++){
if((m=i*7+j)<a.length){
cell[i][j]=a[m];
}
else{
cell[i][j]="";
}
}
}
table=new JTable(cell,b);
table.setCellSelectionEnabled(true);
table.setSelectionMode(ListSelectionModel.SINGLE_INTERVAL_SELECTION);
table.setBackground(Color.pink);
JScrollPane pane=new JScrollPane(table);
add(pane,BorderLayout.NORTH);
}
}
在actionListener里面添加更新的事件,
在CenterPanel里面写更新table的方法...
http://blog.csdn.net/mq612/archive/2006/08/26/1123819.aspx
fireTableStructureChanged();方法
这样便用了这个模型的table都会刷新的
如果单纯的只是想更新下数据的话,有个方法可以用
对象名.updateUI();
结构变化, fireTableStructureChanged()
如果是指定得Cell改变数据, fireTableCellUpdated(int, int)