2013-01-05 86 views
3

我該如何去定製Java中的滾動條,這樣我就可以有一個像鉻一樣的搜索,也就是說把彩色條紋放在結果的位置?java彩色滾動條搜索結果

我不想要一個圖書館,因爲我寧願自己編碼。另外我不想放鬆自己的L。例子歡迎。

其實它會查看一個大的文本文件或一個長的jtable,有沒有人有任何好的想法,我怎麼能有一個高效的滾動條來查看? ...我應該有一個雙滾動條一個開始結束和一個內部滾動短期範圍???

(抱歉,這是種雙重問題,但它的彩色滾動條,將得到蜱)

感謝您的一切可能的幫助和想法。

編輯:沒有代碼示例對不起,因爲我試圖在我的頭腦中計劃出這件事......你會發現它將是一個長文本窗格,並帶有快速搜索功能。用戶需要加載這個大文件並快速訪問細節。目前,我正在爲加載速度保持輕量級。其次,文本將是動態的,所以通過單擊一個單詞,它會在滾動條中放入一個條紋,就像搜索被激活一樣。目前我們只是說它是一個帶滾動條的簡單文本區域。

+0

一些示例代碼從工作將是一個良好的開端TBH – Mark

+1

也許從這裏開始:HTTP:/ /explodingpixels.wordpress.com/2008/12/08/skinning-a-scroll-bar-part-1/真的,我會考慮編寫一個監聽器和一個擴展滾動條的類。使用使用監聽器通過一種方法用新圖像更新滾動條,您將寫入您的新滾動條類 – Mark

+0

不錯的鏈接,明天我會檢查它,我正在考慮放置圖像,但因爲我從來沒有定製過JScrollPane我不知道什麼是可能的... – xchiltonx

回答

1

enter image description here

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 +圖標+的rowHeader」:JScrollBar search highlighter