【问题标题】:Retrieving content of Textarea within a JTabbedpane在 JTabbedpane 中检索 Textarea 的内容
【发布时间】:2012-12-26 07:45:31
【问题描述】:

我正在编写一个使用 JTabbedpane 的 swing 应用程序。 可以在选项卡中打开日志文件并根据需要将其“解析”到另一个选项卡。 将解析的内容保存到文本文件时遇到问题。 每个选项卡都由 JPanel 中的一个 JTextarea 组成。

<pre>
 private void setContentsOfParsedLogFile(JPanel content) {
    JTextArea textArea = new JTextArea();
    addContentsToTextArea(textArea);
    content.add(new JScrollPane(textArea));
    content.setLayout(new GridLayout(1, 1));
    pane.addTab(parsedTabName(), content);
  }

  private void addContentsToTextArea(JTextArea textArea) {
    if (!parsedFileAlreadyOpen()) {
      PortLogParser lp = new PortLogParser();
      lp.parseLogFile(new File(activeTabFullName));
      ArrayList<String> sb = lp.getParsedMsg();
      for (String s : sb) {
        textArea.append(s + System.getProperty("line.separator"));
      }
    }
  }
</pre>

希望我可以使用:

字符串文本 = ((JTextArea) pane.getSelectedComponent()).getText();

在actionPerformed内:

<pre>
  private void createSaveFileMenuItem() {
    saveLogFile = new JMenuItem("Save Log File");
    saveLogFile.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_S, InputEvent.ALT_MASK));
    saveLogFile.addActionListener(new ActionListener() {

      @Override
      public void actionPerformed(ActionEvent e) {
        saveContentOfActiveTab();
      }
    });
  }

  private void saveContentOfActiveTab() {
    JFileChooser fc = new JFileChooser();
    int returnVal = fc.showSaveDialog(this);
    if (returnVal == JFileChooser.APPROVE_OPTION) {
      File file = fc.getSelectedFile();
      try {
        saveContentsToFile(file);
      } catch (IOException e1) {
        e1.printStackTrace();
      }
    }
  }

  private void saveContentsToFile(File file) throws IOException {
    createNewFileIfItDoesntExist(file);
    BufferedWriter bw = new BufferedWriter(new FileWriter(file.getAbsoluteFile()));
    //System.out.println(pane.getSelectedIndex());
    //System.out.println(pane.getSelectedComponent());
    /*
    TODO
    the component is the container JPanel. How do I select the textarea and it's content??
    */
    //    String text = ((JTextArea) pane.getSelectedComponent()).getText();
    //    bw.write(text);
    bw.close();
  }

  private void createNewFileIfItDoesntExist(File file) throws IOException {
    if (!file.exists()) {
      file.createNewFile();
    }
  }
</pre>

但选定的组件是 JPanel。 有没有办法在 JPanel 中包含的 textarea 中选择文本? 我没有唯一标识每个文本区域,所以这可能是个问题?

【问题讨论】:

    标签: java swing jtextarea jtabbedpane


    【解决方案1】:

    一种方法是创建一个JPanel 的子类和一个关联的JTextArea

    class LogTextPanel extends JPanel {
    
        private final JTextArea textArea;
    
        public LogTextPanel() {
            super(new GridLayout(1, 1));
    
            textArea = new JTextArea();
            add(new JScrollPane(textArea));
        }
    
        public void append(String text) {
            textArea.append(text);
        }
    
        public String getText() {
            return textArea.getText();
        }
    }
    

    然后您可以使用以下方法从所选面板中检索文本:

    String text = ((LogTextPanel)pane.getSelectedComponent()).getText();
    

    【讨论】:

    • 感谢雷默斯。一直在姻亲如此缓慢地回复你。会试一试,看看我的进展如何。
    猜你喜欢
    • 1970-01-01
    • 2023-03-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-04-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多