DefaultTableModel を拡張するクラス MyTableModel を作成しました。そして、私が望むのは、すでに 3 つの列をデータで初期化していることです。MyTableModel のコンストラクター内で呼び出してヘッダー/データ値を設定しましthis.addColumn("FIRST COL", FIRST_COL_VALUES);
たが、「スレッド "main" java.lang.ArrayIndexOutOfBoundsException での例外: 0 >= 0」という例外が発生しました。これを修正するにはどうすればよいですか?
public MyTableModel () {
// code below is what I tried so far to fix the exception
//super(INPUT_ROW_COUNT, 1);
// next three lines, is from what I expect to work, but instead I get exception
//this.addColumn("First Column", FIRST_COL_VALUES);
//this.addColumn("Second Column", SECOND_COL_VALUES);
//this.addColumn("Third Column", THIRD_COL_VALUES);
//TableColumn column = new TableColumn();
//column.setModelIndex(0);
//column.setHeaderValue("String value");
//this.addColumn(column);
}
アップデート:
public class Main {
public Main() {
Model model = new Model();
Model.TTableModel tableModel = model.new TTableModel();
Model.TComboBoxModel cBoxModel = model.new TComboBoxModel();
View view = new View(tableModel, cBoxModel);
new Controller(view, model, tableModel, cBoxModel);
}
public static void main(String[] args) {
new Main();
}
}
import java.util.ArrayList;
import javax.swing.AbstractListModel;
import javax.swing.ComboBoxModel;
import javax.swing.table.DefaultTableModel;
public class Model {
private static final int ROW_COUNT = 8;
private ArrayList<Object> columnsNames = new ArrayList<>();
private Object[][] data = new Object[ROW_COUNT][0];
private int columnIndex;
public int getColumnIndex() {
return columnIndex;
}
public void increaseColumnIndex() {
columnIndex++;
}
public void decreaseColumnIndex() {
columnIndex--;
}
public void displayArrayListObjects() {
System.out.println(columnsNames);
}
class TTableModel extends DefaultTableModel {
public TTableModel() {
//super(new Object[][]{{1, 2, 3, 4, 5, 6, 7, 8}, {4, 5, 6, 7}},new String[]{"First", "Second", "Third"});
this.addColumn("First", new Object[] {1, 2, 3, 4, 5, 6, 7, 8});
this.addColumn("Second", new Object[] {1, 2, 3, 4, 5, 6, 7, 8});
this.addColumn("Third", new Object[] {1, 2, 3, 4, 5, 6, 7, 8});
}
public void addColumn(String header) {
columnsNames.add(header);
fireTableStructureChanged();
}
public void removeColumn() {
if (columnIndex >= 0 && columnIndex < getColumnCount()) {
columnsNames.remove(columnIndex);
fireTableStructureChanged();
}
}
@Override
public int getColumnCount() {
return columnsNames.size();
}
@Override
public String getColumnName(int column) {
return columnsNames.get(column).toString();
}
@Override
public int getRowCount() {
if (data != null) {
return data.length;
}
return 0;
}
@Override
public Object getValueAt(int row, int col) {
return data[row][col];
}
}
class TComboBoxModel extends AbstractListModel implements ComboBoxModel {
private Object selectedItem = null;
private ArrayList<Object> itemList = new ArrayList<>();
public void addItem(String item) {
this.itemList.add(item);
this.fireIntervalAdded(item, columnIndex, columnIndex);
}
public void removeItem() {
if (columnIndex >= 0 && columnIndex < getSize()) {
this.itemList.remove(columnIndex);
this.fireIntervalRemoved(selectedItem, columnIndex, columnIndex);
}
}
public void updateSelectedItem() {
if (itemList.get(0) != null) {
selectedItem = itemList.get(0);
} else {
if (selectedItem != null) {
selectedItem = null;
this.fireContentsChanged(selectedItem, -1, -1);
}
}
}
@Override
public void setSelectedItem(Object anObject) {
if ((selectedItem != null && !selectedItem.equals(anObject)) || selectedItem == null && anObject != null) {
this.selectedItem = anObject;
this.fireContentsChanged(anObject, -1, -1);
}
}
@Override
public Object getSelectedItem() {
return selectedItem;
}
@Override
public int getSize() {
return columnsNames.size();
}
@Override
public Object getElementAt(int index) {
return columnsNames.get(index).toString();
}
}
}
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import mvc1.Model.TComboBoxModel;
import mvc1.Model.TTableModel;
public class Controller {
private View view;
private Model model;
private Model.TTableModel tableModel;
private Model.TComboBoxModel cBoxModel;
public Controller(View view, Model model, TTableModel tableModel, TComboBoxModel cBoxModel) {
this.view = view;
this.model = model;
this.tableModel = tableModel;
this.cBoxModel = cBoxModel;
this.view.addButtonListener(new ButtonActionListener());
}
class ButtonActionListener implements ActionListener {
@Override
public void actionPerformed(ActionEvent e) {
String s = e.getActionCommand();
switch (s) {
case "Add New Column":
model.increaseColumnIndex();
tableModel.addColumn("Field " + model.getColumnIndex());
break;
case "Remove Column":
if (model.getColumnIndex() != 0) {
model.decreaseColumnIndex();
tableModel.removeColumn();
} else {
view.displayErrorMessage("There is no column left to remove.");
}
break;
case "Calculate Column":
model.displayArrayListObjects();
break;
case "Final Results Of All Rows":
break;
}
}
}
}
import java.awt.BorderLayout;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.table.TableModel;
public class View {
private JMenuBar menuBar;
private JMenu menuFile;
private JMenuItem newMenuItem;
private JMenuItem exitMenuItem;
private JMenu menuView;
private JMenuItem aboutMenuItem;
private JButton addNewColumnButton;
private JButton removeColumnButton;
private JButton calculateColumnButton;
private JButton totalResultButton;
private JLabel textLabel;
private JTextField columnField;
private JTextField finalResultField;
private JComboBox columnListCB;
private JTable table;
public View(TableModel tableModel, ComboBoxModel cBoxModel) {
JFrame frame = new JFrame("MVC");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setResizable(false);
frame.setJMenuBar(getMenuBarComponent());
frame.add(getPanelComponents(tableModel, cBoxModel));
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
void addMenuListener(ActionListener l) {
newMenuItem.addActionListener(l);
aboutMenuItem.addActionListener(l);
exitMenuItem.addActionListener(l);
}
void addButtonListener(ActionListener l) {
addNewColumnButton.addActionListener(l);
removeColumnButton.addActionListener(l);
calculateColumnButton.addActionListener(l);
totalResultButton.addActionListener(l);
}
void displayErrorMessage(String errorMessage) {
JOptionPane.showMessageDialog(null, errorMessage, "Error Message", JOptionPane.ERROR_MESSAGE);
}
void displayInfoMessage(String infoMessage) {
JOptionPane.showMessageDialog(null, infoMessage, "Information Message", JOptionPane.INFORMATION_MESSAGE);
}
private JMenuBar getMenuBarComponent() {
menuBar = new JMenuBar();
menuFile = new JMenu("File");
newMenuItem = new JMenuItem("New");
menuFile.add(newMenuItem);
menuFile.addSeparator();
exitMenuItem = new JMenuItem("Exit");
menuFile.add(exitMenuItem);
menuView = new JMenu("View");
aboutMenuItem = new JMenuItem("About Me");
menuView.add(aboutMenuItem);
menuBar.add(menuFile);
menuBar.add(menuView);
return menuBar;
}
private JPanel getPanelComponents(TableModel tableModel, ComboBoxModel cBoxModel) {
JPanel panel = new JPanel(new BorderLayout());
panel.setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10));
panel.add(centerPanel(tableModel), BorderLayout.CENTER);
panel.add(southPanel(), BorderLayout.SOUTH);
panel.add(eastPanel(cBoxModel), BorderLayout.EAST);
return panel;
}
private JPanel centerPanel(TableModel tableModel) {
JPanel centerPanel = new JPanel(new GridLayout());
table = new JTable(tableModel);
table.setPreferredScrollableViewportSize(new Dimension(450, 150));
table.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
centerPanel.add(new JScrollPane(table));
return centerPanel;
}
private JPanel southPanel() {
JPanel southPanel = new JPanel(new FlowLayout(FlowLayout.LEFT, 5, 5));
totalResultButton = new JButton("Final Results Of All Rows");
southPanel.add(totalResultButton);
textLabel = new JLabel("Total result:");
southPanel.add(textLabel);
finalResultField = new JTextField();
finalResultField.setPreferredSize(new Dimension(50, 25));
southPanel.add(finalResultField);
return southPanel;
}
private JPanel eastPanel(ComboBoxModel cBoxModel) {
JPanel eastPanel = new JPanel(new FlowLayout(FlowLayout.CENTER, 5, 0));
Box eastPanelBox = Box.createVerticalBox();
addNewColumnButton = new JButton("Add New Column");
addNewColumnButton.setAlignmentX(Box.CENTER_ALIGNMENT);
eastPanelBox.add(addNewColumnButton);
eastPanelBox.add(Box.createVerticalStrut(5));
removeColumnButton = new JButton("Remove Column");
removeColumnButton.setAlignmentX(Box.CENTER_ALIGNMENT);
eastPanelBox.add(removeColumnButton);
eastPanelBox.add(Box.createVerticalStrut(5));
columnField = new JTextField();
columnField.setAlignmentX(Box.CENTER_ALIGNMENT);
columnField.setPreferredSize(new Dimension(130, 25));
eastPanelBox.add(columnField);
eastPanelBox.add(Box.createVerticalStrut(5));
columnListCB = new JComboBox(cBoxModel);
columnListCB.setAlignmentX(Box.CENTER_ALIGNMENT);
eastPanelBox.add(columnListCB); // might need to remove
eastPanelBox.add(Box.createVerticalStrut(5));
calculateColumnButton = new JButton("Calculate Column");
calculateColumnButton.setAlignmentX(Box.CENTER_ALIGNMENT);
eastPanelBox.add(calculateColumnButton);
eastPanel.add(eastPanelBox);
return eastPanel;
}
}