2012-09-12 10 views
6

में 2 पैनल के बीच एक लाइनों आकर्षित कर सकते हैं बस एक रेखा खींचने के लिए उन्हें w/b द्वारा पैनल जुड़ना चाहते हैं।हम कैसे स्विंग

मेरे पास दो पैनल हैं और दोनों पैनलों में एक Jtable है। मैं एक पैनल के jtable के प्रत्येक सेल को किसी अन्य जेपीएलएल के किसी अन्य Jtable से कनेक्ट करना चाहता हूं।

enter image description here

यहाँ मैं लाइनों की तरह मैं गुलाबी रंग चक्र से प्रकाश डाला है आकर्षित करने के लिए चाहते हैं।

और इस कोड स्निपेट मैं jtables

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

यहाँ FieldMapperJTable मेरी अनुकूलित JTable वर्ग है बनाने के लिए उपयोग कर रहा हूँ है।

+0

किसी भी प्रकार की मदद या किसी अन्य तरीके का उपयोग करने के विचार की सराहना की जाएगी। बहुत पहले से धन्यवाद। –

+1

@ स्टैनिस्लाव आपकी पोस्ट के लिए बहुत बहुत धन्यवाद। यह मेरे लिए काम किया गया था। "http://java-sl.com/connector.html" –

+0

@ आश्चर्यचकित होगा कि आपने स्टानी के जवाब को क्यों हटाया - यह निश्चित रूप से उपयोगी है – 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

पी एस लाइनों को मोटा बनाने के लिए आप पेंटिंग करते समय स्ट्रोक बदल सकते हैं:

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

मुझे यकीन है कि अगर यह अभी भी है, लेकिन दिन गए जब स्विंग नए और चमकदार था मैं घटनाओं से निपटने जब glasspane का उपयोग करने की कोशिश कर समस्याओं की बहुत सारी में भाग गया में वापस लागू होता है नहीं कर रहा हूँ। –

+0

+1 घटकों के ऊपर प्रतिपादन के लिए, साथ ही एक सराहनीय टिप्पणी शैली। :-) – trashgod

+0

@JensSchauder हाँ, वहाँ की समस्याओं, आप glasspane सब पर किसी भी घटनाओं (नहीं समस्याएं हैं, लेकिन छोटे कठिनाइयों वास्तव में) को संभालने के लिए चाहते हैं, तो कर रहे हैं। वैसे भी, यही कारण है कि मैं ग्लास फलक घटक की "शामिल" विधि को ओवरराइड करता हूं। आप हमेशा वहाँ रिटर्न फाल्स (या लाइनों के आकार के साथ यह सीमा) है - इसे कहीं भी "अवशोषित" नहीं होगा घटनाओं (या बस आकार सीमा है, जो ठीक रूप में अच्छी तरह है में)। –