[英]How can we Draw a lines between 2 panels in swing
Just want to connect panels by drawing a line b/w them. 只想通过画线来连接面板。
I am having two panels and both panels contains a Jtable.I want to connect each cell of jtable of one panel to another Jtable of another jpanel. 我有两个面板,两个面板都包含一个Jtable。我想将一个面板的jtable的每个单元连接到另一个面板的另一个Jtable。
Here i want to draw the lines like that i have highlighted by pink color circle. 在这里,我想绘制像我用粉红色圆圈突出显示的线条。
and this is the code snippet i am using to create jtables 这是我用来创建jtables的代码片段
DefaultTableModel fcdbDataModel = new DefaultTableModel(fcdbIdTxnArray,
fcdbIdTxnColumnArray);
fcdbIdTxnJTable = new FieldMapperJTable(fcdbDataModel);
here FieldMapperJTable is my customized jtable class. 这里FieldMapperJTable是我自定义的jtable类。
You could easily do that using JFrame/JDialog GlassPane as the painting field. 您可以使用JFrame / JDialog GlassPane作为绘图字段轻松完成此操作。 Just set your custom component as a glass pane for the frame and paint the link directly on it.
只需将自定义组件设置为框架的玻璃窗格,然后直接在其上绘制链接。
You can also do the same using the layered pane of the frame/dialog. 您也可以使用框架/对话框的分层窗格执行相同操作。
Here is a small working example of how to draw such "links" on the glass pane component: 这是一个如何在玻璃窗格组件上绘制这样的“链接”的小工作示例:
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 ();
}
}
}
And the result: 结果如下:
(just click any two buttons one after another and they will get linked) (只需一个接一个地点击任意两个按钮,它们就会被链接)
PS To make the lines thicker you can change the stroke when painting: PS要使线条更粗,您可以在绘画时更改笔划:
g2d.setStroke ( new BasicStroke ( 5f ) );
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.