2012-04-11 79 views
3

我正在尋找一個Swing組件,它是一個滾動窗格/視口的擴展,允許打開並導航到當前顯示的內容的概覽。 我確定我看過一些博客帖子,顯示了這樣一個組件,但無法再找到組件。顯示概覽的滾動窗格/視口擺動組件

這裏是我所期待的一個畫面:

scrollpane with overview

如果你還記得這樣一個職位,或者如果你有你的書籤一些鏈接我要感謝你分享的信息。

感謝您的幫助。

+0

也看到這個[答案](http://stackoverflow.com/a/10097538/230513)。 – trashgod 2012-04-11 15:27:39

回答

1

是可能的,並且與JComponent(s)

1)GlassPane

2)JLayer (Java7)基於JXLayer(的Java6)

3)JViewport

4)Translucent JDialog定位

+0

謝謝,但我想避免重新發明輪子,因爲我確信我已經看到已經完成並由其他人發佈的現有貢獻。 – 2012-04-11 09:57:04

+0

對不起,沒必要,如果我想這樣做,那麼通過使用[GlassPane](http://stackoverflow.com/a/10059295/714968)可以完成這項工作每幾個小時,注意我的點數阻止MouseEvents默認情況下,但鍵盤事件可以安逸地只採用模態JDialog ... – mKorbel 2012-04-11 10:03:24

2

這是一個工作示例,但您需要java-image-scaling庫(其LGPL) - 它是用於預覽圖像的平滑縮放,否則,你得到完全毀了形象:

public static void main (String[] args) 
{ 
    final JFrame mainFrame = new JFrame(); 

    JPanel content = new JPanel (new GridLayout (20, 20)); 
    for (int i = 0; i < 20; i++) 
    { 
     for (int j = 0; j < 20; j++) 
     { 
      content.add (new JLabel ("Test " + i + ":" + j) 
      { 
       { 
        setBorder (BorderFactory.createEmptyBorder (20, 20, 20, 20)); 
       } 
      }); 
     } 
    } 

    final JScrollPane pane = new JScrollPane (content); 
    pane.setCorner (JScrollPane.LOWER_TRAILING_CORNER, new JButton() 
    { 
     { 
      final JButton button = this; 
      addActionListener (new ActionListener() 
      { 
       public void actionPerformed (ActionEvent e) 
       { 
        JComponent comp = (JComponent) pane.getViewport().getView(); 
        Dimension size = comp.getSize(); 
        Rectangle viewRect = comp.getVisibleRect(); 

        // Drawing preview 
        BufferedImage image = new BufferedImage (size.width, size.height, 
          BufferedImage.TYPE_INT_RGB); 
        Graphics2D g2d = image.createGraphics(); 
        comp.print (g2d); 
        g2d.dispose(); 

        // Rescaling preview 
        int width = 200; 
        int height = comp.getHeight() * width/comp.getHeight(); 
        BufferedImage rescaled = 
          new ResampleOp (width, height).filter (image, null); 

        // Drawing view rect 
        float diff = (float)width/size.width; 
        g2d = rescaled.createGraphics(); 
        g2d.setPaint (Color.RED); 
        g2d.drawRect (Math.round (viewRect.x * diff), 
          Math.round (viewRect.y * diff), 
          Math.round (viewRect.width * diff), 
          Math.round (viewRect.height * diff)); 
        g2d.dispose(); 

        // Displaying preview 
        final JDialog preview = new JDialog (mainFrame); 
        preview.setUndecorated (true); 
        preview.add (new JLabel (new ImageIcon (rescaled)) 
        { 
         { 
          setBorder (BorderFactory.createLineBorder (Color.BLACK)); 
          setFocusable (true); 
         } 
        }); 
        Point los = button.getLocationOnScreen(); 
        preview.setSize (width + 2, height + 2); 
        preview.setLocation (los.x - width - 2, los.y - height - 2); 
        preview.setVisible (true); 

        preview.requestFocus(); 
        preview.addFocusListener (new FocusAdapter() 
        { 
         public void focusLost (FocusEvent e) 
         { 
          preview.dispose(); 
         } 
        }); 
       } 
      }); 
     } 
    }); 
    mainFrame.add (pane); 

    mainFrame.setSize (600, 600); 
    mainFrame.setLocationRelativeTo (null); 
    mainFrame.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE); 
    mainFrame.setVisible (true); 
} 

所以基本上我創建了面板的快照,重新調整並放入小對話框,在按鈕附近打開並關閉焦點丟失。創建快照可能會浪費時間,但我不知道有什麼更好的方法來創建整個scrollpane容器的渲染預覽 - 您必須將它繪製到某個顯示預覽的東西上,這意味着它將花費相同的時間。

你也可以很容易地修改預覽組件,因此您可以通過拖動紅色矩形的預覽對話框:)

+0

嗨,這是一個很好的起點,但我仍然會嘗試找到我正在考慮的組件。 – 2012-04-11 10:10:46

+0

正如我記得我在SwingX示例中看到類似的東西 - 你可能會在那裏檢查它。不知道,雖然... – 2012-04-11 10:29:20