Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/357.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 在JTable中显示数据_Java_Swing_Jtable - Fatal编程技术网

Java 在JTable中显示数据

Java 在JTable中显示数据,java,swing,jtable,Java,Swing,Jtable,我想将一些数据添加到JTable并写入某个文件。数据未添加到表中,但已正确写入文件 下面是我所指的一段代码: import java.awt.BorderLayout; import java.awt.FlowLayout; import java.awt.GridLayout; import java.io.BufferedReader; import java.io.FileReader; import java.io.FileWriter; import java.io.IOExcepti

我想将一些数据添加到
JTable
并写入某个文件。数据未添加到表中,但已正确写入文件

下面是我所指的一段代码:

import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Vector;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.table.DefaultTableModel;

  public class Layout extends JFrame {
        DefaultTableModel dtm;

        public Layout() {

        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        p1 = new JPanel(new BorderLayout());
        p2 = new JPanel(new BorderLayout());
        p3 = new JPanel(new GridLayout(4, 2));
        p4 = new JPanel(new FlowLayout());
        p5 = new JPanel(new BorderLayout());

        l1 = new JLabel("Item");
        l2 = new JLabel("Price");
        l3 = new JLabel("Quantity");
        l4 = new JLabel("ReorderLevel");
        tf1 = new JTextField();
        tf2 = new JTextField();
        tf3 = new JTextField();
        tf4 = new JTextField();
        p3.add(l1);
        p3.add(tf1);
        p3.add(l2);
        p3.add(tf2);
        p3.add(l3);
        p3.add(tf3);
        p3.add(l4);
        p3.add(tf4);

        b1 = new JButton("Add");
        b2 = new JButton("Update");
        b3 = new JButton("Delete");
        b4 = new JButton("List");
        p4.add(b1);
        p4.add(b2);
        p4.add(b3);
        p4.add(b4);

        t = new JTable();
        t.setModel(new javax.swing.table.DefaultTableModel(
                new Object[][]{
                    //{null, null, null, null},
                    //{null, null, null, null},
                    //{null, null, null, null}
                },
                new String[]{
                    "Item", "Price", "Quantity", "Reorder"
                }
        ));
        sc = new JScrollPane(t);

        p5.add(sc, BorderLayout.CENTER);
        p2.add(p3, BorderLayout.NORTH);
        p2.add(p4, BorderLayout.SOUTH);
        p1.add(p2, BorderLayout.NORTH);
        p1.add(p5, BorderLayout.CENTER);
        add(p1);

        pack();

        b1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnaddActionPerformed(evt);
            }
        }
        );   
    }                      `add button action`
        private void btnaddActionPerformed(java.awt.event.ActionEvent evt) 
        {
               String item = tf1.getText();
               String price = tf2.getText();
               String quantity=tf3.getText();
               String reorder=tf4.getText();


               try {


               String filename= "C:\\WINNT\\PROFILES\\palanisamyp\\workspace\\InventoryDBInfile\\Myfile.txt";


                       FileWriter myfile = new FileWriter(filename,true);

                        PrintWriter outStream = new PrintWriter(myfile);
                        outStream.print(item);
                        outStream.print("|");
                        outStream.print(price);
                        outStream.print("|");
                        outStream.print(quantity);
                        outStream.print("|");
                        outStream.print(reorder);
                        outStream.print("\r\n");
                        outStream.close();

                            tf1.setText("");
                            tf2.setText("");
                            tf3.setText("");
                            tf4.setText("");
                            myfile.close(); 

              readFile();



               } catch (Exception e) {
        e.printStackTrace();}


        }

        private void readFile()
        {
                BufferedReader br = null;

          try {

          String sCurrentLine;

          br = new BufferedReader(new FileReader("C:\\WINNT\\PROFILES\\palanisamyp\\workspace\\InventoryDBInfile\\Myfile.txt"));

          if (dtm.getRowCount() > 0) {
              for (int i = dtm.getRowCount() - 1; i > -1; i--) {
                  dtm.removeRow(i);
              }
          }
          while ((sCurrentLine = br.readLine()) != null) {

          String item1=sCurrentLine.toString();

          String regexp = "[|]+";

          String[] lines = item1.split(regexp);

          Vector v = new Vector();  

                 String item2;
                 String price2;
                 String quantity2;
                 String reorder2;

                 item2=lines[0].toString();
                 price2=lines[1].toString();
                 quantity2=lines[2].toString();
                 reorder2=lines[3].toString();


                 v.add(item2);
                 v.add(price2);
                 v.add(quantity2);
                 v.add(reorder2);

                 dtm.toString();
                 dtm.addRow(v); 


          }

          } catch (IOException e) {
          e.printStackTrace();
          } finally {
          try {
          if (br != null)br.close();
          } catch (IOException ex) {
          ex.printStackTrace();
          }
          }
        }


    public static void main(String[] args) {
        new Layout().setVisible(true);
    }

    private JPanel p1, p2, p3, p4, p5;
    private JLabel l1, l2, l3, l4;
    private JTextField tf1, tf2, tf3, tf4;
    private JButton b1, b2, b3, b4;
    private JScrollPane sc;
    private JTable t;
}

请查看如何在Java表中使用模型、渲染器和编辑器。

因为您将表的日期模型分配给了新的DefaultTableModel

 t.setModel(new javax.swing.table.DefaultTableModel(
            new Object[][]{
                //{null, null, null, null},
                //{null, null, null, null},
                //{null, null, null, null}
            },
            new String[]{
                "Item", "Price", "Quantity", "Reorder"
            }
    ));
而不是使用您的字段
dtm

DefaultTableModel dtm;
试试这个:

 dtm = new javax.swing.table.DefaultTableModel(
                new Object[][]{
                    //{null, null, null, null},
                    //{null, null, null, null},
                    //{null, null, null, null}
                },
                new String[]{
                    "Item", "Price", "Quantity", "Reorder"
                }
        ));
  t.setModel(dtm);
可能重复的