JTable删除列的问题 请问各位大虾如何将JTable中的某一列进行彻底的删除?谢谢了 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 TableColumnModel tcm = table.getColumnModel();TableColumn firstNameColumn = table.getColumn("ColumnName"); 例: 添加和删除列--------------------------------------------------------------------------------import javax.swing.*;import javax.swing.table.*;import java.awt.*;import java.awt.event.*;import java.util.*; public class Test extends JFrame {JTable table = new JTable(new Object[][] {{"Mouse", "Mighty", "M." },{"Mouse", "Polly", "A." },{"Doright", "Dudley", "L." }},new Object[] {"Last Name", "First Name", "Middle Initial" });public Test() {Container cp = getContentPane();cp.add(new JScrollPane(table), BorderLayout.CENTER);cp.add(new ControlPanel(), BorderLayout.NORTH);}class ControlPanel extends JPanel {private JCheckBox checkBox = new JCheckBox("First Name Column Showing");public ControlPanel() {final TableColumnModel tcm = table.getColumnModel();final TableColumn firstNameColumn = table.getColumn("First Name");checkBox.setSelected(true);add(checkBox);checkBox.addActionListener(new ActionListener() {public void actionPerformed(ActionEvent event) {if(checkBox.isSelected()) {tcm.addColumn(firstNameColumn);tcm.moveColumn(2,1);}else {tcm.removeColumn(firstNameColumn);}table.sizeColumnsToFit(-1);}});}}public static void main(String args[]) {GJApp.launch(new Test(), "Showing/Hiding Columns",300,300,450,175);}}class GJApp extends WindowAdapter {static private JPanel statusArea = new JPanel();static private JLabel status = new JLabel(" ");static private ResourceBundle resources;public static void launch(final JFrame f, String title,final int x, final int y, final int w, int h) {launch(f,title,x,y,w,h,null); }public static void launch(final JFrame f, String title,final int x, final int y, final int w, int h,String propertiesFilename) {f.setTitle(title);f.setBounds(x,y,w,h);f.setVisible(true);statusArea.setBorder(BorderFactory.createEtchedBorder());statusArea.setLayout(new FlowLayout(FlowLayout.LEFT,0,0));statusArea.add(status);status.setHorizontalAlignment(JLabel.LEFT);f.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);if(propertiesFilename != null) {resources = ResourceBundle.getBundle(propertiesFilename, Locale.getDefault());}f.addWindowListener(new WindowAdapter() {public void windowClosed(WindowEvent e) {System.exit(0);}});}static public JPanel getStatusArea() {return statusArea;}static public void showStatus(String s) {status.setText(s);}static Object getResource(String key) {if(resources != null) {return resources.getString(key);}return null;}} 求助,Java中遇到的问题! 能帮我看看这段代码是在干什么的吗? java的认识 java的OutOfMemory问题 基础: 抽象类不能用实例化,但可以创建对象引用,是什么意思呢? 环境变量设置后,javac不可用? 请高手指教--怎么用javac编译互相import的包? !!!中文问题,up有分 关于jbuilder的问题,来者有分。 能告诉我在哪儿下最新的tomcat及如何配置么?一定要说的详细哦!谢了先,定给分! Graphics里面没有清除画图板的方法。怎么实现? 请高手赐教!很急!
TableColumn firstNameColumn = table.getColumn("ColumnName");
--------------------------------------------------------------------------------
import javax.swing.*;
import javax.swing.table.*;
import java.awt.*;
import java.awt.event.*;
import java.util.*;
public class Test extends JFrame {
JTable table = new JTable(
new Object[][] {
{"Mouse", "Mighty", "M." },
{"Mouse", "Polly", "A." },
{"Doright", "Dudley", "L." }
},
new Object[] {
"Last Name", "First Name", "Middle Initial"
}
);
public Test() {
Container cp = getContentPane();cp.add(new JScrollPane(table), BorderLayout.CENTER);
cp.add(new ControlPanel(), BorderLayout.NORTH);
}
class ControlPanel extends JPanel {
private JCheckBox checkBox = new JCheckBox(
"First Name Column Showing");
public ControlPanel() {
final TableColumnModel tcm = table.getColumnModel();
final TableColumn firstNameColumn =
table.getColumn("First Name");checkBox.setSelected(true);
add(checkBox);checkBox.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent event) {
if(checkBox.isSelected()) {
tcm.addColumn(firstNameColumn);
tcm.moveColumn(2,1);
}
else {
tcm.removeColumn(firstNameColumn);
}
table.sizeColumnsToFit(-1);
}
});
}
}
public static void main(String args[]) {
GJApp.launch(
new Test(), "Showing/Hiding Columns",300,300,450,175);
}
}
class GJApp extends WindowAdapter {
static private JPanel statusArea = new JPanel();
static private JLabel status = new JLabel(" ");
static private ResourceBundle resources;public static void launch(final JFrame f, String title,
final int x, final int y,
final int w, int h) {
launch(f,title,x,y,w,h,null);
}
public static void launch(final JFrame f, String title,
final int x, final int y,
final int w, int h,
String propertiesFilename) {
f.setTitle(title);
f.setBounds(x,y,w,h);
f.setVisible(true);statusArea.setBorder(BorderFactory.createEtchedBorder());
statusArea.setLayout(new FlowLayout(FlowLayout.LEFT,0,0));
statusArea.add(status);
status.setHorizontalAlignment(JLabel.LEFT);f.setDefaultCloseOperation(
WindowConstants.DISPOSE_ON_CLOSE);if(propertiesFilename != null) {
resources = ResourceBundle.getBundle(
propertiesFilename, Locale.getDefault());
}f.addWindowListener(new WindowAdapter() {
public void windowClosed(WindowEvent e) {
System.exit(0);
}
});
}
static public JPanel getStatusArea() {
return statusArea;
}
static public void showStatus(String s) {
status.setText(s);
}
static Object getResource(String key) {
if(resources != null) {
return resources.getString(key);
}
return null;
}
}