更改一个 JTable 会更改 java 中的其他 JTable
Changing one JTable changes other JTable in java
我正在制作一个需要很多 JTable 的程序。因此,对于设置字体、列大小等,我使用的是这种方法:
public static void Enhance(JTable t){
t.setRowHeight(70);
DefaultTableCellRenderer centerRenderer = new DefaultTableCellRenderer();
centerRenderer.setHorizontalAlignment(JLabel.CENTER);
t.setDefaultRenderer(String.class, centerRenderer);
t.setAlignmentX(JTable.CENTER_ALIGNMENT);
t.getModel().addTableModelListener(new TableModelListener() {
@Override
public void tableChanged(TableModelEvent e) {
resizeColumnWidth(t);
}
});
int index = 0;
t.setAutoCreateColumnsFromModel(true);
while (index < t.getColumnModel().getColumnCount()) {
TableColumn a = t.getColumnModel().getColumn(index);
a.setMinWidth(130);
a.setPreferredWidth(130);
index++;
}
JTextField textField = new JTextField();
textField.setFont(new Font("Open Sans", Font.PLAIN, 27));
textField.setBorder(new LineBorder(Color.BLACK));
DefaultCellEditor dce = new DefaultCellEditor( textField );
t.getColumnModel().getColumn(0).setCellEditor(dce);
for(int i =1 ; i<t.getColumnCount();i++){
TableColumn sportColumn = t.getColumnModel().getColumn(i);
JComboBox comboBox = new JComboBox();
comboBox.setFont(new Font("Open Sans", Font.PLAIN,25));
comboBox.addItem("Hulk- Maths");
comboBox.addItem("THor - Computer");
comboBox.addItem("Iron M- Geo");
comboBox.addItem("Black P- History");
comboBox.addItem("War M- Science");
comboBox.addItem("Java - Easy");
comboBox.addItem("Custom");
sportColumn.setCellEditor(new DefaultCellEditor(comboBox));
comboBox.addItemListener(new ItemListener() {
@Override
public void itemStateChanged(ItemEvent e) {
Object selectedItem = comboBox.getSelectedItem();
boolean editable = selectedItem instanceof String && ((String)selectedItem).equals("Custom");
comboBox.setEditable(editable);
}
});
}
t.setShowGrid(true);
t.setFont(new Font("Open Sans",Font.PLAIN,25));
t.getTableHeader().setFont(new Font("Open Sans", Font.PLAIN, 27));
t.setAutoResizeMode( JTable.AUTO_RESIZE_OFF );
t.setBackground(Color.WHITE);
t.setSelectionBackground(new Color(172, 208, 252));
t.setRowHeight(50);
t.setSelectionForeground(Color.black);
t.getTableHeader().setBackground(new Color(23, 180, 252));
t.setGridColor(Color.lightGray);
t.getTableHeader().setForeground(Color.white);
}
我是这样使用这个方法的:-
JTable t1 = new JTable(data1, title);//data is a String[][] Object and title is a String[] Object.
Enhance(t1);
这样我还有4张桌子。
但问题是当我对 t1
进行编辑时,JTable t2
也在发生变化。
如果我输入
"Java"
在t1
的第一个单元格中,t2
的第一个单元格也将是
"Java"
如何解决这个问题,使对 t1
所做的编辑在 t2
中不可见?
编辑(调试细节):
即使这个代码也不起作用
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTable;
/*
* Made By JFan
*/
/**
*
* @author JFan
*/
public class Main {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
// TODO code application logic here
JFrame f = new JFrame();
f.setSize(1000,1000);
f.setLayout(null);
String[][] data = {
{"","","","",""},
{"","","","",""},
{"","","","",""},
{"","","","",""},{"","","","",""}
};
String[] t = {
"1","2","3","4","5"
};
JTable t1 = new JTable(data,t);
JScrollPane j = new JScrollPane(t1);
j.setBounds(0, 200, 500, 500);
f.add(j);
JTable t2 = new JTable(data,t);
f.add(j);
JButton b = new JButton("Click");
b.setBounds(0,0,100,100);
b.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
j.setViewportView(t2);
}
});
f.add(b);
f.setVisible(true);
}
}
不要使用:
JTable t2 = new JTable(data,t);
创建 JTable。这将创建一个使用数组作为数据存储的简单 TableModel。
如您所见,此数组由所有 table 共享。
改为使用:
DefaultTableModel model = new DefaultTableModel(data, t);
JTable t2 = new JTable( model );
现在数组中的数据将被复制到 DefaultTableModel。
因此每个 table 都有自己的数据副本。
我正在制作一个需要很多 JTable 的程序。因此,对于设置字体、列大小等,我使用的是这种方法:
public static void Enhance(JTable t){
t.setRowHeight(70);
DefaultTableCellRenderer centerRenderer = new DefaultTableCellRenderer();
centerRenderer.setHorizontalAlignment(JLabel.CENTER);
t.setDefaultRenderer(String.class, centerRenderer);
t.setAlignmentX(JTable.CENTER_ALIGNMENT);
t.getModel().addTableModelListener(new TableModelListener() {
@Override
public void tableChanged(TableModelEvent e) {
resizeColumnWidth(t);
}
});
int index = 0;
t.setAutoCreateColumnsFromModel(true);
while (index < t.getColumnModel().getColumnCount()) {
TableColumn a = t.getColumnModel().getColumn(index);
a.setMinWidth(130);
a.setPreferredWidth(130);
index++;
}
JTextField textField = new JTextField();
textField.setFont(new Font("Open Sans", Font.PLAIN, 27));
textField.setBorder(new LineBorder(Color.BLACK));
DefaultCellEditor dce = new DefaultCellEditor( textField );
t.getColumnModel().getColumn(0).setCellEditor(dce);
for(int i =1 ; i<t.getColumnCount();i++){
TableColumn sportColumn = t.getColumnModel().getColumn(i);
JComboBox comboBox = new JComboBox();
comboBox.setFont(new Font("Open Sans", Font.PLAIN,25));
comboBox.addItem("Hulk- Maths");
comboBox.addItem("THor - Computer");
comboBox.addItem("Iron M- Geo");
comboBox.addItem("Black P- History");
comboBox.addItem("War M- Science");
comboBox.addItem("Java - Easy");
comboBox.addItem("Custom");
sportColumn.setCellEditor(new DefaultCellEditor(comboBox));
comboBox.addItemListener(new ItemListener() {
@Override
public void itemStateChanged(ItemEvent e) {
Object selectedItem = comboBox.getSelectedItem();
boolean editable = selectedItem instanceof String && ((String)selectedItem).equals("Custom");
comboBox.setEditable(editable);
}
});
}
t.setShowGrid(true);
t.setFont(new Font("Open Sans",Font.PLAIN,25));
t.getTableHeader().setFont(new Font("Open Sans", Font.PLAIN, 27));
t.setAutoResizeMode( JTable.AUTO_RESIZE_OFF );
t.setBackground(Color.WHITE);
t.setSelectionBackground(new Color(172, 208, 252));
t.setRowHeight(50);
t.setSelectionForeground(Color.black);
t.getTableHeader().setBackground(new Color(23, 180, 252));
t.setGridColor(Color.lightGray);
t.getTableHeader().setForeground(Color.white);
}
我是这样使用这个方法的:-
JTable t1 = new JTable(data1, title);//data is a String[][] Object and title is a String[] Object.
Enhance(t1);
这样我还有4张桌子。
但问题是当我对 t1
进行编辑时,JTable t2
也在发生变化。
如果我输入
"Java"
在t1
的第一个单元格中,t2
的第一个单元格也将是
"Java"
如何解决这个问题,使对 t1
所做的编辑在 t2
中不可见?
编辑(调试细节):
即使这个代码也不起作用
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTable;
/*
* Made By JFan
*/
/**
*
* @author JFan
*/
public class Main {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
// TODO code application logic here
JFrame f = new JFrame();
f.setSize(1000,1000);
f.setLayout(null);
String[][] data = {
{"","","","",""},
{"","","","",""},
{"","","","",""},
{"","","","",""},{"","","","",""}
};
String[] t = {
"1","2","3","4","5"
};
JTable t1 = new JTable(data,t);
JScrollPane j = new JScrollPane(t1);
j.setBounds(0, 200, 500, 500);
f.add(j);
JTable t2 = new JTable(data,t);
f.add(j);
JButton b = new JButton("Click");
b.setBounds(0,0,100,100);
b.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
j.setViewportView(t2);
}
});
f.add(b);
f.setVisible(true);
}
}
不要使用:
JTable t2 = new JTable(data,t);
创建 JTable。这将创建一个使用数组作为数据存储的简单 TableModel。
如您所见,此数组由所有 table 共享。
改为使用:
DefaultTableModel model = new DefaultTableModel(data, t);
JTable t2 = new JTable( model );
现在数组中的数据将被复制到 DefaultTableModel。
因此每个 table 都有自己的数据副本。