英文:
Java-How to save JTable cells in my text variable
问题
public class TableSave extends JFrame {
private JPanel contentPane;
private JTable table;
private String text;
public TableSave() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 450, 300);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
contentPane.setLayout(null);
Object[][] data = {
{"NAME", ""},
{"SURNAME", ""},
{"CITY", ""},
};
String[] column = {"DESCRIPTION", "DATE"};
table = new JTable(data, column);
contentPane.add(table);
table.setBounds(10, 11, 368, 146);
table.getModel().addTableModelListener(new TableModelListener() {
public void tableChanged(TableModelEvent e) {
TableModel model = table.getModel();
Object data = model.getValueAt(0, 1);
text = (String) data;
}
});
JButton save = new JButton("SAVE");
save.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent evt) {
text = (String) table.getValueAt(0, 1);
System.out.println(text);
System.exit(0);
}
});
save.setBounds(96, 190, 89, 23);
add(save);
}
}
英文:
I have a JTable in my JFrame and i want to know how to save the edited cells in my text variable when I
click save button. I edit cell only when i press enter + clik SAVE
Why?????? This is my code:
public class TableSave extends JFrame {
private JPanel contentPane;
private JTable table;
private String text;
public TableSave() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 450, 300);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
contentPane.setLayout(null);
Object [][] date =
{
{"NAME ", ""},
{"SURNAME ", ""},
{"CITY", ""},
};
String [] column = {"DESCRIPTION","DATE"};
table = new JTable(date,column);// my jtable
contentPane.add(table);
table.setBounds(10, 11, 368, 146);
table.getModel().addTableModelListener(new TableModelListener() {
public void tableChanged(TableModelEvent e) {
TableModel model = table.getModel(); // new model
Object data = model.getValueAt(0, 1);
text = (String) data;
}
});
JButton save = new JButton("SAVE"); // my button save, in i do not press enter don't save the date
save.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent evt) {
text = (String) evt.toString();
text = (String) table.getValueAt(0, 1);
System.out.println(text);
System.exit(0);}
});
save.setBounds(96, 190, 89, 23);
add(save);
}}
can you help me please
答案1
得分: 1
你在使用文本变量的方式上感到困惑。你在不同的地方将其设置为各种不同的内容,但只在保存按钮的点击处理程序中使用了其值,而在那里你也进行了退出操作。
我之前已经说过了,但在构建GUI应用程序时正确的起点是先获得经过测试和正常工作的模型(参考模型-视图-控制器),也就是创建一个TableModel
,该模型可以被序列化为文本(以任何你想要的方式进行),并且首先通过编程对模型进行彻底的测试。一旦模型正常工作,添加GUI就很简单了:只需将监听器连接到现有功能即可。
英文:
You are confused in how you are using your text variable. You are setting this to all different things in different places, but only using the value in your save button click handler, where you are also exiting.
I've said this before, but the correct place to start in building a GUI app is to get the model (search Model-View-Controller) tested and working first. That means creating a TableModel
that can be serialized to text (however you want to do that) and testing the model thoroughly through programming first. Once the model is working, adding the GUI is simple: Just hook your listeners up to existing functionality.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论