使Java JTable行打开文本文件 [英] Make a Java JTable row open a text file

查看:101
本文介绍了使Java JTable行打开文本文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您知道在iTunes中双击歌曲时如何播放MP3文件.

You know how when you double click on a song in iTunes, a MP3 file plays.

使用JTable作为我的用户界面,如何将行链接到应用程序文件系统中的文本文件,以便当我双击行时,与该行关联的txt文件打开?

Using a JTable for my user interface, how do I link a row to a text file in the application file system so that when I double click on a row, a txt file associated with that row opens?

推荐答案

在单击任何行时,都需要输入文件名才能打开它,在下面的代码中,我将文件名存储在最后柱子.我在鼠标单击上使用相同的按钮在记事本中打开文件.

On click on any row you need to have the file name with you in order to open it, in the below code I have stored the file name in the last column. And I am using the same on mouse click to open the file in notepad.

代码段:

import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.io.IOException;
import java.awt.Desktop;
import javax.swing.JFrame;
import javax.swing.JTable;
import javax.swing.ListSelectionModel;
import javax.swing.table.AbstractTableModel;
import javax.swing.table.TableModel;

public class JTableTest {
JTable  myTable;

public JTableTest() {
    JFrame frame = new JFrame("Double Click on Table Test");
    final String[] columnNames = {"S.No. ", "File Name", "File Path", ""};
    final Object[][] tableData = {{"1", "test1.txt", "C://test1.txt", "C:/Test/test1.txt"},
            {"2", "test2.txt", "C://test2.txt", "C:/Test/test2.txt"}, {"3", "test2.txt", "C://test3.txt", "C:/Test/test3.txt"},};

    TableModel dataModel = new AbstractTableModel() {
        public int getColumnCount() {
            return columnNames.length;
        }

        public int getRowCount() {
            return tableData.length;
        }

        public Object getValueAt(int row, int col) {
            return tableData[row][col];
        }

        public String getColumnName(int column) {
            return columnNames[column];
        }

        public Class getColumnClass(int col) {
            return getValueAt(0, col).getClass();
        }

        public void setValueAt(Object aValue, int row, int column) {
            tableData[row][column] = aValue;
        }
    };

    myTable = new JTable(dataModel);
    myTable.getColumnModel().getColumn(3).setMaxWidth(0);
    myTable.getColumnModel().getColumn(3).setMinWidth(0);
    myTable.getColumnModel().getColumn(3).setPreferredWidth(0);
    myTable.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
    frame.getContentPane().add(myTable);
    frame.pack();
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setVisible(true);

    myTable.addMouseListener(new MouseAdapter() {
        public void mouseClicked(MouseEvent e) {
            if (e.getClickCount() == 2) {
                int selectedRow = myTable.getSelectedRow();
                try {
                    Desktop.getDesktop().open(new File((String) myTable.getValueAt(selectedRow, 3)));
                } catch (IOException e1) {
                    e1.printStackTrace();
                    }
                }
            }
        });
    }

    public static void main(String[] args) {
        new JTableTest();
    }
}

希望这可以帮助您

这篇关于使Java JTable行打开文本文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆