我创建了下表。但是,在运行示例程序时,我会在tcm.getColumn(0).setHeaderValue(columnNames[0]);
行
Exception in thread "AWT-EventQueue-0" java.lang.ArrayIndexOutOfBoundsException: 0 >= 0
at java.util.Vector.elementAt(Unknown Source)
at javax.swing.table.DefaultTableColumnModel.getColumn(Unknown Source)
at test.tableHeader.JTableRowHeader.<init>(JTableRowHeader.java:62)
at test.tableHeader.JTableRowHeader$6.run(JTableRowHeader.java:169)
at java.awt.event.InvocationEvent.dispatch(Unknown Source)
at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
at java.awt.EventQueue.access$400(Unknown Source)
at java.awt.EventQueue$2.run(Unknown Source)
at java.awt.EventQueue$2.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.AccessControlContext$1.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue.dispatchEvent(Unknown Source)
at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.run(Unknown Source)
com.passlogix.vgo.ho.WindowHandleException: null: javax.swing.JFrame[frame0,0,0,0x0,invalid,hidden,layout=java.awt.BorderLayout,title=JTable,resizable,normal,defaultCloseOperation=HIDE_ON_CLOSE,rootPane=javax.swing.JRootPane[,0,0,0x0,invalid,layout=javax.swing.JRootPane$RootLayout,alignmentX=0.0,alignmentY=0.0,border=javax.swing.plaf.synth.SynthBorder@949f69,flags=16777609,maximumSize=,minimumSize=,preferredSize=],rootPaneCheckingEnabled=true]
at com.passlogix.vgo.ho.ApplicationWindow.nativeGetWindowHandle(Native Method)
at com.passlogix.vgo.ho.ApplicationWindowAccessJava1dot4.getHWnd(ApplicationWindowAccessJava1dot4.java:50)
at com.passlogix.vgo.ho.WindowScanner.run(WindowScanner.java:569)
这是一个可重复的示例程序:
import java.awt.Color;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Font;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.SwingConstants;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
import javax.swing.event.RowSorterEvent;
import javax.swing.event.RowSorterListener;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableCellRenderer;
import javax.swing.table.TableColumnModel;
import javax.swing.table.TableModel;
import javax.swing.table.TableRowSorter;
public class JTableRowHeader {
private JFrame frame = new JFrame("JTable");
private JScrollPane scrollPane;
private JTable tableAnalytics;
private DefaultTableModel model;
private DefaultTableModel model1;
private TableRowSorter<TableModel> sorter;
private JTable headerTable;
private String[] columnNames = {"AVERAGE", "2014", "2015", "2016", "2017", "2018", "2019", "2020", "2021", "2022", "2023"};
private String[] rowNames = {"Production Costs", "Average Equity", "Debt Yield Percentage", "Sales Valuation"};
public JTableRowHeader() {
model1 = new DefaultTableModel() {
/**
* UUID
*/
private static final long serialVersionUID = 8538648271391086030L;
@Override
public boolean isCellEditable(int row, int col) {
return false;
}
};
tableAnalytics = new JTable(rowNames.length, columnNames.length);
tableAnalytics.setModel(model1);
for (int i = 0; i < model1.getRowCount(); i++) {
for (int ii = 0; ii < model1.getRowCount(); ii++) {
model1.setValueAt(i + ii, i, ii);
}
}
TableColumnModel tcm = tableAnalytics.getColumnModel();
tcm.getColumn(0).setHeaderValue(columnNames[0]);//here I get the expection!
tcm.getColumn(1).setHeaderValue(columnNames[1]);
tcm.getColumn(2).setHeaderValue(columnNames[2]);
tcm.getColumn(3).setHeaderValue(columnNames[3]);
tcm.getColumn(4).setHeaderValue(columnNames[4]);
tcm.getColumn(5).setHeaderValue(columnNames[5]);
tcm.getColumn(6).setHeaderValue(columnNames[6]);
tcm.getColumn(7).setHeaderValue(columnNames[7]);
tcm.getColumn(8).setHeaderValue(columnNames[8]);
tcm.getColumn(9).setHeaderValue(columnNames[9]);
sorter = new TableRowSorter<TableModel>(tableAnalytics.getModel());
tableAnalytics.setRowSorter(sorter);
model = new DefaultTableModel() {
private static final long serialVersionUID = 12312432141L;
@Override
public int getColumnCount() {
return 1;
}
@Override
public boolean isCellEditable(int row, int col) {
return false;
}
@Override
public int getRowCount() {
return tableAnalytics.getRowCount();
}
@Override
public Class<?> getColumnClass(int colNum) {
switch (colNum) {
case 0:
return String.class;
default:
return super.getColumnClass(colNum);
}
}
};
headerTable = new JTable(model);
headerTable.setValueAt(rowNames[0], 0, 0);
headerTable.setValueAt(rowNames[1], 1, 0);
headerTable.setValueAt(rowNames[2], 2, 0);
headerTable.setValueAt(rowNames[3], 3, 0);
headerTable.setShowGrid(false);
headerTable.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
headerTable.setPreferredScrollableViewportSize(new Dimension(50, 0));
headerTable.getColumnModel().getColumn(0).setPreferredWidth(50);
headerTable.getColumnModel().getColumn(0).setCellRenderer(new TableCellRenderer() {
@Override
public Component getTableCellRendererComponent(JTable x, Object value, boolean isSelected, boolean hasFocus, int row, int column) {
boolean selected = tableAnalytics.getSelectionModel().isSelectedIndex(row);
Component component = tableAnalytics.getTableHeader().getDefaultRenderer().getTableCellRendererComponent(tableAnalytics, value, false, false, -1, -2);
((JLabel) component).setHorizontalAlignment(SwingConstants.CENTER);
if (selected) {
component.setFont(component.getFont().deriveFont(Font.BOLD));
component.setForeground(Color.red);
} else {
component.setFont(component.getFont().deriveFont(Font.PLAIN));
}
return component;
}
});
tableAnalytics.getRowSorter().addRowSorterListener(new RowSorterListener() {
@Override
public void sorterChanged(RowSorterEvent e) {
model.fireTableDataChanged();
}
});
tableAnalytics.getSelectionModel().addListSelectionListener(new ListSelectionListener() {
@Override
public void valueChanged(ListSelectionEvent e) {
model.fireTableRowsUpdated(0, model.getRowCount() - 1);
}
});
scrollPane = new JScrollPane(tableAnalytics);
scrollPane.setRowHeaderView(headerTable);
tableAnalytics.setPreferredScrollableViewportSize(tableAnalytics.getPreferredSize());
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
frame.add(scrollPane);
frame.pack();
frame.setLocation(150, 150);
frame.setVisible(true);
}
public static void main(String[] args) {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (InstantiationException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalAccessException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (UnsupportedLookAndFeelException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
@SuppressWarnings("unused")
JTableRowHeader TestTableRowHeader = new JTableRowHeader();
}
});
}
}
任何想法我做错了什么?
更新
我现在尝试动态创建我的列标题,其中包含:
TableColumnModel tcm = tableAnalytics.getColumnModel();
for (int k = 0; k < tcm.getColumnCount(); k++) {
tcm.getColumn(k).setHeaderValue(columnNames[k]);
}
而不是:
tcm.getColumn(0).setHeaderValue(columnNames[0]);
tcm.getColumn(1).setHeaderValue(columnNames[1]);
tcm.getColumn(2).setHeaderValue(columnNames[2]);
tcm.getColumn(3).setHeaderValue(columnNames[3]);
tcm.getColumn(4).setHeaderValue(columnNames[4]);
tcm.getColumn(5).setHeaderValue(columnNames[5]);
tcm.getColumn(6).setHeaderValue(columnNames[6]);
tcm.getColumn(7).setHeaderValue(columnNames[7]);
tcm.getColumn(8).setHeaderValue(columnNames[8]);
tcm.getColumn(9).setHeaderValue(columnNames[9]);
但是,我仍然收到错误:
Exception in thread "AWT-EventQueue-0" java.lang.ArrayIndexOutOfBoundsException: 0 >= 0
答案 0 :(得分:3)
这是您rowNames
的初始化:
private String[] rowNames = {"Production Costs", "Average Equity", "Debt Yield Percentage", "Sales Valuation"};
此数组只有4个元素,因此rowNames[4]
超出范围。有效索引为0到3。
答案 1 :(得分:3)
下一步你的问题:
您使用行/列创建JTable
:
tableAnalytics = new JTable(rowNames.length, columnNames.length);
但是你用行= 0 / columns = 0覆盖模型tableAnalytics.setModel(model1);
,因为你得到了ArrayIndexOutOfBoundsException
。
像下一个创建JTable
:
DefaultTableModel model1 = new DefaultTableModel(rowNames.length, columnNames.length) {
/**
* UUID
*/
private static final long serialVersionUID = 8538648271391086030L;
@Override
public boolean isCellEditable(int row, int col) {
return false;
}
};
JTable tableAnalytics = new JTable(model1);