Insert a JFrame into a JPanel or something

Hi,

I have a plotter that is being shown in JFrame. It has a main code.

I need to show this plot inside al applet, so I thought that maybe I could put it into a Jpanel inside an applet (the applet window contains more things than the plot).

Is this possible? How? are there any restrictions?

Is there any other option to do this?

[360 byte] By [kandua] at [2007-11-27 9:31:57]
# 1
You cannot add a JFrame into a panel (or anything else for that matter)you could add its contentPane into your panelIn general it's bad design to build UI components within JFrames or the other kinds of windows for exactly this reason
tjacobs01a at 2007-7-12 22:48:13 > top of Java-index,Desktop,Core GUI APIs...
# 2

Is there a way that I do what I have in my JFrame but inside a Panel or another structure instead?

I'll show you my code:

import java.awt.GridBagConstraints;

import java.awt.GridBagLayout;

import java.io.File;

import java.io.FileInputStream;

import java.io.FileNotFoundException;

import java.io.IOException;

import javax.swing.JFrame;

import javax.swing.SwingUtilities;

import ptolemy.plot.Plot;

//////////////////////////////////////////////////////////////////////////

//// TwoPlotExample

public class TwoPlotExampleinvento extends JFrame {

TwoPlotExampleinvento() {

// Instantiate the two plots.

Plot leftPlot = new Plot();

Plot rightPlot = new Plot();

// Set the size of the toplevel window.

setSize(400, 500);

leftPlot.setSize(350, 200);

leftPlot.setButtons(true);

leftPlot.setTitle("Se馻l en el tiempo");

leftPlot.setXLabel("Tiempo (segundos)");

leftPlot.setYLabel("Amplitud");

leftPlot.setMarksStyle("none");

leftPlot.setImpulses(true,1);

for (double i = -4; i <= 50; i=i+1) {

leftPlot.addPoint(1, i, 5 * Math.sin((Math.PI * i) / 20), false);

leftPlot.addPoint(2, i, 10 * Math.cos((Math.PI * i) / 25), true);

}

int T=5;

for (double i = -4; i <= T; i=i+1){

leftPlot.addPoint(4,i,2*i+1,true);

}

rightPlot.setButtons(true);rightPlot.setSize(350, 200);

for (int i = -4; i <= 50; i++) {

rightPlot.addPoint(0, i, 5 * Math.cos((Math.PI * i) / 20), true);

}

rightPlot.setTitle("Espectro de Frecuencia");

rightPlot.setXLabel("Frecuencia (Hz)");

rightPlot.setYLabel("Amplitud");

// Layout the two plots

GridBagLayout gridbag = new GridBagLayout();

GridBagConstraints c = new GridBagConstraints();

getContentPane().setLayout(gridbag);

// Handle the leftPlot

c.gridx = 0;

c.gridy = 0;

c.gridwidth = 1;

gridbag.setConstraints(leftPlot, c);

getContentPane().add(leftPlot);

// Handle the rightPlot

c.gridx = 0;

c.gridy = 1;

c.gridwidth = 1;

//c.fill = GridBagConstraints.BOTH;//Creo que esto obliga a que los graficos ocupen toda la pantalla

c.weightx = 1.0;

c.weighty = 1.0;

gridbag.setConstraints(rightPlot, c);

getContentPane().add(rightPlot);

setVisible(true);

}

/** main method called in a standalone java application.

* We simple instantiate this class, most of the work

* happens in the constructor.

*/

[b] public static void main(String[] args) {

// We execute everything in the Swing Event Thread, see

// the comment

Runnable doAction = new Runnable() {

public void run() {

new TwoPlotExampleinvento();

}

};[/b]

SwingUtilities.invokeLater(doAction);

}

}

kandua at 2007-7-12 22:48:13 > top of Java-index,Desktop,Core GUI APIs...
# 3
As you could see, the code has a main. Is there a way I can show it in another structure that lets me put it inside an applet? Could it be on a Jpanel? Is there any other structure you recommend?Thank you
kandua at 2007-7-12 22:48:14 > top of Java-index,Desktop,Core GUI APIs...
# 4
Use a design something like the example in replies 27 and 30 of this posting: http://forum.java.sun.com/thread.jspa?threadID=5145907&start=27
camickra at 2007-7-12 22:48:14 > top of Java-index,Desktop,Core GUI APIs...
# 5
Thanks
kandua at 2007-7-12 22:48:14 > top of Java-index,Desktop,Core GUI APIs...