主题:[讨论]JTable里面的一个难题
搞了几天几夜都不知道这段代码究竟有什么问题.
JTable显示不出来.
package swing;
import java.awt.*;
import javax.swing.*;
import java.awt.BorderLayout;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableColumnModel;
public class TableDemo extends JFrame {
BorderLayout borderLayout1 = new BorderLayout();
JScrollPane jScrollPane1 = new JScrollPane();
JTable jTable1 = new JTable();
JComboBox cbo = new JComboBox();
DefaultTableModel tmodel = new DefaultTableModel();
public TableDemo() {
try {
jbInit();
} catch (Exception exception) {
exception.printStackTrace();
}
}
private void jbInit() throws Exception {
getContentPane().setLayout(borderLayout1);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.getContentPane().add(jScrollPane1, java.awt.BorderLayout.CENTER);
jScrollPane1.getViewport().add(jTable1);
cbo.addItem("Java");
cbo.addItem("Oracle");
cbo.addItem("C#");
cbo.addItem("Linux");
tmodel.addColumn("列A");
tmodel.addColumn("列B");
tmodel.addRow(new Object[] {"教育", ""});
DefaultCellEditor ce = new DefaultCellEditor(cbo);
TableColumnModel cm = jTable1.getColumnModel();
cm.getColumn(1).setCellEditor(ce); //此处的1为每一行的第二列,列号从0开始;
jTable1.setModel(tmodel);
}
public static void main(String[] args) {
TableDemo tabledemo = new TableDemo();
tabledemo.setTitle("技能設置");
tabledemo.setSize(400, 300);
tabledemo.setVisible(true);
}
}
JTable显示不出来.
package swing;
import java.awt.*;
import javax.swing.*;
import java.awt.BorderLayout;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableColumnModel;
public class TableDemo extends JFrame {
BorderLayout borderLayout1 = new BorderLayout();
JScrollPane jScrollPane1 = new JScrollPane();
JTable jTable1 = new JTable();
JComboBox cbo = new JComboBox();
DefaultTableModel tmodel = new DefaultTableModel();
public TableDemo() {
try {
jbInit();
} catch (Exception exception) {
exception.printStackTrace();
}
}
private void jbInit() throws Exception {
getContentPane().setLayout(borderLayout1);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.getContentPane().add(jScrollPane1, java.awt.BorderLayout.CENTER);
jScrollPane1.getViewport().add(jTable1);
cbo.addItem("Java");
cbo.addItem("Oracle");
cbo.addItem("C#");
cbo.addItem("Linux");
tmodel.addColumn("列A");
tmodel.addColumn("列B");
tmodel.addRow(new Object[] {"教育", ""});
DefaultCellEditor ce = new DefaultCellEditor(cbo);
TableColumnModel cm = jTable1.getColumnModel();
cm.getColumn(1).setCellEditor(ce); //此处的1为每一行的第二列,列号从0开始;
jTable1.setModel(tmodel);
}
public static void main(String[] args) {
TableDemo tabledemo = new TableDemo();
tabledemo.setTitle("技能設置");
tabledemo.setSize(400, 300);
tabledemo.setVisible(true);
}
}