Tinmar
Tinmar

Reputation: 370

How to connect JLabels with a line on JPanel with MigLayout?

I searched a lot on google on this subject, but just can't come out with right solution. I tried "painting" with Graphics paintComponent and everything seems fine, but lines just doesn't appear on my JPanel. Part of my code with JLabels created:

frame = new JFrame();
        frame.setTitle("New family tree");
        ...
                JPanel panel = new JPanel();
        panel.setBackground(new Color(30, 144, 255));
        frame.getContentPane().add(panel, BorderLayout.EAST);
        panel.setLayout(new MigLayout("", "[]", "[][][][][][][][]"));


    JButton newPersonButton = new JButton("New Person");
    panel.add(newPersonButton, "cell 0 5");

    tabbedPane = new JTabbedPane(JTabbedPane.TOP);
    frame.getContentPane().add(tabbedPane, BorderLayout.CENTER);

    scrollPane = new JScrollPane();
    tabbedPane.addTab("Tree", null, scrollPane, null);

    panel_1 = new JPanel();
    scrollPane.setViewportView(panel_1);
    panel_1.setLayout(new MigLayout("",
            "[][][][][][][][][][][][][][][][][]",
            "[][][][][][][][][][][][][][][][][][][][][]"));

    final JLabel lblAddGreatgrandmother = new JLabel("Add Great-grandmother");
    panel_1.add(lblAddGreatgrandmother, "cell 3 4,growx");

    final JLabel lblAddGrandmother_1 = new JLabel("Add Grandmother");
    panel_1.add(lblAddGrandmother_1, "cell 2 5");

Should I use painting? Or put JLabels in array list and use Point? I'll appriciate any help.

EDIT: Runnable example - http://pastebin.com/NFug1QA1

Upvotes: 0

Views: 195

Answers (1)

ControlAltDel
ControlAltDel

Reputation: 35011

The problem with the java-sl.com/connector solution is that the connection itself becomes a component- ie needs layout, accepts events etc. I put together a solution for this years ago, which you can find in my sourceforge project. Specifically, see ConnectionPanel and Connection.

... Also, just in my experience, they kinds of connections are most useable when the layout manager is set to null and components are not restricted to being locked into position. But you will know what's best for your case.

Upvotes: 2

Related Questions