Hi, im trying to use an actionlistener attached to a button to add a component to a frame I have created. The actual button should add to the frame a circle with some text placed in it which I have already created, it works fine so there's no problems there.

When I do this in my main method (i.e startGUI() )

static JTree j1 = new JTree();
centerframe.add(j1, BorderLayout.CENTER);

it is displayed fine but when i use it in the following context it does nothing (except display the output message i use to check whether the actions are performing when the buttons are clicked:

public void actionPerformed(ActionEvent ear) 
	  {
		 if("addition".equals(ear.getActionCommand() )  ){
			 
			 centerframe.add(j1, BorderLayout.CENTER);
			 System.out.println("ADD");
			 
		 } else if("delete".equals(ear.getActionCommand() )){
			 
			 System.out.println("DELETE");
			 
		 }
	  }

Why doesn't this work? Any suggestions would be appreciated!

Yeah so I've solved this little problem too. Theres a-lot of code involved so I won't display it all. I'll leave the part that draws the actually components out.

JButton generate = new JButton("Generate Tree");
		generate.addActionListener(new ActionListener()
		{
			public void actionPerformed(ActionEvent e)
			{
				Shapes widget;
				widget = new Shapes();
				
				myFrame.add(widget);
				myFrame.validate();
				
				orderReal.setText(setMessage());
				printtoFile("\nOrder: "+setMessage());
				
				sizeNumber.setText(Size());
				printtoFile("Size of Tree: "+Size());
				
				inttreeHeight.setText(getTreeHeight());
				printtoFile("Tree Height: "+getTreeHeight());
				
				printtoFile("Root node: "+returnRoot()+"\n");
			}
});

Hope this helps :D

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.