Hi, im trying to call this method in my class "MyWidget" which is suppose to draw a line when someone indicates the coordinates via 4 parameters. Here is the method:

public void Connect(int xline, int yline, int nextX, int nextY)
	{
		Graphics g = getGraphics();
		g.drawLine(xline, yline, nextX, nextY);
	}

When I call this method from another class, for example from my class JTree, and substitute in values a click run on the GUI button it just gives me an error message. Here is the code when I call it:

JButton drawLine = new JButton("Draw a line");
		drawLine.addActionListener(new ActionListener()
		{
			public void actionPerformed(ActionEvent e)
			{
				MyWidget widget;
				widget = new MyWidget();
				
				widget.Connect(0, 0, 300, 300);
				myFrame.add(widget);
				myFrame.validate();
				
		
			}
		});

Whats the problem, anyone?

I have managed to find the solution to this problem. If you want to create a line by pressing a button then what I did was to create a class which extends JPanel and then create a method which draws the line:

class MyWidget extends JPanel
{

	static JTree myTree = new JTree();
	static BinarySearchTree myPlant = new BinarySearchTree();
	
	public void paintComponent(Graphics g)
	{
		
		super.paintComponent(g);
		Connect(0, 0, 300, 300);
	
	}
      
       public void Connect(int xline, int yline, int nextX, int nextY)
	{
		Graphics g = getGraphics();
		g.drawLine(xline, yline, nextX, nextY);
	}

int xline is the x co-ordinate of the first point, int yline the y-coordinate of the first point. I m sure you can guess what the next values stand for?

Next to call this method i created an object of this class in the method for the button and then the job was done.

JButton generate = new JButton("Generate Tree");
		generate.addActionListener(new ActionListener()
		{
			public void actionPerformed(ActionEvent e)
			{
				MyWidget widget;
				widget = new MyWidget();
				
				myFrame.add(widget);
				myFrame.validate();
		
			}
		});

Job done :P

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.