I have a Jgoodies form, here it's code.
public class MainForm{
MainForm(){
createUIComponents();
}
public JPanel getMainPanel() {
return mainPanel;
}
private void createUIComponents() {
drawingPanel=new DrawingPanel();
//there is some code, which adds components to combo boxes
}
private JPanel mainPanel;
private JComboBox directDirectionCombobox;
private JButton directLineOkButton;
private JButton crossLineOkButton;
private JComboBox crossLineComboBox;
private JTextField crossLineSizeValue;
private JButton clearButton;
private JLabel directLineLabel;
private JPanel directLinePanel;
private JLabel crossLineLabel;
private JPanel crossLinePanel;
private JPanel okClearButtonPanel;
private JTextField directLineSizeValue;
private JButton saveButton;
private JPanel drawingPanel;
}
DrawingPanel is a custom class which extends JPanel which I want to add on the panel. Here is a screenshot of user interface. Well, it doesn't work. JPanel is created, but it's not my custom class. I tried to enable custom create field in property inspector, but it says that there are null pointer exception. Any way to add custom class to JPanel?
Update: Here is a code of DrawingPanel class and MouseHandler
public class DrawingPanel extends JPanel{
DrawingPanel(){
mouseHandler=new MouseHandler(this);
addMouseListener(mouseHandler);
}
public void paintComponent(Graphics g){
super.paintComponent(g);
Point=mouseHandler.getPoint();
if (Point!=null){
System.out.println(Point.getX()+" "+Point.getY());
}
g2 = (Graphics2D) g;
g2.setColor(Color.BLACK);
g2.setStroke(new BasicStroke(2));
g2.draw(new Line2D.Double(0,0,10,10));
}
private Graphics2D g2;
private MouseHandler mouseHandler;
private Point2D Point;
}
public class MouseHandler implements MouseListener
{
MouseHandler(DrawingPanel thePanel)
{
this.panel=thePanel;
}
public void mouseClicked(MouseEvent event)
{
System.out.println("OK");
Point=event.getPoint();
panel.repaint();
}
public void mouseEntered(MouseEvent arg0) {
// TODO Auto-generated method stub
}
public void mouseExited(MouseEvent arg0) {
// TODO Auto-generated method stub
}
public void mousePressed(MouseEvent arg0) {
// TODO Auto-generated method stub
}
@Override
public void mouseReleased(MouseEvent arg0) {
// TODO Auto-generated method stub
}
public Point2D getPoint()
{
Point2D thePoint=this.Point;
this.Point=null;
return thePoint;
}
private Point2D Point;
private DrawingPanel panel;
}
So, when I click mouse on the white field it should write "OK" at the console, but that's not happening.