我有一个程序,目前实现了添加和删除表格列的功能,但有一个问题:当您向该表格中输入值时,它们开始在其他单元格中重复:
然后我按 Enter 键停止编辑单元格,不知何故,该单元格左侧的所有单元格都神奇地填充了相同的值:
如果我单击该单元格右侧的任何单元格,则原始单元格和单击单元格之间的所有单元格(包括它)都将填充相同的值:
如果我与按钮交互以沿着一行单元格移动,则所有单元格都会填充相同的值。
代码:
package ru.kirill.task9;
import java.awt.Color;
import java.awt.Component;
import java.awt.Container;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.DefaultCellEditor;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.table.DefaultTableCellRenderer;
import javax.swing.table.TableColumn;
import javax.swing.table.TableColumnModel;
import ru.kirill.addition.Addition;
public class GraphicApp {
private boolean isReverse = false;
class MainFrame extends JFrame {
private static final String TITLE = "Главное меню";
private static final String MINUS = "-";
private static final String PLUS = "+";
private static final String OPEN_BUTTON_TEXT = "Открыть";
private static final String SAVE_BUTTON_TEXT = "Сохранить";
private static final String REVERSE_BUTTON_TEXT = "Перевернуть";
private static final String ENTER_LABEL_TITLE = "Количество элементов";
private static final String CALCULATE_BUTTON_TEXT = "Рассчитать";
private static final String REFRESH_BUTTON_TEXT = "Обновить";
JButton minusButton;
JButton refreshButton;
JButton plusButton;
JButton openButton;
JButton saveButton;
JButton reverseButton;
JButton calculateButton;
JTable inputTable;
JTable outputTable;
JLabel titleOfEnterQuantityElements;
JTextField fieldOfEnterQuantityElements;
MainFrame() {
super(TITLE);
setDefaultCloseOperation(EXIT_ON_CLOSE);
createMainFrameLayout();
minusButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
int numberOfCells = Integer.parseInt(fieldOfEnterQuantityElements.getText());
if (numberOfCells > 1) {
fieldOfEnterQuantityElements.setText(
Integer.toString(
numberOfCells - 1));
}
}
});
refreshButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
int numberOfCells = 1;
try {
numberOfCells = Integer.parseInt(fieldOfEnterQuantityElements.getText());
} catch (NumberFormatException err) {
JOptionPane.showMessageDialog(null,
"Вы ввели неверное значение");
fieldOfEnterQuantityElements.setText("1");
}
int columnCount = inputTable.getColumnCount();
if (numberOfCells < 1) {
numberOfCells = 1;
fieldOfEnterQuantityElements.setText("1");
}
if (numberOfCells < columnCount) {
TableColumnModel inputTableModel = inputTable.getColumnModel();
for (int i = 0; i < columnCount - numberOfCells; i++) {
inputTable.removeColumn(inputTableModel.getColumn(columnCount - i - 1));
}
} else {
for (int i = 0; i < numberOfCells - columnCount; i++) {
TableColumn newColumn = new TableColumn();
newColumn.setCellRenderer(new CustomCellRenderer());
newColumn.setCellEditor(new CustomCellEditor(new JTextField()));
inputTable.addColumn(newColumn);
}
}
}
});
plusButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
fieldOfEnterQuantityElements.setText(
Integer.toString(
Integer.parseInt(fieldOfEnterQuantityElements.getText()) + 1));
}
});
pack();
setSize(600, 285);
setLocationRelativeTo(null);
setResizable(false);
setVisible(true);
calculateButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
for (int i = 0; i < inputTable.getColumnCount(); i++) {
System.out.print(inputTable.getValueAt(0, i));
}
}
});
}
private void createMainFrameLayout() {
Container pane = getContentPane();
pane.setLayout(new GridBagLayout());
GridBagConstraints layout = new GridBagConstraints();
JPanel topInteractionPanel = new JPanel(new GridBagLayout());
topInteractionPanel.setBackground(Color.GREEN);
layout.fill = GridBagConstraints.BOTH;
layout.weightx = 1;
layout.weighty = 0.15;
layout.gridx = 0;
layout.gridy = 0;
layout.gridwidth = 1;
layout.gridheight = 1;
pane.add(topInteractionPanel, layout);
minusButton = new JButton(MINUS);
layout.fill = GridBagConstraints.BOTH;
layout.weightx = 0.1;
layout.weighty = 1;
layout.gridx = 0;
layout.gridy = 0;
layout.gridwidth = 1;
layout.gridheight = 1;
topInteractionPanel.add(minusButton, layout);
JPanel enterQuantityElementsPanel = new JPanel(new GridBagLayout());
layout.weightx = 0.8;
layout.weighty = 1;
layout.gridx = 1;
layout.gridy = 0;
layout.gridwidth = 1;
layout.gridheight = 1;
topInteractionPanel.add(enterQuantityElementsPanel, layout);
titleOfEnterQuantityElements = new JLabel(ENTER_LABEL_TITLE);
layout.weightx = 1;
layout.weighty = 0.5;
layout.gridx = 0;
layout.gridy = 0;
layout.gridwidth = 1;
layout.gridheight = 1;
enterQuantityElementsPanel.add(titleOfEnterQuantityElements, layout);
JPanel enterTextFieldPanel = new JPanel(new GridBagLayout());
layout.weightx = 1;
layout.weighty = 0.5;
layout.gridx = 0;
layout.gridy = 1;
layout.gridwidth = 1;
layout.gridheight = 1;
enterQuantityElementsPanel.add(enterTextFieldPanel, layout);
fieldOfEnterQuantityElements = new JTextField();
fieldOfEnterQuantityElements.setText("1");
layout.weightx = 0.7;
layout.weighty = 1;
layout.gridx = 0;
layout.gridy = 0;
layout.gridwidth = 1;
layout.gridheight = 1;
enterTextFieldPanel.add(fieldOfEnterQuantityElements, layout);
refreshButton = new JButton(REFRESH_BUTTON_TEXT);
layout.weightx = 0.3;
layout.weighty = 1;
layout.gridx = 1;
layout.gridy = 0;
layout.gridwidth = 1;
layout.gridheight = 1;
enterTextFieldPanel.add(refreshButton, layout);
plusButton = new JButton(PLUS);
layout.weightx = 0.1;
layout.weighty = 1;
layout.gridx = 2;
layout.gridy = 0;
layout.gridwidth = 1;
layout.gridheight = 1;
topInteractionPanel.add(plusButton, layout);
JPanel inputTablePanel = new JPanel(new GridBagLayout());
inputTablePanel.setBackground(Color.BLUE);
layout.fill = GridBagConstraints.BOTH;
layout.weightx = 1;
layout.weighty = 0.30;
layout.gridx = 0;
layout.gridy = 1;
layout.gridwidth = 1;
layout.gridheight = 1;
pane.add(inputTablePanel, layout);
inputTable = new JTable(1, 1);
TableColumnModel inputTableModel = inputTable.getColumnModel();
TableColumn firstColumn = new TableColumn();
firstColumn.setCellRenderer(new CustomCellRenderer());
firstColumn.setCellEditor(new CustomCellEditor(new JTextField()));
inputTableModel.addColumn(firstColumn);
inputTableModel.removeColumn(inputTable.getColumnModel().getColumn(0));
inputTable.setTableHeader(null);
inputTable.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
inputTable.setRowHeight(50);
JScrollPane inputScrollPane = new JScrollPane(inputTable);
layout.fill = GridBagConstraints.BOTH;
layout.weightx = 1;
layout.weighty = 1;
layout.gridx = 0;
layout.gridy = 0;
layout.gridwidth = 1;
layout.gridheight = 1;
inputTablePanel.add(inputScrollPane, layout);
JPanel bottomInteractionPanel = new JPanel(new GridBagLayout());
bottomInteractionPanel.setBackground(Color.ORANGE);
layout.fill = GridBagConstraints.BOTH;
layout.weightx = 1;
layout.weighty = 0.15;
layout.gridx = 0;
layout.gridy = 2;
layout.gridwidth = 1;
layout.gridheight = 1;
pane.add(bottomInteractionPanel, layout);
openButton = new JButton(OPEN_BUTTON_TEXT);
layout.weightx = 0.25;
layout.weighty = 1;
layout.gridx = 0;
layout.gridy = 0;
layout.gridwidth = 1;
layout.gridheight = 1;
bottomInteractionPanel.add(openButton, layout);
saveButton = new JButton(SAVE_BUTTON_TEXT);
layout.weightx = 0.25;
layout.weighty = 1;
layout.gridx = 1;
layout.gridy = 0;
layout.gridwidth = 1;
layout.gridheight = 1;
bottomInteractionPanel.add(saveButton, layout);
reverseButton = new JButton(REVERSE_BUTTON_TEXT);
layout.weightx = 0.25;
layout.weighty = 1;
layout.gridx = 2;
layout.gridy = 0;
layout.gridwidth = 1;
layout.gridheight = 1;
bottomInteractionPanel.add(reverseButton, layout);
calculateButton = new JButton(CALCULATE_BUTTON_TEXT);
layout.weightx = 0.25;
layout.weighty = 1;
layout.gridx = 3;
layout.gridy = 0;
layout.gridwidth = 1;
layout.gridheight = 1;
bottomInteractionPanel.add(calculateButton, layout);
JPanel outputTablePanel = new JPanel(new GridBagLayout());
outputTablePanel.setBackground(Color.RED);
layout.fill = GridBagConstraints.BOTH;
layout.weightx = 1;
layout.weighty = 0.30;
layout.gridx = 0;
layout.gridy = 3;
layout.gridwidth = 1;
layout.gridheight = 1;
pane.add(outputTablePanel, layout);
outputTable = new JTable(1, 1);
outputTable.setTableHeader(null);
outputTable.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
outputTable.setRowHeight(50);
outputTable.setEnabled(false);
JScrollPane outputScrollPane = new JScrollPane(outputTable);
layout.fill = GridBagConstraints.BOTH;
layout.weightx = 1;
layout.weighty = 1;
layout.gridx = 0;
layout.gridy = 0;
layout.gridwidth = 1;
layout.gridheight = 1;
outputTablePanel.add(outputScrollPane, layout);
}
class CustomCellEditor extends DefaultCellEditor {
public CustomCellEditor(JTextField textField) {
super(textField);
this.clickCountToStart = 1;
}
}
class CustomCellRenderer extends DefaultTableCellRenderer {
public Component getTableCellRendererComponent(JTable table, Object value,
boolean isSelected, boolean hasFocus,
int row, int column) {
setFont(table.getFont());
setValue(value);
return this;
}
}
}
public GraphicApp() {
new MainFrame();
}
public GraphicApp(boolean isReverse) {
this.isReverse = isReverse;
new MainFrame();
}
public static void main(String[] args) {
new GraphicApp();
}
}
如何删除此自动完成功能以使一切正常?