i)我的表中没有列和最后一列的jcheckbox。 ii)我使用组合框来选择某个值。 iii)基于组合框的值,jbutton将数据加载到表中。 iv)当我 重新加载 数据到表中时,新数据将显示在jtable中。 v)问题在于,当我按下Jcheckbox时,旧数据将显示在jtable中。 代码如下:
public class aap2 extends JFrame {
@SuppressWarnings({ "unchecked", "rawtypes" })
static JComboBox year = new JComboBox(new Object[]
{"2012-13", "2013-14", "2014-15", "2015-16","2016-17","2017-
18","2018-19"});
@SuppressWarnings({ "unchecked", "rawtypes" })
static JComboBox division = new JComboBox(new Object[]
{"Name", "address","profession","others"});
JComboBox schemetype = new JComboBox(new Object[]
{});
JButton showschemes = new JButton("Show Schemes");
static ResultSet rs;
Connection con;
Statement st;
static JPanel panel = new JPanel();
static JPanel panel1 = new JPanel();
static JPanel panel2= new JPanel();
static JPanel panel3= new JPanel();
static JPanel panel4= new JPanel();
static JPanel panel5= new JPanel();
UIManager ui= new UIManager();
static int columns;
static int sel[];
JTable table;
DefaultTableModel dtm;
public aap2(){
division.setMaximumRowCount(5);
year.setMaximumRowCount(5);
year.setForeground(Color.blue);
year.setBackground(Color.white);
year.setSelectedItem("2009-10");
setBounds(00,40,1020,700);
Color c= new Color(160,200,100);
getContentPane().setBackground(c);
Color c3= new Color(0,50,50,2);
panel1.setBackground(c3);
panel2.setBackground(c3);
panel.setBackground(c);
panel.setBorder(new TitledBorder(new LineBorder(Color.white,1),""));
panel.setLayout(new GridBagLayout());
GridBagConstraints gc= new GridBagConstraints();
panel1.setLayout( new GridBagLayout());
GridBagConstraints gc1 = new GridBagConstraints();
gc1.gridx=0;
gc1.gridy=0;
panel1.add( new JLabel("Financial Year"),gc1);
gc1.insets= new Insets(1,10,1,10);
gc1.gridx=1;
gc1.gridy=0;
panel1.add(year,gc1);
gc1.gridx=4;
gc1.gridy=0;
panel1.add( new JLabel("Division"),gc1);
gc1.gridx=5;
gc1.gridy=0;
panel1.add(division,gc1);
gc.gridx=0;
gc.gridy=0;
panel.add(panel1,gc);
JPanel p2= new JPanel();
gc.gridx=0;
gc.gridy=4;
p2.setBackground(c3);
panel.add(p2,gc);
panel3.setLayout( new GridBagLayout());
GridBagConstraints gc3 = new GridBagConstraints();
gc3.insets= new Insets(30,10,1,10);
gc3.gridx=0;
gc3.gridy=2;
panel3.add(showschemes,gc3);
showschemes.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e){
showschemsActionPerformed(e);
}
});
gc.gridx=0;
gc.gridy=5;
panel3.setBackground(c3);
panel.add(panel3,gc);
add(panel, BorderLayout.NORTH);
setUndecorated(true);
getRootPane().setWindowDecorationStyle(JRootPane.PLAIN_DIALOG);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);
}
public void showschemsActionPerformed(ActionEvent e) {
showtable();
}
public void showtable() {
final Vector<String> columnNames = new Vector<String>();
final Vector<Vector<Object>> data = new Vector<Vector<Object>>();
try{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
System.out.println("Driver loaded");
// Establish a connection
con= DriverManager.getConnection
("jdbc:odbc:ysr");
System.out.println("Database connecteddddd");
// Create a statement
st = con.createStatement();
ResultSet rs = st.executeQuery("SELECT * FROM ysr2011 where=
'"+division.getSelectedItem()+"' ");
ResultSetMetaData md = rs.getMetaData();
columns = md.getColumnCount();
System.out.println("col" +(columns+1));
for (int i = 1; i <= columns; i++) {
columnNames.addElement( md.getColumnName(i) );
}
columnNames.addElement("Save");
while (rs.next()) {
Vector<Object> row = new Vector<Object>(columns);
for (int i = 1; i <= columns; i++) {
row.addElement( rs.getObject(i) );
}
row.addElement(new Boolean(false));
data.addElement( row );
}
rs.close();
con.close();
st.close();
}
catch(Exception e1){}
dtm = new DefaultTableModel(data, columnNames) {
public Class getColumnClass(int col) {
if(col==columns){
return Boolean.class;
}else{
return String.class;
}
}
public boolean isCellEditable(int rowIndex, int colIndex) {
return (colIndex == columns);
}
};
dtm.fireTableDataChanged();
table= new JTable(dtm); ;
table.setFont(new Font(" Arial",Font.PLAIN,12));
table.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
table.setAutoCreateRowSorter(true);
JTableHeader header = table.getTableHeader();
header.setBackground(Color.yellow);
table.setRowSelectionAllowed(false);
header.setFont(new Font(" Arial",Font.BOLD,12));
JScrollPane scrollPane = new JScrollPane(table);
JButton button= new JButton("Save");
button.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
for (int row = 0; row < table.getRowCount(); row++) {
Boolean b = ((Boolean) table.getValueAt(row, columns));
if (b.booleanValue()) {
System.out.print("row " + row + " is " + b + ": ");
for (int col = 0; col < table.getColumnCount(); col++) {
System.out.print(table.getValueAt(row, col) + " ");
}
System.out.println();
}
}
}
});
JPanel buttonpanel= new JPanel();
buttonpanel.add(button);
add(scrollPane,BorderLayout.CENTER);
add(buttonpanel,BorderLayout.SOUTH);
Color c1= new Color(160,200,100);
table.setBackground(c1);
buttonpanel.setBackground(c1);
setBackground(c1);
setVisible(true);
}
public static void main(String args[]){
new aap2();
}
}
答案 0 :(得分:1)
如果要更改表中的数据,请尝试创建新的DefaultTableModel
对象(或实现TableModel
接口的任何其他对象),并调用table.setModel(yourNewModel);