确定在jScrollPane中选择的行方法

huangapple go评论163阅读模式
英文:

How to determine row selected within jScrollPane

问题

我正在填充一个包含文件列表的jScrollPane(在JFrame Swing GUI中)。有没有办法确定用户在jScrollPane中点击了哪一行。我想要识别行,并提取用户选择的那一行的内容。

英文:

I am populating a jScrollPane (within a JFrame Swing GUI) with a list of files. Is there a way to determine which row the user has clicked on within jScrollPane. I want to identify the row and extract the contents of that row that the user selected.

答案1

得分: 1

你最好使用具有内置选择功能的JList或JTable。这里是JTable的Oracle文档基础部分

我建议在初学时使用JTableDefaultTableModel

英文:

You would be better off using a JList or JTable which have built in selection functionality. Here is the Oracle documentation on the basics of JTable.

I suggest using JTable with the DefaultTableModel when you are first getting started.

答案2

得分: 0

你可以使用一个名为 process 的方法来打开文件或执行操作。

using System.Diagnostics;
Process p = new Process();
ProcessStartInfo pi = new ProcessStartInfo();
pi.UseShellExecute = true;
pi.FileName = @"MY_FILE_WITH_FULL_PATH.jpg"; //c:\Desktop\example\sample.txt
p.StartInfo = pi;

try
{
    p.Start();
}
catch (Exception Ex)
{
    //MessageBox.Show(Ex.Message);
}
英文:

Well you can open a file or execute using a method called process

using System.Diagnostics;
Process p = new Process();
ProcessStartInfo pi = new ProcessStartInfo();
pi.UseShellExecute = true;
pi.FileName = @"MY_FILE_WITH_FULL_PATH.jpg";//c:\Desktop\example\sample.txt
p.StartInfo = pi;

try
{
    p.Start();
}
catch (Exception Ex)
{
    //MessageBox.Show(Ex.Message);
}

huangapple
  • 本文由 发表于 2020年8月13日 06:02:49
  • 转载请务必保留本文链接:https://go.coder-hub.com/63385292.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定