Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Dynamic control loading

Status
Not open for further replies.

chigley

Programmer
Sep 30, 2002
104
0
0
GB
I want to be able to load controls dynamically at runtime into a JPanel. Can someone give me a starter for 10?

Ultimately I want to create a diagramming interface using swing but the first hurdle is to just be able to load the controls dynamically, for example a jLabel.

Thanks

Charlie Benger-Stevenson
Hart Hill IT Ltd
 
I don't get the question. The only way to add controls is at runtime with add method.



Cheers,
Dian
 
OK, so lets say on my jFrame I have a button called LoadControl. Every time I click it, it creates a new instance of the jLabel class, names it appropriately eg Label1, Label2 etc etc and loads it onto the jFrame at a specific point (x,y)

Charlie Benger-Stevenson
Hart Hill IT Ltd
 
That's not all that helpful, and to be honest quite patronising. These forums are about helping others, not being like that. Shame on you!

Charlie Benger-Stevenson
Hart Hill IT Ltd
 
OK, so lets say on my jFrame I have a button called LoadControl. Every time I click it, it creates a new instance of the jLabel class, names it appropriately eg Label1, Label2 etc etc and loads it onto the jFrame at a specific point. (x,y)
Well - yes, that's possible.
Where is your problem?
How to write an eventhandler?
How to add the control?
How to keep track of the number of lables?
How to convert a counter into a String?

You like us to write whole Program?

don't visit my homepage:
 
add event handler to that button
In the event handler, JLabel jlab = new JLabel(); add jLab to your JPanel. You can use the reference to your Jpanel to add jLab.
You do not need load a class for creating a component, JLabel jlab = new JLabel();
For creating an object with the class name dynamically, you can use Class.forName().
To load a library, you can use System.load or System.loadLibrary

You should have a quick look in Sun tutorial in Swing so that you do not write the Swing in a more difficult way than it needs.
 
I have come up against another problem with this

Firstly I was using netbeans to create the form, so I decided to do it the long handed way and I got something working.

Basically the code looks like

Code:
JFrame frame = new JFrame("HelloWorldSwing");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        
        JPanel jp = new JPanel(null);
JButton jb = new JButton("A button");
jp.add(b);
frame.getContentPane().add(jp);
frame.pack
frame.setVisible(true);

The problem is - as stated at the top of this thread, adding controls at run time and not design time.

So and controls are added after the lines

Code:
frame.pack
frame.setVisible(true);

have been executed. And this does not work.

Can someone shed some light on this. I just want to reiterate, some controls will be loaded at design time, and others at runtime. It is the runtime objects that are not displaying.

Thanks

Charlie Benger-Stevenson
Hart Hill IT Ltd
 
Firstly on a point of order, perhaps looking at somebodys profile would be good before charging in.

Anyway, I don't think you described what you wanted in the first instance too well. The last post gives me a better clue as to what you want.

Once you have done frame.setVisible(true); then its all done and in the hands of whatever you have designed.

So you will have to cater for the creation of new things before program hits this point.

Although I may have got the drift completely wrong.

[blue] A perspective from the other side!![/blue]

Cheers
Scott
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top