met8657
met8657

Reputation: 1

Java-How to save JTable cells in my text variable

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

Upvotes: 0

Views: 167

Answers (1)

ControlAltDel
ControlAltDel

Reputation: 35011

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.

Upvotes: 1

Related Questions