Saghir A. Khatri
Saghir A. Khatri

Reputation: 3128

JTable and JButton

i am writing java desktop application. i have some textareas to get data from user. A JButton, which takes input and process that. And a JTable to populate data processed.

here is my constructor em calling from main.

    public ChangeButtonLabel()
    {
        JFrame f=new JFrame();
            f.setLayout(null);

        JLabel lab1=new JLabel("Name");
            JLabel lab2=new JLabel("Age");

        //table=new JTable(model);

        text1=new JTextField(20);
            text2=new JTextField(20);


        button=new JButton("OK");

            lab1.setBounds(10,10,100,20);
            text1.setBounds(120,10,100,20);

            lab2.setBounds(10,40,100,20);
            text2.setBounds(120,40,100,20);

            Table=new ArrayList();
        Table.add(new ArrayList());
        ((ArrayList)Table.get(0)).add("\nProgram Name   ");
        ((ArrayList)Table.get(0)).add("Count   ");
        ((ArrayList)Table.get(0)).add("Elapsed Time   ");
        ((ArrayList)Table.get(0)).add("Average ET\n");

        button.setBounds(120, 100, 100, 20);        

        button.addActionListener(new MyAction());

            Object[][] data = {
                {"Program Name","count","ET","Avg ET"}
            };


        model = new DefaultTableModel(data,columnNames);

            dataTable = new JTable(model);

            dataTable.setPreferredScrollableViewportSize(new Dimension(500, 70));
            dataTable.setFillsViewportHeight(true);

        dataTable.setBounds(220,130,300,200);
            f.add(lab1);
            f.add(text1);
            f.add(lab2);
            f.add(text2);

        f.add(dataTable);

        f.add(button);

            f.setVisible(true);
            f.setSize(300,350);
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);   
    }
}

on event i am trying to update whole jtable, by deleting its previous values. here is the code

model = new DefaultTableModel(data,columnNames);
dataTable.repaint();
dataTable=new JTable(model);

    dataTable.setPreferredScrollableViewportSize(new Dimension(500, 70));
    dataTable.setFillsViewportHeight(true);

dataTable.setBounds(220,130,300,200);

model.fireTableDataChanged();

Where data is 2d array, and columnames are columns.

But my JTable isnt getting updated.

Where i am doing wrong, please give me some direction.

Upvotes: 0

Views: 437

Answers (1)

Hovercraft Full Of Eels
Hovercraft Full Of Eels

Reputation: 285405

You don't want to create a new JTable since that has no effect on the currently displayed JTable. Instead you want to call setModel(...) on the current JTable and pass in the new table model.

Also, please work on your code formatting since to be blunt, it stinks. Since you're asking volunteers to put in effort to help you, I don't think it's asking too much for you to put in similar effort to not make it so difficult to read your code.

Just as an example, check to see which is easier to read, your code above, or this code:

public class ChangeButtonLabel {
   private JTextField text1;
   private JButton button;
   private ArrayList Table;
   private JTextField text2;
   private Object[] columnNames;
   private DefaultTableModel model;
   private JTable dataTable;

   public ChangeButtonLabel() {
      JFrame f = new JFrame();
      f.setLayout(null);
      JLabel lab1 = new JLabel("Name");
      JLabel lab2 = new JLabel("Age");
      // table=new JTable(model);

      text1 = new JTextField(20);
      text2 = new JTextField(20);
      button = new JButton("OK");
      lab1.setBounds(10, 10, 100, 20);
      text1.setBounds(120, 10, 100, 20);
      lab2.setBounds(10, 40, 100, 20);
      text2.setBounds(120, 40, 100, 20);

      Table = new ArrayList();
      Table.add(new ArrayList());
      ((ArrayList) Table.get(0)).add("\nProgram Name   ");
      ((ArrayList) Table.get(0)).add("Count   ");
      ((ArrayList) Table.get(0)).add("Elapsed Time   ");
      ((ArrayList) Table.get(0)).add("Average ET\n");

      button.setBounds(120, 100, 100, 20);
      button.addActionListener(new MyAction());

      Object[][] data = { { "Program Name", "count", "ET", "Avg ET" } };
      model = new DefaultTableModel(data, columnNames);
      dataTable = new JTable(model);
      dataTable.setPreferredScrollableViewportSize(new Dimension(500, 70));
      dataTable.setFillsViewportHeight(true);
      dataTable.setBounds(220, 130, 300, 200);

      f.add(lab1);
      f.add(text1);
      f.add(lab2);
      f.add(text2);
      f.add(dataTable);
      f.add(button);
      f.setVisible(true);
      f.setSize(300, 350);
      f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
   }
}

Next we'll work on use of layout managers...

Upvotes: 6

Related Questions