单击JFrame中的“搜索"按钮,应打开一个搜索栏 [英] Clicking on the Search button in JFrame should open a search bar

查看:164
本文介绍了单击JFrame中的“搜索"按钮,应打开一个搜索栏的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在下面的链接中查看PDF文件. Java Swing PDF查看器.

I am following the below link to view the PDF file. Java Swing PDF Viewer.

现在,我需要在JFrame上添加一个搜索"按钮(除了导航按钮之外).单击该搜索按钮应打开一个带有文本字段的标签.当客户在该文本字段中输入值时,则应在整个PDF中进行搜索,并在文本字段下方显示匹配单词的数量.我很新,要尝试从3天开始寻找解决方案.但是我没有确切的解决方案.对我来说非常紧急.任何帮助将不胜感激.

Now I need to add a Search button to the JFrame (besides to the navigation buttons). Clicking on that Search button should open a label with a text field. When the client enter the value to that text field then it should perform the search in whole PDF and display the count of matched words below to the text field. I am very new to swing and trying to find the solution from 3 days. But I didn't get the exact solution. It is very urgent to me. Any help is greatly appreciated.

public class PdfViewer extends JPanel {
    private static enum Navigation {
        GO_FIRST_PAGE, FORWARD, BACKWARD, GO_LAST_PAGE, GO_N_PAGE
    }

    private static final CharMatcher POSITIVE_DIGITAL = CharMatcher.anyOf("0123456789");
    private static final String GO_PAGE_TEMPLATE = "%s of %s";
    private static final int FIRST_PAGE = 1;
    private int currentPage = FIRST_PAGE;
    private JButton btnFirstPage;
    private JButton btnPreviousPage;
    private JTextField txtGoPage;
    private JButton btnNextPage;
    private JButton btnLastPage;
    private JButton print;
    private JButton search;
    private PagePanel pagePanel;
    private PDFFile pdfFile;

    public PdfViewer() {
        initial();
    }

    private void initial() {
        setLayout(new BorderLayout(0, 0));
        JPanel topPanel = new JPanel(new FlowLayout(FlowLayout.CENTER));
        add(topPanel, BorderLayout.NORTH);
        btnFirstPage = createButton("|<<");
        topPanel.add(btnFirstPage);
        btnPreviousPage = createButton("<<");
        topPanel.add(btnPreviousPage);
        txtGoPage = new JTextField(10);
        txtGoPage.setHorizontalAlignment(JTextField.CENTER);
        topPanel.add(txtGoPage);
        btnNextPage = createButton(">>");
        topPanel.add(btnNextPage);
        btnLastPage = createButton(">>|");
        topPanel.add(btnLastPage);

        search = new JButton("search");
        topPanel.add(search);
        JScrollPane scrollPane = new JScrollPane();
        add(scrollPane, BorderLayout.CENTER);
        JPanel viewPanel = new JPanel(new BorderLayout(0, 0));
        scrollPane.setViewportView(viewPanel);

        pagePanel = new PagePanel();
        Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
        pagePanel.setPreferredSize(screenSize);
        viewPanel.add(pagePanel, BorderLayout.CENTER);

        disableAllNavigationButton();

        btnFirstPage.addActionListener(new PageNavigationListener(Navigation.GO_FIRST_PAGE));
        btnPreviousPage.addActionListener(new PageNavigationListener(Navigation.BACKWARD));
        btnNextPage.addActionListener(new PageNavigationListener(Navigation.FORWARD));
        btnLastPage.addActionListener(new PageNavigationListener(Navigation.GO_LAST_PAGE));
        txtGoPage.addActionListener(new PageNavigationListener(Navigation.GO_N_PAGE));

        search.addActionListener(new Action1());
    }

    static class Action1 implements ActionListener {        
          public void actionPerformed (ActionEvent e) {  
              JFrame parent = new JFrame();
              JDialog jDialog = new JDialog();
              Label label = new Label("Search: ");
              final JTextField jTextField = new JTextField(10);
              JPanel panel = new JPanel();
              parent.add(panel);
              panel.add(label);
              panel.add(jTextField);
              parent.setVisible(true);
              parent.setSize(800,400);
              parent.setLocationRelativeTo(null);
          }
        }   
}

这是我修改过的代码.其余代码与链接中的代码相同.有了此代码,我在JFrame中获得了一个搜索按钮.当我单击该按钮时,它会打开一个窗口,其中Search为标签名,一个文本字段.因此,现在我很惊讶.当我向该文本字段输入值时,它应该使用文本字段值在整个pdf中进行搜索,并在该文本字段下方显示匹配项的计数.

This is the code i have modified.The remaining code is same as there in link.With this code i am getting a search button in JFrame.When i click on that button it is opening a window with Search as the labelname and a textfield.So now i am struck up here.When i enter a value to that textfield it should perform the search through the whole pdf with the textfield value and display the count of matched below to that textfield.

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Label;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextField;
import javax.swing.SwingUtilities;
import javax.swing.event.DocumentEvent;
import javax.swing.event.DocumentListener;

import com.google.common.base.CharMatcher;
import com.sun.pdfview.PDFFile;
import com.sun.pdfview.PagePanel;

public class PdfViewer extends JPanel {
    private static enum Navigation {
        GO_FIRST_PAGE, FORWARD, BACKWARD, GO_LAST_PAGE, GO_N_PAGE
    }

    private static final CharMatcher POSITIVE_DIGITAL = CharMatcher.anyOf("0123456789");
    private static final String GO_PAGE_TEMPLATE = "%s of %s";
    private static final int FIRST_PAGE = 1;
    private int currentPage = FIRST_PAGE;
    private JButton btnFirstPage;
    private JButton btnPreviousPage;
    private JTextField txtGoPage;
    private JButton btnNextPage;
    private JButton btnLastPage;
    private JButton print;
    private JButton search;
    private PagePanel pagePanel;
    private static PDFFile pdfFile;

    static String text;

    public PdfViewer() {
        initial();
    }

    private void initial() {
        setLayout(new BorderLayout(0, 0));
        JPanel topPanel = new JPanel(new FlowLayout(FlowLayout.CENTER));
        add(topPanel, BorderLayout.NORTH);
        btnFirstPage = createButton("|<<");
        topPanel.add(btnFirstPage);
        btnPreviousPage = createButton("<<");
        topPanel.add(btnPreviousPage);
        txtGoPage = new JTextField(10);
        txtGoPage.setHorizontalAlignment(JTextField.CENTER);
        topPanel.add(txtGoPage);
        btnNextPage = createButton(">>");
        topPanel.add(btnNextPage);
        btnLastPage = createButton(">>|");
        topPanel.add(btnLastPage);

        search = new JButton("search");
        topPanel.add(search);
        JScrollPane scrollPane = new JScrollPane();
        add(scrollPane, BorderLayout.CENTER);
        JPanel viewPanel = new JPanel(new BorderLayout(0, 0));
        scrollPane.setViewportView(viewPanel);

        pagePanel = new PagePanel();
        Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
        pagePanel.setPreferredSize(screenSize);
        viewPanel.add(pagePanel, BorderLayout.CENTER);

        // disableAllNavigationButton();

        search.addActionListener(new Action1());
    }

    private JButton createButton(String string) {
        return new JButton(string);
    }

    class Action1 implements ActionListener {
        @Override
        public void actionPerformed(ActionEvent e) {
            JDialog jDialog = new JDialog(SwingUtilities.getWindowAncestor(search));
            Label label = new Label("Search: ");
            final JTextField jTextField = new JTextField(10);
            jTextField.addActionListener(new ActionListener() {

                @Override
                public void actionPerformed(ActionEvent e) {
                    // Here perform search in PDF
            text = jTextField.getText();
            search(pdfFile,text);
                    System.out.println("Search for text " + jTextField.getText() + " requested");
                }
            });
            // If you want to react to every change of text in the textfield, you can
            // use a DocumentListener and invoke the search method for all events.
            jTextField.getDocument().addDocumentListener(new DocumentListener() {

                @Override
                public void removeUpdate(DocumentEvent e) {

                }

                @Override
                public void insertUpdate(DocumentEvent e) {

                }

                @Override
                public void changedUpdate(DocumentEvent e) {

                }
            });
            JPanel panel = new JPanel();
            jDialog.add(panel);
            panel.add(label);
            panel.add(jTextField);
            jDialog.pack();
            jDialog.setLocationRelativeTo(search);
            jDialog.setVisible(true);
        }
    }

    public static void search(PDFFile pdffile2,String text) {

                System.out.println("Inside searh" +text);
                System.out.println("Inside Search Page ::::::::" + pdffile2.getNumPages()); //43 pages
                for (int i = 0;i <= pdffile2.getNumPages(); i++) {
                PDFPage pdfPage = pdffile2.getPage(i);
                if(pdfPage.equals(text)) {
                    System.out.println("equal");
                }
                else{
                    System.out.println("Not Equal");
                }

                }
            }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                JFrame frame = new JFrame();
                frame.add(new PdfViewer());
                frame.pack();
                frame.setVisible(true);
            }
        });
    }
}

推荐答案

这是一种实现方法.

  1. 在文本字段中添加ActionListener(每次用户按下"Enter"键时,都会调用actionPerformed)
  2. 在文本字段中添加DocumentListener(每次文本在文本字段中更改时,都会调用DocumentListener的方法)
  1. Add an ActionListener to the textfield (actionPerformed will be invoked everytime the user presses "Enter")
  2. Add a DocumentListener to the textfield (the methods of the DocumentListener will be invoked everytime the text changes in the textfield)

您仍然必须在您的课程中实现搜索方法:

You will still have to implement the search method in your class:

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Label;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextField;
import javax.swing.SwingUtilities;
import javax.swing.event.DocumentEvent;
import javax.swing.event.DocumentListener;

import com.google.common.base.CharMatcher;
import com.sun.pdfview.PDFFile;
import com.sun.pdfview.PagePanel;

public class PdfViewer extends JPanel {
    private static enum Navigation {
        GO_FIRST_PAGE, FORWARD, BACKWARD, GO_LAST_PAGE, GO_N_PAGE
    }

    private static final CharMatcher POSITIVE_DIGITAL = CharMatcher.anyOf("0123456789");
    private static final String GO_PAGE_TEMPLATE = "%s of %s";
    private static final int FIRST_PAGE = 1;
    private int currentPage = FIRST_PAGE;
    private JButton btnFirstPage;
    private JButton btnPreviousPage;
    private JTextField txtGoPage;
    private JButton btnNextPage;
    private JButton btnLastPage;
    private JButton print;
    private JButton search;
    private PagePanel pagePanel;
    private PDFFile pdfFile;

    public PdfViewer() {
        initial();
    }

    private void initial() {
        setLayout(new BorderLayout(0, 0));
        JPanel topPanel = new JPanel(new FlowLayout(FlowLayout.CENTER));
        add(topPanel, BorderLayout.NORTH);
        btnFirstPage = createButton("|<<");
        topPanel.add(btnFirstPage);
        btnPreviousPage = createButton("<<");
        topPanel.add(btnPreviousPage);
        txtGoPage = new JTextField(10);
        txtGoPage.setHorizontalAlignment(JTextField.CENTER);
        topPanel.add(txtGoPage);
        btnNextPage = createButton(">>");
        topPanel.add(btnNextPage);
        btnLastPage = createButton(">>|");
        topPanel.add(btnLastPage);

        search = new JButton("search");
        topPanel.add(search);
        JScrollPane scrollPane = new JScrollPane();
        add(scrollPane, BorderLayout.CENTER);
        JPanel viewPanel = new JPanel(new BorderLayout(0, 0));
        scrollPane.setViewportView(viewPanel);

        pagePanel = new PagePanel();
        Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
        pagePanel.setPreferredSize(screenSize);
        viewPanel.add(pagePanel, BorderLayout.CENTER);

        // disableAllNavigationButton();

        search.addActionListener(new Action1());
    }

    private JButton createButton(String string) {
        return new JButton(string);
    }

    class Action1 implements ActionListener {
        @Override
        public void actionPerformed(ActionEvent e) {
            JDialog jDialog = new JDialog(SwingUtilities.getWindowAncestor(search));
            Label label = new Label("Search: ");
            final JTextField jTextField = new JTextField(10);
            jTextField.addActionListener(new ActionListener() {

                @Override
                public void actionPerformed(ActionEvent e) {
                    // Here perform search in PDF
                    System.out.println("Search for text " + jTextField.getText() + " requested");
                }
            });
            // If you want to react to every change of text in the textfield, you can
            // use a DocumentListener and invoke the search method for all events.
            jTextField.getDocument().addDocumentListener(new DocumentListener() {

                @Override
                public void removeUpdate(DocumentEvent e) {

                }

                @Override
                public void insertUpdate(DocumentEvent e) {

                }

                @Override
                public void changedUpdate(DocumentEvent e) {

                }
            });
            JPanel panel = new JPanel();
            jDialog.add(panel);
            panel.add(label);
            panel.add(jTextField);
            jDialog.pack();
            jDialog.setLocationRelativeTo(search);
            jDialog.setVisible(true);
        }
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                JFrame frame = new JFrame();
                frame.add(new PdfViewer());
                frame.pack();
                frame.setVisible(true);
            }
        });
    }
}

这篇关于单击JFrame中的“搜索"按钮,应打开一个搜索栏的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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