我这个程序可以判断一下表格单元的所在行数,至于列数你自己改改看吧。
import java.util.*;import java.awt.*;
import java.awt.event.*;import javax.swing.*;
import javax.swing.table.*;
import javax.swing.event.*;
// main class, starting of application
public class Components
{
public static void main(String[] args)
{
JFrame jf=new TableFrame();
}
}
// table model class, set tabel model and initialize table's data
class ReviewTableModel extends AbstractTableModel
{
// take over tabledata class instance
private static TableData tabledata=new TableData();
// take over table column's name
String[] columnName=tabledata.columnName;
// take over table row's data
private Object[][] rowData=tabledata.rowData;
// implement 3 methods which AbstractTableModel class don't implement
public int getColumnCount()
{
return columnName.length;
} public int getRowCount()
{
return rowData.length;
}
public Object getValueAt(int row,int col)
{
return rowData[row][col];
}
// override 4 methods which AbstractTableModel class has already implemented
public String getColumnName(int col)
{
return columnName[col];
}
public Class getColumnClass(int col)
{
return rowData[0][col].getClass();
} public void setValueAt(Object obj,int row,int col)
{
rowData[row][col]=obj;
} public boolean isCellEditable(int row,int col)
{
if (col==0)
{
return true;
}
else
{
return false;
}
}
}
// table cell renderer
class MyTableCellRenderer implements TableCellRenderer
{
public Component getTableCellRendererComponent
(
JTable table,Object value,boolean isSelected,
boolean hasFocus,final int row,int column
)
{
button.setText(new Integer(row+1).toString());
button.addActionListener
(
new ActionListener()
{
public void actionPerformed(ActionEvent ae)
{
System.out.println(row+1);
}
}
);
return button;
}
/* the following panel is returned for all cells, with
the background color set to the Color value of the cell
*/
private JButton button=new JButton();
}
// table cell editor
class MyTableCellEditor implements TableCellEditor
{
public Component getTableCellEditorComponent
(
JTable table,Object obj,boolean isSelected,int row,int column
)
{
return new MyTableCellRenderer().
getTableCellRendererComponent(table,obj,isSelected,true,row,column);
}
// implement all methods in TableCellEditor interface
public boolean isCellEditable(EventObject anEvent)
{
return true;
} public boolean shouldSelectCell(EventObject anEvent)
{
return true;
} public void cancelCellEditing()
{
} public boolean stopCellEditing()
{
return true;
} public Object getCellEditorValue()
{
return null;
} public void addCellEditorListener(CellEditorListener l)
{
} public void removeCellEditorListener(CellEditorListener l)
{
} protected void fireEditingStopped()
{
} protected void fireEditingCanceled()
{
}
}class TableData
{
private int questionCount=59;
String[] columnName;
Object[][] rowData;
Integer[] questionNumber;
String[] questionType;
String[] attempted;
Boolean[] unAttempted;
Boolean[] isMarked;
TableData()
{
initTableData();
}
void initTableData()
{
// initialize array data of table column's name
columnName=new String[]{"No.","Type","Attempted","UnAttempted","Marked"};
// initialize array data of question number column
questionNumber=new Integer[questionCount];
for(int i=0;i<questionNumber.length;i++)
{
questionNumber[i]=new Integer(i);
}
// initialize array data of question type column
questionType=new String[questionCount];
for(int i=0;i<questionType.length;i++)
{
questionType[i]="Choice";
}
// initialize array data of attempted column
attempted=new String[questionCount];
for(int i=0;i<attempted.length;i++)
{
attempted[i]="";
}
// initialize array data of unAttempted column
unAttempted=new Boolean[questionCount];
for(int i=0;i<unAttempted.length;i++)
{
if (attempted[i]!="")
{
unAttempted[i]=new Boolean(true);
}
else
{
unAttempted[i]=new Boolean(false);
}
}
// initializate array data of isMarked column
isMarked=new Boolean[questionCount];
for(int i=0;i<isMarked.length;i++)
{
if(i%2==0)
{
isMarked[i]=new Boolean(true);
}
else
{
isMarked[i]=new Boolean(false);
}
}
// initializate array data of table
rowData=new Object[questionCount][5];
for(int i=0;i<rowData.length;i++)
{
for(int j=0;j<5;j++)
{
switch(j)
{
case 0:
rowData[i][j]=questionNumber[i];
break;
case 1:
rowData[i][j]=questionType[i];
break;
case 2:
rowData[i][j]=attempted[i];
break;
case 3:
rowData[i][j]=unAttempted[i];
break;
case 4:
rowData[i][j]=isMarked[i];
break;
}
}
}
}
}
// jframe container
class TableFrame extends JFrame
{
JTable jtable;
TableModel reviewTableModel;
Container contentpane=getContentPane();
private final boolean ALLOW_ROW_SELECTION=true;
public TableFrame()
{
super("Temp");
setDefaultCloseOperation(DISPOSE_ON_CLOSE);
setupJTable();
contentpane.add(new JScrollPane(jtable),"Center");
setBounds(100,100,800,600);
// setResizable(false);
setVisible(true);
// setExtendedState(Frame.MAXIMIZED_BOTH); // JDK1.4
} public void setupJTable()
{
reviewTableModel=new ReviewTableModel();
jtable=new JTable(reviewTableModel); // set up renderers and editors
jtable.setDefaultRenderer(Integer.class,new MyTableCellRenderer());
jtable.setDefaultEditor(Integer.class,new MyTableCellEditor()); // show table
jtable.setRowHeight(50);
// select one list index at a time.
jtable.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
if(ALLOW_ROW_SELECTION)
{
// ALLOW_ROW_SELECTION is true by default
ListSelectionModel rowSelectionModel=jtable.getSelectionModel();
rowSelectionModel.addListSelectionListener
(
new ListSelectionListener()
{
public void valueChanged(ListSelectionEvent e)
{
//Ignore extra messages.
if (e.getValueIsAdjusting())
{
return;
}
}
}
);
}
}
}
import java.util.*;import java.awt.*;
import java.awt.event.*;import javax.swing.*;
import javax.swing.table.*;
import javax.swing.event.*;
// main class, starting of application
public class Components
{
public static void main(String[] args)
{
JFrame jf=new TableFrame();
}
}
// table model class, set tabel model and initialize table's data
class ReviewTableModel extends AbstractTableModel
{
// take over tabledata class instance
private static TableData tabledata=new TableData();
// take over table column's name
String[] columnName=tabledata.columnName;
// take over table row's data
private Object[][] rowData=tabledata.rowData;
// implement 3 methods which AbstractTableModel class don't implement
public int getColumnCount()
{
return columnName.length;
} public int getRowCount()
{
return rowData.length;
}
public Object getValueAt(int row,int col)
{
return rowData[row][col];
}
// override 4 methods which AbstractTableModel class has already implemented
public String getColumnName(int col)
{
return columnName[col];
}
public Class getColumnClass(int col)
{
return rowData[0][col].getClass();
} public void setValueAt(Object obj,int row,int col)
{
rowData[row][col]=obj;
} public boolean isCellEditable(int row,int col)
{
if (col==0)
{
return true;
}
else
{
return false;
}
}
}
// table cell renderer
class MyTableCellRenderer implements TableCellRenderer
{
public Component getTableCellRendererComponent
(
JTable table,Object value,boolean isSelected,
boolean hasFocus,final int row,int column
)
{
button.setText(new Integer(row+1).toString());
button.addActionListener
(
new ActionListener()
{
public void actionPerformed(ActionEvent ae)
{
System.out.println(row+1);
}
}
);
return button;
}
/* the following panel is returned for all cells, with
the background color set to the Color value of the cell
*/
private JButton button=new JButton();
}
// table cell editor
class MyTableCellEditor implements TableCellEditor
{
public Component getTableCellEditorComponent
(
JTable table,Object obj,boolean isSelected,int row,int column
)
{
return new MyTableCellRenderer().
getTableCellRendererComponent(table,obj,isSelected,true,row,column);
}
// implement all methods in TableCellEditor interface
public boolean isCellEditable(EventObject anEvent)
{
return true;
} public boolean shouldSelectCell(EventObject anEvent)
{
return true;
} public void cancelCellEditing()
{
} public boolean stopCellEditing()
{
return true;
} public Object getCellEditorValue()
{
return null;
} public void addCellEditorListener(CellEditorListener l)
{
} public void removeCellEditorListener(CellEditorListener l)
{
} protected void fireEditingStopped()
{
} protected void fireEditingCanceled()
{
}
}class TableData
{
private int questionCount=59;
String[] columnName;
Object[][] rowData;
Integer[] questionNumber;
String[] questionType;
String[] attempted;
Boolean[] unAttempted;
Boolean[] isMarked;
TableData()
{
initTableData();
}
void initTableData()
{
// initialize array data of table column's name
columnName=new String[]{"No.","Type","Attempted","UnAttempted","Marked"};
// initialize array data of question number column
questionNumber=new Integer[questionCount];
for(int i=0;i<questionNumber.length;i++)
{
questionNumber[i]=new Integer(i);
}
// initialize array data of question type column
questionType=new String[questionCount];
for(int i=0;i<questionType.length;i++)
{
questionType[i]="Choice";
}
// initialize array data of attempted column
attempted=new String[questionCount];
for(int i=0;i<attempted.length;i++)
{
attempted[i]="";
}
// initialize array data of unAttempted column
unAttempted=new Boolean[questionCount];
for(int i=0;i<unAttempted.length;i++)
{
if (attempted[i]!="")
{
unAttempted[i]=new Boolean(true);
}
else
{
unAttempted[i]=new Boolean(false);
}
}
// initializate array data of isMarked column
isMarked=new Boolean[questionCount];
for(int i=0;i<isMarked.length;i++)
{
if(i%2==0)
{
isMarked[i]=new Boolean(true);
}
else
{
isMarked[i]=new Boolean(false);
}
}
// initializate array data of table
rowData=new Object[questionCount][5];
for(int i=0;i<rowData.length;i++)
{
for(int j=0;j<5;j++)
{
switch(j)
{
case 0:
rowData[i][j]=questionNumber[i];
break;
case 1:
rowData[i][j]=questionType[i];
break;
case 2:
rowData[i][j]=attempted[i];
break;
case 3:
rowData[i][j]=unAttempted[i];
break;
case 4:
rowData[i][j]=isMarked[i];
break;
}
}
}
}
}
// jframe container
class TableFrame extends JFrame
{
JTable jtable;
TableModel reviewTableModel;
Container contentpane=getContentPane();
private final boolean ALLOW_ROW_SELECTION=true;
public TableFrame()
{
super("Temp");
setDefaultCloseOperation(DISPOSE_ON_CLOSE);
setupJTable();
contentpane.add(new JScrollPane(jtable),"Center");
setBounds(100,100,800,600);
// setResizable(false);
setVisible(true);
// setExtendedState(Frame.MAXIMIZED_BOTH); // JDK1.4
} public void setupJTable()
{
reviewTableModel=new ReviewTableModel();
jtable=new JTable(reviewTableModel); // set up renderers and editors
jtable.setDefaultRenderer(Integer.class,new MyTableCellRenderer());
jtable.setDefaultEditor(Integer.class,new MyTableCellEditor()); // show table
jtable.setRowHeight(50);
// select one list index at a time.
jtable.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
if(ALLOW_ROW_SELECTION)
{
// ALLOW_ROW_SELECTION is true by default
ListSelectionModel rowSelectionModel=jtable.getSelectionModel();
rowSelectionModel.addListSelectionListener
(
new ListSelectionListener()
{
public void valueChanged(ListSelectionEvent e)
{
//Ignore extra messages.
if (e.getValueIsAdjusting())
{
return;
}
}
}
);
}
}
}
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货