My own component is not is not displaying on panel.
import rysujdrzewo.nodeLabel.NodeLabel;
import rysujdrzewo.nodeLabel.PointOfContact;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Point;
import javax.swing.JComponent;
public class PaintLinesBetweenNodes extends JComponent{
private final NodeLabel oNode1;
private final NodeLabel oNode2;
private final PointOfContact oPointForNode1;
private final PointOfContact oPointForNode2;
private final String sEdgeName;
public PaintLinesBetweenNodes(NodeLabel oNode1,NodeLabel oNode2, PointOfContact oPointForNode1, PointOfContact oPointForNode2, String sEdgeName){
super();
this.oNode1 = oNode1;
this.oNode2 = oNode2;
this.oPointForNode1 = oPointForNode1;
this.oPointForNode2 = oPointForNode2;
this.sEdgeName = sEdgeName;
}
private void paintNodeConnections(Graphics2D oGraphics) {
Point p1 = oNode1.getConnectionPoint(oPointForNode1);
Point p2 = oNode2.getConnectionPoint(oPointForNode2);
oGraphics.drawLine(p1.x, p1.y, p2.x, p2.y);
oGraphics.drawString(sEdgeName, (float)(p1.x + p2.x) / (float)2, (float)(p1.y + p2.y) / (float)2);
}
@Override
public void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2d = (Graphics2D) g;
paintNodeConnections(g2d);
}
}
This one is displaying correctly:
import java.awt.Point;
import java.awt.event.ComponentEvent;
import java.util.HashMap;
import java.util.Map;
import javax.swing.JLabel;
public class NodeLabel extends JLabel{
Map<PointOfContact, Point> connectionPoints = new HashMap<>();
public NodeLabel(String text) {
super(text);
addComponentListener(new java.awt.event.ComponentAdapter(){
@Override
public void componentResized(java.awt.event.ComponentEvent evt) {
mapConnectionPoints();
}
@Override
public void componentMoved(ComponentEvent e) {
mapConnectionPoints();
}
});
}
// updates the mapped positions of the connection points
// called whenever the component get's resized or moved
private void mapConnectionPoints(){
connectionPoints.clear();
Point point = new Point(getX(),getY()+getHeight()/2);
connectionPoints.put(PointOfContact.LEFT, point);
point = new Point(getX() + getWidth(), getY() + getHeight()/2);
connectionPoints.put(PointOfContact.RIGHT, point);
point = new Point(getX() + getWidth()/2, getY());
connectionPoints.put(PointOfContact.TOP, point);
point = new Point(getX() + getWidth()/2, getY() + getHeight());
connectionPoints.put(PointOfContact.BOTTOM, point);
}
public Point getConnectionPoint(PointOfContact key) {
return connectionPoints.get(key);
}
}
and execution:
import java.awt.Color;
import java.awt.Dimension;
import javax.swing.BorderFactory;
import javax.swing.JFrame;
import static javax.swing.JFrame.EXIT_ON_CLOSE;
import javax.swing.JPanel;
import javax.swing.SwingConstants;
import rysujdrzewo.nodeLabel.NodeLabel;
import rysujdrzewo.nodeLabel.PointOfContact;
public class PaintTree extends JFrame{
public PaintTree() {
initComponents();
}
private void initComponents() {
setDefaultCloseOperation(EXIT_ON_CLOSE);
setMinimumSize(new Dimension(800, 800));
JPanel oJPanel = new JPanel();
oJPanel.setLayout(null);
NodeLabel oNode1 = new NodeLabel("Node 1");
oNode1.setHorizontalAlignment(SwingConstants.CENTER);
oNode1.setBorder(BorderFactory.createLineBorder(new Color(0, 0, 0)));
NodeLabel oNode2 = new NodeLabel("Node 2");
oNode2.setHorizontalAlignment(SwingConstants.CENTER);
oNode2.setBorder(BorderFactory.createLineBorder(new Color(0, 0, 0)));
NodeLabel oNode3 = new NodeLabel("Node 3");
oNode3.setHorizontalAlignment(SwingConstants.CENTER);
oNode3.setBorder(BorderFactory.createLineBorder(new Color(0, 0, 0)));
oJPanel.add(oNode1);
oJPanel.add(oNode2);
oJPanel.add(oNode3);
PaintLinesBetweenNodes o1 = new PaintLinesBetweenNodes(oNode1, oNode2, PointOfContact.BOTTOM, PointOfContact.TOP, "Czesc1");
PaintLinesBetweenNodes o2 = new PaintLinesBetweenNodes(oNode1, oNode3, PointOfContact.BOTTOM, PointOfContact.TOP, "Czesc2");
oJPanel.add(o1);
oJPanel.add(o2);
getContentPane().add(oJPanel);
oNode1.setBounds(100, 52, 56, 28);
oNode2.setBounds(54, 200, 56, 28);
oNode3.setBounds(150, 200, 56, 28);
pack();
}
}
I have no idea what's wrong with this code. I don't use any layouts because I try create some graphical representation of tree data(Decision tree).
Basically, you never set the size of your instances of PaintLinesBetweenNodes
, meaning that they will have an initial size of 0x0
This is one of the problems of using a null
layout, you become responsible for sizing an positioning of the components
I also think you'll find pack
won't work they way you expect it to, as it relies on the information supplied by the layout managers