该用什么方法。我的表是:
Object[][] playerInfo = new Object[0][5];
String[] Names = {
"ID" ,"地区", "学校代码", "学校名称", "详细地址"};
defaultModel = new DefaultTableModel(playerInfo, Names);
table = new JTable(defaultModel);
TableColumnModel columnModel=table.getColumnModel();
TableColumn column=columnModel.getColumn(0);
column.setMinWidth(0);
column.setMaxWidth(0);
column.setWidth(0);
column.setPreferredWidth(0);
为什么第一列还是显示。
4 个解决方案
#1
LZ 我测试你写的代码,并没有显示第一列啊。
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableColumn;
import javax.swing.table.TableColumnModel;
public class TestFrame extends JFrame
{
public TestFrame()
{
this.setSize(500, 400);
Object[][] playerInfo = new Object[0][5];
String[] names =
{
"ID", "地区", "学校代码", "学校名称", "详细地址"};
DefaultTableModel defaultModel = new DefaultTableModel(playerInfo, names);
JTable table = new JTable(defaultModel);
TableColumnModel columnModel = table.getColumnModel();
TableColumn column = columnModel.getColumn(0);
column.setMinWidth(0);
column.setMaxWidth(0);
column.setWidth(0);
column.setPreferredWidth(0);
this.add(new JScrollPane(table));
}
public static void main(String[] args)
{
(new TestFrame()).setVisible(true);
}
}
#2
我测试下来也没显示
import javax.swing.JFrame;
import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableColumn;
import javax.swing.table.TableColumnModel;
public class Test {
public static void main(String[] args) {
JFrame jf = new JFrame();
Object[][] playerInfo = new Object[1][5];
String[] Names = { "ID", "地区", "学校代码", "学校名称", "详细地址" };
playerInfo[0][0]="1";
playerInfo[0][1]="2";
playerInfo[0][2]="3";
playerInfo[0][3]="4";
playerInfo[0][4]="5";
DefaultTableModel defaultModel = new DefaultTableModel(playerInfo,Names);
JTable table = new JTable(defaultModel);
TableColumnModel columnModel = table.getColumnModel();
TableColumn column = columnModel.getColumn(0);
jf.add(table);
jf.setVisible(true);
column.setMinWidth(0);
column.setMaxWidth(0);
column.setWidth(0);
column.setPreferredWidth(0);
System.out.println(table.getModel().getValueAt(0, 0));
}
}
#3
学习了,原来隐藏可以这样处理,我以为要动态改变表中数据呢?
#4
不用set这么多,呵呵
这两个就够了
column.setMinWidth(0);
column.setMaxWidth(0);
试想,最大值都是零了,还能显示啥
这两个就够了
column.setMinWidth(0);
column.setMaxWidth(0);
试想,最大值都是零了,还能显示啥
#1
LZ 我测试你写的代码,并没有显示第一列啊。
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableColumn;
import javax.swing.table.TableColumnModel;
public class TestFrame extends JFrame
{
public TestFrame()
{
this.setSize(500, 400);
Object[][] playerInfo = new Object[0][5];
String[] names =
{
"ID", "地区", "学校代码", "学校名称", "详细地址"};
DefaultTableModel defaultModel = new DefaultTableModel(playerInfo, names);
JTable table = new JTable(defaultModel);
TableColumnModel columnModel = table.getColumnModel();
TableColumn column = columnModel.getColumn(0);
column.setMinWidth(0);
column.setMaxWidth(0);
column.setWidth(0);
column.setPreferredWidth(0);
this.add(new JScrollPane(table));
}
public static void main(String[] args)
{
(new TestFrame()).setVisible(true);
}
}
#2
我测试下来也没显示
import javax.swing.JFrame;
import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableColumn;
import javax.swing.table.TableColumnModel;
public class Test {
public static void main(String[] args) {
JFrame jf = new JFrame();
Object[][] playerInfo = new Object[1][5];
String[] Names = { "ID", "地区", "学校代码", "学校名称", "详细地址" };
playerInfo[0][0]="1";
playerInfo[0][1]="2";
playerInfo[0][2]="3";
playerInfo[0][3]="4";
playerInfo[0][4]="5";
DefaultTableModel defaultModel = new DefaultTableModel(playerInfo,Names);
JTable table = new JTable(defaultModel);
TableColumnModel columnModel = table.getColumnModel();
TableColumn column = columnModel.getColumn(0);
jf.add(table);
jf.setVisible(true);
column.setMinWidth(0);
column.setMaxWidth(0);
column.setWidth(0);
column.setPreferredWidth(0);
System.out.println(table.getModel().getValueAt(0, 0));
}
}
#3
学习了,原来隐藏可以这样处理,我以为要动态改变表中数据呢?
#4
不用set这么多,呵呵
这两个就够了
column.setMinWidth(0);
column.setMaxWidth(0);
试想,最大值都是零了,还能显示啥
这两个就够了
column.setMinWidth(0);
column.setMaxWidth(0);
试想,最大值都是零了,还能显示啥