Open Side Menu Go to the Top

02-23-2013 , 06:59 AM
I am currently working with a Java Swing application and have the following issue:

So I am having an application that dynamically creates JPanels and add them to the main window, on this panel there is also a button. However I wont get the button to work, its displayed and all but is not active.

I assume it has something to do with it the fact that its added during runtime cause if I move the button and put it in one of the panels that exists from the beginning it works with the same code.

Any suggestions for solutions?
Adding JButton at runtime (java) Quote
Adding JButton at runtime (java)
150% up to $2,000 Welcome Bonus on CoinPoker
Join the action now
Daily Rewards • Splash Pots • CoinRaces
Adding JButton at runtime (java)
02-24-2013 , 10:34 PM
Try creating a minimal code fragment and post it here, or better yet on http://stackoverflow.com/.

Here's a sample of one approach:
Code:
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;

public class Temp extends JFrame {
	static int WIDTH = 400;
	static int HEIGHT = 300;
	int buttonCount = 1;

	public Temp() {
		setLayout(new FlowLayout());
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		addButton();
		setSize(WIDTH, HEIGHT);
	}

	public void addButton() {
		JButton b = new JButton("Button " + buttonCount++);
		b.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				addButton();				
			}
		});
		add(b);
		revalidate();
	}
	
	public static void main(String[] args) {
		Temp t = new Temp();
		
		t.setVisible(true);
	}
	
}
Adding JButton at runtime (java) Quote
03-01-2013 , 05:38 AM
I didnt really know what parts of the code to provide since the button itself worked but only adding it during runtime was the problem. And providing all code regarding what could potentially be the problem seemd to much.

After discussing this with my teacher the problem was that the panels were added to a JTable and we lacked a method isCellEditable. This info was probably very important so sry for leaving it out. Thanks anyway.
Adding JButton at runtime (java) Quote
Adding JButton at runtime (java)
150% up to $2,000 Welcome Bonus on CoinPoker
Join the action now
Daily Rewards • Splash Pots • CoinRaces
Adding JButton at runtime (java)

      
m