2012-09-12 22 views
6

只需通過繪製一條線連接面板b/w他們。我們如何在2個面板之間繪製線條擺動

我有兩個面板,兩個面板都包含一個Jtable.I想要將一個面板的jtable的每個單元連接到另一個jpanel的另一個Jtable。

enter image description here

在這裏,我想畫一樣,我有粉紅色圓圈突出的線條。

,這是代碼片段我使用創建JTable

DefaultTableModel fcdbDataModel = new DefaultTableModel(fcdbIdTxnArray, 
    fcdbIdTxnColumnArray); 
fcdbIdTxnJTable = new FieldMapperJTable(fcdbDataModel); 

這裏FieldMapperJTable是我定製JTable類。

+0

任何類型的幫助或使用其他方式的想法,將不勝感激。提前感謝。 –

+1

@Stanislav非常感謝您的回覆。這對我很有幫助。 「http://java-sl.com/connector.html」 –

+0

@想知道爲什麼你刪除了Stani的答案 - 這絕對有幫助 – kleopatra

回答

8

你可以很容易地使用JFrame/JDialog GlassPane作爲繪畫領域。只需將您的自定義組件設置爲框架的玻璃窗格並直接在其上繪製鏈接即可。

您還可以使用框架/對話框的分層窗格執行相同操作。

下面是如何在玻璃面板組件上得出這樣的「鏈接」的小工作示例:

import javax.swing.*; 
import java.awt.*; 
import java.awt.event.ActionEvent; 
import java.awt.event.ActionListener; 
import java.util.HashMap; 
import java.util.Map; 

/** 
* @see http://stackoverflow.com/a/12389479/909085 
*/ 

public class ComponentLinkerTest extends JComponent 
{ 
    private Map<JComponent, JComponent> linked; 

    public ComponentLinkerTest() 
    { 
     super(); 
     linked = new HashMap<JComponent, JComponent>(); 
    } 

    public void link (JComponent c1, JComponent c2) 
    { 
     linked.put (c1, c2); 
     repaint(); 
    } 

    protected void paintComponent (Graphics g) 
    { 
     Graphics2D g2d = (Graphics2D) g; 
     g2d.setRenderingHint (RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); 

     g2d.setPaint (Color.BLACK); 
     for (JComponent c1 : linked.keySet()) 
     { 
      Point p1 = getRectCenter (getBoundsInWindow (c1)); 
      Point p2 = getRectCenter (getBoundsInWindow (linked.get (c1))); 
      g2d.drawLine (p1.x, p1.y, p2.x, p2.y); 
     } 
    } 

    private Point getRectCenter (Rectangle rect) 
    { 
     return new Point (rect.x + rect.width/2, rect.y + rect.height/2); 
    } 

    private Rectangle getBoundsInWindow (Component component) 
    { 
     return getRelativeBounds (component, getRootPaneAncestor (component)); 
    } 

    private Rectangle getRelativeBounds (Component component, Component relativeTo) 
    { 
     return new Rectangle (getRelativeLocation (component, relativeTo), 
       component.getSize()); 
    } 

    private Point getRelativeLocation (Component component, Component relativeTo) 
    { 
     Point los = component.getLocationOnScreen(); 
     Point rt = relativeTo.getLocationOnScreen(); 
     return new Point (los.x - rt.x, los.y - rt.y); 
    } 

    private JRootPane getRootPaneAncestor (Component c) 
    { 
     for (Container p = c.getParent(); p != null; p = p.getParent()) 
     { 
      if (p instanceof JRootPane) 
      { 
       return (JRootPane) p; 
      } 
     } 
     return null; 
    } 

    public boolean contains (int x, int y) 
    { 
     return false; 
    } 

    private static ComponentLinkerTest linker; 

    public static void main (String[] args) 
    { 
     setupLookAndFeel(); 

     JFrame frame = new JFrame(); 

     linker = new ComponentLinkerTest(); 
     frame.setGlassPane (linker); 
     linker.setVisible (true); 

     JPanel content = new JPanel(); 
     content.setLayout (new GridLayout (10, 5, 5, 5)); 
     content.setBorder (BorderFactory.createEmptyBorder (5, 5, 5, 5)); 
     frame.add (content); 

     for (int i = 0; i < 50; i++) 
     { 
      final JButton button = new JButton ("Button" + i); 
      button.addActionListener (new ActionListener() 
      { 
       public void actionPerformed (ActionEvent e) 
       { 
        link (button); 
       } 
      }); 
      content.add (button); 
     } 

     frame.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE); 
     frame.pack(); 
     frame.setLocationRelativeTo (null); 
     frame.setVisible (true); 
    } 

    private static JButton last = null; 

    private static void link (JButton button) 
    { 
     if (last == null) 
     { 
      last = button; 
     } 
     else 
     { 
      linker.link (last, button); 
      last = null; 
     } 
    } 

    private static void setupLookAndFeel() 
    { 
     try 
     { 
      UIManager.setLookAndFeel (UIManager.getSystemLookAndFeelClassName()); 
     } 
     catch (ClassNotFoundException e) 
     { 
      e.printStackTrace(); 
     } 
     catch (InstantiationException e) 
     { 
      e.printStackTrace(); 
     } 
     catch (IllegalAccessException e) 
     { 
      e.printStackTrace(); 
     } 
     catch (UnsupportedLookAndFeelException e) 
     { 
      e.printStackTrace(); 
     } 
    } 
} 

而結果:
(只需點擊任意兩個按鈕,一個又一個,他們將得到鏈接)
enter image description here

PS爲了使線條更粗,您可以在塗漆時更改筆劃:

g2d.setStroke (new BasicStroke (5f)); 
+0

我不確定這是否仍然適用,但在Swing是新的和有光澤的日子裏,當我嘗試使用玻璃窗時遇到了很多處理事件的問題。 –

+0

+1用於渲染組件頂部以及令人讚歎的評論風格。 :-) – trashgod

+0

@JensSchauder是的,如果你想讓glasspane處理所有事件(不是問題,但實際上很小的困難),就會出現問題。無論如何,這就是爲什麼我重寫玻璃窗格組件的「包含」方法。如果你總是在那裏返回假(或者用線的形狀限制它) - 它不會「吸收」任何地方的事件(或者只是在形狀邊界上,這也很好)。 –

相關問題