请大侠帮我看看Java的JTable的隐藏列,这段代码有什么问题,为什么老报EventDispatchThread 20
publicclassHideTableTestextendsJFrame{privateJTabletable;DefaultTableModeltableMode;M...
public class HideTableTest extends JFrame
{
private JTable table;
DefaultTableModel tableMode;
Map<String, TableColumn> map = new HashMap<String,TableColumn>();
public static void main(String[] args)
{
new HideTableTest().launch();
}
private void launch()
{
setBounds(300, 300, 600, 300);
String[] columnNames = new String[] { "A", "B", "C", "D", "E" };
Object[][] data = new Object[][] { { 1, 2, 3, 4, 5, },
{ 1, 2, 3, 4, 5 }, { 1, 2, 3, 4, 5 }, };
tableMode = new DefaultTableModel(data, columnNames)
{
@Override
public boolean isCellEditable(int row, int column)
{
return false;
}
};
table = new JTable(tableMode);
JScrollPane pane = new JScrollPane(table);
add(pane, BorderLayout.CENTER);
table.setAutoCreateRowSorter(true);
table.setRowSelectionAllowed(true);
initHidePopMenu();
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);
}
private void initHidePopMenu()
{
JPopupMenu menu = new JPopupMenu();
table.getTableHeader().setComponentPopupMenu(menu);
int colSize = tableMode.getColumnCount();
for (int i = 0; i < colSize; i++)
{
String colName = tableMode.getColumnName(i);
JCheckBoxMenuItem menuItem = new JCheckBoxMenuItem(colName);
menuItem.addActionListener(new ActionListener()
{
@Override
public void actionPerformed(ActionEvent e)
{
JCheckBoxMenuItem menuItem = (JCheckBoxMenuItem) e
.getSource();
String colName = menuItem.getText();
if (menuItem.isSelected())
{
int colIndex = tableMode.findColumn(colName);
TableColumn t = table.getColumnModel().getColumn(
colIndex);
table.removeColumn(t);
map.put(colName, t);
} else
{
table.addColumn(map.get(colName));
}
}
});
menu.add(menuItem);
}
}
}
Exception in thread "AWT-EventQueue-0" java.lang.ArrayIndexOutOfBoundsException: -1
at java.awt.EventDispatchThread.run(EventDispatchThread.java:122)
请大侠帮我看看这个问题出在哪了?
我想把列隐藏,在原来的位置还原,请问要怎么改? 展开
{
private JTable table;
DefaultTableModel tableMode;
Map<String, TableColumn> map = new HashMap<String,TableColumn>();
public static void main(String[] args)
{
new HideTableTest().launch();
}
private void launch()
{
setBounds(300, 300, 600, 300);
String[] columnNames = new String[] { "A", "B", "C", "D", "E" };
Object[][] data = new Object[][] { { 1, 2, 3, 4, 5, },
{ 1, 2, 3, 4, 5 }, { 1, 2, 3, 4, 5 }, };
tableMode = new DefaultTableModel(data, columnNames)
{
@Override
public boolean isCellEditable(int row, int column)
{
return false;
}
};
table = new JTable(tableMode);
JScrollPane pane = new JScrollPane(table);
add(pane, BorderLayout.CENTER);
table.setAutoCreateRowSorter(true);
table.setRowSelectionAllowed(true);
initHidePopMenu();
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);
}
private void initHidePopMenu()
{
JPopupMenu menu = new JPopupMenu();
table.getTableHeader().setComponentPopupMenu(menu);
int colSize = tableMode.getColumnCount();
for (int i = 0; i < colSize; i++)
{
String colName = tableMode.getColumnName(i);
JCheckBoxMenuItem menuItem = new JCheckBoxMenuItem(colName);
menuItem.addActionListener(new ActionListener()
{
@Override
public void actionPerformed(ActionEvent e)
{
JCheckBoxMenuItem menuItem = (JCheckBoxMenuItem) e
.getSource();
String colName = menuItem.getText();
if (menuItem.isSelected())
{
int colIndex = tableMode.findColumn(colName);
TableColumn t = table.getColumnModel().getColumn(
colIndex);
table.removeColumn(t);
map.put(colName, t);
} else
{
table.addColumn(map.get(colName));
}
}
});
menu.add(menuItem);
}
}
}
Exception in thread "AWT-EventQueue-0" java.lang.ArrayIndexOutOfBoundsException: -1
at java.awt.EventDispatchThread.run(EventDispatchThread.java:122)
请大侠帮我看看这个问题出在哪了?
我想把列隐藏,在原来的位置还原,请问要怎么改? 展开
2个回答
展开全部
数组越界,是因为你的程序把TableColumn删除了,所以TableColumn数组就越界了。现在,我们不删除TableColumn,而是隐藏TableColumn,就是把TableColumn的宽度设为0,这样数组就不会越界了。
为了隐藏TableColumn,我们要继承JTable给它赋予三个属性,分别是hideColumn,isHideColumn和showColumn。
完整的程序如下:(改动的地方见注释)
import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.HashMap;
import java.util.Map;
import javax.swing.*;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableColumn;
public class HideTableTest extends JFrame{
private MyJTable table;//这里把private JTable table;改成private MyJTable table;
DefaultTableModel tableMode;
Map<String, Integer> map = new HashMap<String,Integer>();//这里把Map<String, TableColumn> map = new HashMap<String,TableColumn>();改成Map<String, Integer> map = new HashMap<String,Integer>();
public static void main(String[] args){
new HideTableTest().launch();
}
private void launch(){
setBounds(300, 300, 600, 300);
String[] columnNames = new String[] { "A", "B", "C", "D", "E" };
Object[][] data = new Object[][] { { 1, 2, 3, 4, 5, },{ 1, 2, 3, 4, 5 }, { 1, 2, 3, 4, 5 }, };
tableMode = new DefaultTableModel(data, columnNames){
@Override
public boolean isCellEditable(int row, int column){
return false;
}
};
table = new MyJTable(tableMode);//这里把table = new JTable(tableMode);改成table = new MyJTable(tableMode);
JScrollPane pane = new JScrollPane(table);
add(pane, BorderLayout.CENTER);
table.setAutoCreateRowSorter(true);
table.setRowSelectionAllowed(true);
initHidePopMenu();
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);
}
private void initHidePopMenu(){
JPopupMenu menu = new JPopupMenu();
table.getTableHeader().setComponentPopupMenu(menu);
int colSize = tableMode.getColumnCount();
for (int i = 0; i < colSize; i++){
String colName = tableMode.getColumnName(i);
JCheckBoxMenuItem menuItem = new JCheckBoxMenuItem(colName);
menuItem.addActionListener(new ActionListener(){
@Override
public void actionPerformed(ActionEvent e){
JCheckBoxMenuItem menuItem = (JCheckBoxMenuItem) e.getSource();
String colName = menuItem.getText();
if (menuItem.isSelected()){
int colIndex = tableMode.findColumn(colName);
//TableColumn t = table.getColumnModel().getColumn(colIndex);//去掉这一句
//table.removeColumn(t); //去掉这一句
table.hideColumn(colIndex);//这里加一句
map.put(colName, colIndex);//这里把map.put(colName, t);改成map.put(colName, colIndex);
} else{
//table.addColumn(map.get(colName));//去掉这一句
table.showColumn(map.get(colName));//这里加一句
}
}
});
menu.add(menuItem);
}
}
}
class MyJTable extends JTable{ //这里加了一个类MyJTable继承JTable
MyJTable(DefaultTableModel dtm){
super(dtm);
}
public void hideColumn(int index)
{
TableColumn tc = this.getColumnModel().getColumn(index);
tc.setMaxWidth(0);
tc.setPreferredWidth(0);
tc.setWidth(0);
tc.setMinWidth(0);
this.getTableHeader().getColumnModel().getColumn(index).setMaxWidth(0);
this.getTableHeader().getColumnModel().getColumn(index).setMinWidth(0);
}
public boolean isHideColumn(int index)
{
TableColumn tc = this.getColumnModel().getColumn(index);
if(tc.getWidth()==0){
return true;
}else{
return false;
}
}
public void showColumn(int index)
{
int width=super.getWidth();
int columnCount=super.getColumnCount();
TableColumn tc = this.getColumnModel().getColumn(index);
tc.setWidth(width/columnCount);
this.getTableHeader().getColumnModel().getColumn(index).setMaxWidth(width/columnCount);
this.getTableHeader().getColumnModel().getColumn(index).setMinWidth(width/columnCount);
}
}
为了隐藏TableColumn,我们要继承JTable给它赋予三个属性,分别是hideColumn,isHideColumn和showColumn。
完整的程序如下:(改动的地方见注释)
import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.HashMap;
import java.util.Map;
import javax.swing.*;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableColumn;
public class HideTableTest extends JFrame{
private MyJTable table;//这里把private JTable table;改成private MyJTable table;
DefaultTableModel tableMode;
Map<String, Integer> map = new HashMap<String,Integer>();//这里把Map<String, TableColumn> map = new HashMap<String,TableColumn>();改成Map<String, Integer> map = new HashMap<String,Integer>();
public static void main(String[] args){
new HideTableTest().launch();
}
private void launch(){
setBounds(300, 300, 600, 300);
String[] columnNames = new String[] { "A", "B", "C", "D", "E" };
Object[][] data = new Object[][] { { 1, 2, 3, 4, 5, },{ 1, 2, 3, 4, 5 }, { 1, 2, 3, 4, 5 }, };
tableMode = new DefaultTableModel(data, columnNames){
@Override
public boolean isCellEditable(int row, int column){
return false;
}
};
table = new MyJTable(tableMode);//这里把table = new JTable(tableMode);改成table = new MyJTable(tableMode);
JScrollPane pane = new JScrollPane(table);
add(pane, BorderLayout.CENTER);
table.setAutoCreateRowSorter(true);
table.setRowSelectionAllowed(true);
initHidePopMenu();
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);
}
private void initHidePopMenu(){
JPopupMenu menu = new JPopupMenu();
table.getTableHeader().setComponentPopupMenu(menu);
int colSize = tableMode.getColumnCount();
for (int i = 0; i < colSize; i++){
String colName = tableMode.getColumnName(i);
JCheckBoxMenuItem menuItem = new JCheckBoxMenuItem(colName);
menuItem.addActionListener(new ActionListener(){
@Override
public void actionPerformed(ActionEvent e){
JCheckBoxMenuItem menuItem = (JCheckBoxMenuItem) e.getSource();
String colName = menuItem.getText();
if (menuItem.isSelected()){
int colIndex = tableMode.findColumn(colName);
//TableColumn t = table.getColumnModel().getColumn(colIndex);//去掉这一句
//table.removeColumn(t); //去掉这一句
table.hideColumn(colIndex);//这里加一句
map.put(colName, colIndex);//这里把map.put(colName, t);改成map.put(colName, colIndex);
} else{
//table.addColumn(map.get(colName));//去掉这一句
table.showColumn(map.get(colName));//这里加一句
}
}
});
menu.add(menuItem);
}
}
}
class MyJTable extends JTable{ //这里加了一个类MyJTable继承JTable
MyJTable(DefaultTableModel dtm){
super(dtm);
}
public void hideColumn(int index)
{
TableColumn tc = this.getColumnModel().getColumn(index);
tc.setMaxWidth(0);
tc.setPreferredWidth(0);
tc.setWidth(0);
tc.setMinWidth(0);
this.getTableHeader().getColumnModel().getColumn(index).setMaxWidth(0);
this.getTableHeader().getColumnModel().getColumn(index).setMinWidth(0);
}
public boolean isHideColumn(int index)
{
TableColumn tc = this.getColumnModel().getColumn(index);
if(tc.getWidth()==0){
return true;
}else{
return false;
}
}
public void showColumn(int index)
{
int width=super.getWidth();
int columnCount=super.getColumnCount();
TableColumn tc = this.getColumnModel().getColumn(index);
tc.setWidth(width/columnCount);
this.getTableHeader().getColumnModel().getColumn(index).setMaxWidth(width/columnCount);
this.getTableHeader().getColumnModel().getColumn(index).setMinWidth(width/columnCount);
}
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询