java彩色滚动条搜索结果 [英] java coloured scroll bar search result

查看:90
本文介绍了java彩色滚动条搜索结果的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我如何在Java中自定义滚动条,以便我可以进行类似chrome的搜索,也就是说将结果显示为彩色条纹?

How would I go about customizing a scroll bar in Java so that I could have a search that acts like chrome, that's to say puts coloured stripes where the results are?

我不想要一个库,因为我更喜欢自己编写代码。另外,我不想放弃我的L& F.欢迎举例。

I don't want a library as I'd prefer to code it myself. Plus I don't want to loose my L&F that I have. Examples welcome.

实际上它会查看一个大文本文件或一个长jtable,有没有人对如何有一个高效的滚动条有任何好的想法看到这个?
...我应该有一个从头开始结束的双滚动条和一个在短距离内滚动的内部滚动条???

Actually it will be to view a large text file or a long jtable, and does anyone have any good ideas for how I could have an efficient scroll bar to view this? ... should I have a double scroll bar one beginning to end and an inner one which scrolls over a short range???

(对不起它是亲切的一个双重问题,但它是彩色滚动条,将得到勾号)

(sorry it's kind of a double question but it's the coloured scroll bar that will get the tick)

感谢您提供所有可能的帮助和想法。

Thanks for all your possible help and ideas.

编辑:没有代码示例抱歉,因为我试图在我的脑海中计划好这个......你会看到它将是一个带有快速搜索的长文本窗格。用户需要加载此大文件并快速访问详细信息。目前我正在为加载速度保持清晰。其次,文本将是动态的,因此通过单击一个单词,它将在滚动条中放置条带,就像激活了搜索一样。暂时让我们说它是一个带滚动条的简单文本区域。

No code example sorry as I'm trying to plan this out in my head before hand... You see it will be a long text pane with a quick search. Users will need to load in this large file and get quick access to details. For the moment I'm keeping it light for loading speeds. Secondly the text will be dynamic so by clicking on a word it will put a stripe in the scroll bar as if the search was activated. For the moment lets just say it is a simple text area with scroll bar.

推荐答案

import java.awt.*;
import java.awt.event.*;
import java.awt.geom.*;
import java.util.regex.*;
import javax.swing.*;
import javax.swing.text.*;
import javax.swing.plaf.metal.MetalScrollBarUI;

public class ScrollBarSearchHighlighterTest {
  private static final Highlighter.HighlightPainter highlightPainter
    = new DefaultHighlighter.DefaultHighlightPainter(Color.YELLOW);
  private static final String pattern = "Swing";
  private static final String initTxt =
    "Trail: Creating a GUI with JFC/Swing\n" +
    "Lesson: Learning Swing by Example\n" +
    "This lesson explains the concepts you need to\n" +
    " use Swing components in building a user interface.\n" +
    " First we examine the simplest Swing application you can write.\n" +
    " Then we present several progressively complicated examples of creating\n" +
    " user interfaces using components in the javax.swing package.\n" +
    " We cover several Swing components, such as buttons, labels, and text areas.\n" +
    " The handling of events is also discussed,\n" +
    " as are layout management and accessibility.\n" +
    " This lesson ends with a set of questions and exercises\n" +
    " so you can test yourself on what you've learned.\n" +
    "http://docs.oracle.com/javase/tutorial/uiswing/learn/index.html\n";

  private final JTextArea textArea   = new JTextArea();
  private final JScrollPane scroll   = new JScrollPane(textArea);
  private final JScrollBar scrollbar = new JScrollBar(JScrollBar.VERTICAL);
  public JComponent makeUI() {
    textArea.setEditable(false);
    textArea.setLineWrap(true);
    textArea.setText(initTxt+initTxt+initTxt);
    scrollbar.setUnitIncrement(10);

    scrollbar.setUI(new MetalScrollBarUI() {
      @Override protected void paintTrack(
            Graphics g, JComponent c, Rectangle trackBounds) {
        super.paintTrack(g, c, trackBounds);
        Rectangle rect = textArea.getBounds();
        double sy = trackBounds.getHeight() / rect.getHeight();
        AffineTransform at = AffineTransform.getScaleInstance(1.0, sy);
        Highlighter highlighter = textArea.getHighlighter();
        g.setColor(Color.YELLOW);
        try{
          for(Highlighter.Highlight hh: highlighter.getHighlights()) {
            Rectangle r = textArea.modelToView(hh.getStartOffset());
            Rectangle s = at.createTransformedShape(r).getBounds();
            int h = 2; //Math.max(2, s.height-2);
            g.fillRect(trackBounds.x+2, trackBounds.y+1+s.y, trackBounds.width, h);
          }
        } catch(BadLocationException e) {
          e.printStackTrace();
        }
      }
    });
    scroll.setVerticalScrollBar(scrollbar);

    Box box = Box.createHorizontalBox();
    box.add(Box.createHorizontalGlue());
    box.add(new JButton(new AbstractAction("highlight") {
      @Override public void actionPerformed(ActionEvent e) {
        setHighlight(textArea, pattern);
      }
    }));
    box.add(Box.createHorizontalStrut(2));
    box.add(new JButton(new AbstractAction("clear") {
      @Override public void actionPerformed(ActionEvent e) {
        textArea.getHighlighter().removeAllHighlights();
        scrollbar.repaint();
      }
    }));

    JPanel p = new JPanel(new BorderLayout());
    p.add(scroll);
    p.add(box, BorderLayout.SOUTH);
    return p;
  }
  public void setHighlight(JTextComponent jtc, String pattern) {
    Highlighter highlighter = jtc.getHighlighter();
    highlighter.removeAllHighlights();
    Document doc = jtc.getDocument();
    try{
      String text = doc.getText(0, doc.getLength());
      Matcher matcher = Pattern.compile(pattern).matcher(text);
      int pos = 0;
      while(matcher.find(pos)) {
        int start = matcher.start();
        int end   = matcher.end();
        highlighter.addHighlight(start, end, highlightPainter);
        pos = end;
      }
    }catch(BadLocationException e) {
      e.printStackTrace();
    }
    scroll.repaint();
  }
  public void removeHighlights(JTextComponent jtc) {
    Highlighter hilite = jtc.getHighlighter();
    Highlighter.Highlight[] hilites = hilite.getHighlights();
    for(int i=0; i<hilites.length; i++) {
      if(hilites[i].getPainter() instanceof
             DefaultHighlighter.DefaultHighlightPainter) {
        hilite.removeHighlight(hilites[i]);
      }
    }
    scroll.repaint();
  }
  public static void main(String[] args) {
    EventQueue.invokeLater(new Runnable() {
      @Override public void run() {
        createAndShowGUI();
      }
    });
  }
  public static void createAndShowGUI() {
    JFrame frame = new JFrame();
    frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
    frame.getContentPane().add(new ScrollBarSearchHighlighterTest().makeUI());
    frame.setSize(320, 240);
    frame.setLocationRelativeTo(null);
    frame.setVisible(true);
  }
}

或另一种选择是使用MatteBorder + Icon + RowHeader: JScrollBar搜索荧光笔

Or another option is to use "MatteBorder + Icon + RowHeader": JScrollBar search highlighter

这篇关于java彩色滚动条搜索结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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