Read a file in a jtable

不打扰是莪最后的温柔 提交于 2019-12-13 04:47:12

问题


I have a code like this :

private void jTable4MouseClicked(java.awt.event.MouseEvent evt) {                                     
    if (evt.getClickCount() == 1) {
        System.out.println("clicked");
        int row = jTable4.getSelectedRow();
        if (row != -1) {
            String firstColumnValue = jTable4.getModel().getValueAt(row, 0).toString();
            String secondColumnValue = jTable4.getModel().getValueAt(row, 1).toString();
            jTextAreaMainFileHighlight.setText(firstColumnValue); // just show name of a file
            jTextAreaComparingFileHighlighter.setText(secondColumnValue); // just show name of a file

        }

You know, the jtabel is contains a name of file. How to read that file to and then show in jTextArea


回答1:


This is a basic of java file reading .Anyway to read the a file (name given in jtable) and to display the content of the file you can use following

    BufferedReader br = null;

            try {

                String str;

                br = new BufferedReader(new FileReader(firstColumnValue));

                while ((str = br.readLine()) != null) {
                    System.out.println(str);
                   jTextAreaMainFileHighlight.setText(str);
                }

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


来源:https://stackoverflow.com/questions/25119034/read-a-file-in-a-jtable

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!