简体   繁体   English

JTable和JButton

[英]JTable and JButton

i am writing java desktop application. 我正在编写Java桌面应用程序。 i have some textareas to get data from user. 我有一些textareas可以从用户那里获取数据。 A JButton , which takes input and process that. 一个JButton ,它接受输入并对其进行处理。 And a JTable to populate data processed. 和一个JTable来填充处理过的数据。

here is my constructor em calling from main. 这是我的构造函数em从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. 在事件上,我试图通过删除其先前的值来更新整个jtable。 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. 其中data是2d数组,columnames是列。

But my JTable isnt getting updated. 但是我的JTable更新。

Where i am doing wrong, please give me some direction. 如果我做错了,请给我一些指导。

You don't want to create a new JTable since that has no effect on the currently displayed JTable. 您不想创建新的JTable,因为这对当前显示的JTable没有影响。 Instead you want to call setModel(...) on the current JTable and pass in the new table model. 相反,您想在当前JTable上调用setModel(...)并传入新的表模型。

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... 接下来,我们将继续使用布局管理器...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM