将方法移动到SwingWorker中(Moving a method into SwingWorker)

编程入门 行业动态 更新时间:2024-10-26 16:26:07
方法移动到SwingWorker中(Moving a method into SwingWorker)

我正在研究一种分析DNA的程序。 该程序的主要工作是采用某种文件类型的方法,解析文件并将其转换为字符串,然后解析字符串以将其分成段。 然后,它在新JFrame上的JTable返回分析结果。

public JTable readFile(File file) throws IOException{ BufferedReader in = new BufferedReader(new FileReader(file)); String line; String result = ""; while((line=in.readLine())!=null) { if(!line.startsWith(">")) { result = result + line; } } Sequence sequence = new Sequence("Unspecified", result); ArrayList<Contig> contigs = sequence.getReadingFrames(); String [] columnNames = {"Position", "Frame", "Sequence"}; Object[][] data = new Object[contigs.size()][3]; int j = 0; for (int i = 0; i <= contigs.size() && j < 3; i++){ if (i == contigs.size()){ j++; i = -1; } else if (j == 0){ data[i][j] = Integer.toString(contigs.get(i).getStartPosition()); } else if (j == 1){ data[i][j] = Integer.toString(contigs.get(i).getReadingFrame()); } else if (j == 2){ data[i][j] = contigs.get(i).getSequence(); } } JTable seqTable = new JTable(data, columnNames); in.close(); return seqTable; }

我应该如何将其SwingWorker到SwingWorker ? 我尝试了复制,粘贴和编辑,但整个过程只返回空结果。

I am working on a program that analyzes DNA. The main work of the program is in a method that takes in a certain file type, parses the file and converts it into a string, then parses the string to separate it into segments. It then returns the results of the analysis in a JTable on a new JFrame.

public JTable readFile(File file) throws IOException{ BufferedReader in = new BufferedReader(new FileReader(file)); String line; String result = ""; while((line=in.readLine())!=null) { if(!line.startsWith(">")) { result = result + line; } } Sequence sequence = new Sequence("Unspecified", result); ArrayList<Contig> contigs = sequence.getReadingFrames(); String [] columnNames = {"Position", "Frame", "Sequence"}; Object[][] data = new Object[contigs.size()][3]; int j = 0; for (int i = 0; i <= contigs.size() && j < 3; i++){ if (i == contigs.size()){ j++; i = -1; } else if (j == 0){ data[i][j] = Integer.toString(contigs.get(i).getStartPosition()); } else if (j == 1){ data[i][j] = Integer.toString(contigs.get(i).getReadingFrame()); } else if (j == 2){ data[i][j] = contigs.get(i).getSequence(); } } JTable seqTable = new JTable(data, columnNames); in.close(); return seqTable; }

How should I go about moving this into a SwingWorker? I tried a copy, paste, and edit, but the whole thing just returns empty results.

最满意答案

尝试这个。 由于缺少依赖性,未对源进行测试。 您只需要找到更新JTable 。 我建议使用TableModel的实例创建CustomSwingWorker ,然后在方法中更新它: protected void done()

import java.io.BufferedReader; import java.io.File; import java.io.FileReader; import java.util.ArrayList; import java.util.concurrent.ExecutionException; import javax.sound.midi.Sequence; import javax.swing.JTable; import javax.swing.SwingWorker; import javax.swing.table.DefaultTableModel; import javax.swing.table.TableModel; public class CustomSwingWorker extends SwingWorker<TableModel, Void> { private File file; private JTable table; public CustomSwingWorker(final JTable table, final File file) { this.table = table; this.file = file; } @Override protected TableModel doInBackground() throws Exception { BufferedReader in = new BufferedReader(new FileReader(file)); String line; String result = ""; while ((line = in.readLine()) != null) { if (!line.startsWith(">")) { result = result + line; } } Sequence sequence = new Sequence("Unspecified", result); ArrayList<Contig> contigs = sequence.getReadingFrames(); String [] columnNames = {"Position", "Frame", "Sequence"}; Object[][] data = new Object[contigs.size()][3]; int j = 0; for (int i = 0; i <= contigs.size() && j < 3; i++) { if (i == contigs.size()) { j++; i = -1; } else if (j == 0) { data[i][j] = Integer .toString(contigs.get(i).getStartPosition()); } else if (j == 1) { data[i][j] = Integer.toString(contigs.get(i).getReadingFrame()); } else if (j == 2) { data[i][j] = contigs.get(i).getSequence(); } } in.close(); return new DefaultTableModel(data, columnNames); } protected void done() { try { this.table.setModel(this.get()); } catch (Exception ignore) { // Simply ignore it ... } } }

Try this. The source isn't tested due to missing dependencies. You just need to find a way to update your JTable. I suggest to create the CustomSwingWorker with an instance of your TableModel and then just update it within the method: protected void done()

import java.io.BufferedReader; import java.io.File; import java.io.FileReader; import java.util.ArrayList; import java.util.concurrent.ExecutionException; import javax.sound.midi.Sequence; import javax.swing.JTable; import javax.swing.SwingWorker; import javax.swing.table.DefaultTableModel; import javax.swing.table.TableModel; public class CustomSwingWorker extends SwingWorker<TableModel, Void> { private File file; private JTable table; public CustomSwingWorker(final JTable table, final File file) { this.table = table; this.file = file; } @Override protected TableModel doInBackground() throws Exception { BufferedReader in = new BufferedReader(new FileReader(file)); String line; String result = ""; while ((line = in.readLine()) != null) { if (!line.startsWith(">")) { result = result + line; } } Sequence sequence = new Sequence("Unspecified", result); ArrayList<Contig> contigs = sequence.getReadingFrames(); String [] columnNames = {"Position", "Frame", "Sequence"}; Object[][] data = new Object[contigs.size()][3]; int j = 0; for (int i = 0; i <= contigs.size() && j < 3; i++) { if (i == contigs.size()) { j++; i = -1; } else if (j == 0) { data[i][j] = Integer .toString(contigs.get(i).getStartPosition()); } else if (j == 1) { data[i][j] = Integer.toString(contigs.get(i).getReadingFrame()); } else if (j == 2) { data[i][j] = contigs.get(i).getSequence(); } } in.close(); return new DefaultTableModel(data, columnNames); } protected void done() { try { this.table.setModel(this.get()); } catch (Exception ignore) { // Simply ignore it ... } } }

更多推荐

本文发布于:2023-08-05 05:24:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1427973.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:方法   SwingWorker   method   Moving

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!