Content Pane Problem

Greetings!

I'm facing two related problems in developing a Java Applet.

My applet class extends JApplet.

1) The current default layout of the whole window (not individual

components) seems to be a flowLayout. I'd like to set the layout to a

gridBagLayout, however the setLayout command does not seem to be

understood (my compiler is eclipse, JRE 1.5.0 and JDK 5.0), so I

cannot get the following code to work, no specific error msg

generated:

getContentPane.setLayout(new GridBagLayout);

2) similar problem, cannot setBackground or setForeground of the

overall pane.

Please Advise, thanks.

-Darum

[683 byte] By [Daruma] at [2007-11-27 10:59:29]
# 1

>getContentPane.setLayout(new GridBagLayout);

This is not valid java

> similar problem, cannot setBackground or

> setForeground of the

> overall pane.

post your code

tjacobs01a at 2007-7-29 12:23:25 > top of Java-index,Desktop,Core GUI APIs...
# 2

The essentials of the code in question:

public class STMapplet extends JApplet implements ActionListener{

getContentPane.setLayout(new GridBagLayout());

}

The whole code:

public class STMapplet extends JApplet implements ActionListener{

//Creat Public Text Area with 10 rows, 5 columns

JTextArea text = new JTextArea(10,20);

//Set the contentPane (overall applett) layout

getContentPane.setLayout(new GridBagLayout());

//Generate image public object and set location

Image picture;

int imageX = 175;

int imageY = 50;

//frame specs, # of frames, time between frames (ms)

int NUM_FRMS = 65;

int MAX_FRMS = 65;

long FRMRATE = 7;

//Declare variables

boolean highV = false;

boolean lowV = false;

boolean highI = false;

boolean lowI = false;

// Declare Public image URLS, will be established later during paint method

// Use the same URLs for LILV and HIHV

URL hVhI;

URL hVlI;

URL lVhI;

//Border URLs

URL IMGBorderTop;

URL IMGBorderLeft;

URL IMGBorderRight;

URL IMGBorderBottom;

Image BORDERTOP;

Image BORDERLEFT;

Image BORDERRIGHT;

Image BORDERBOTTOM;

//Declare Public Buttons

JButton highVButton;

JButton lowVButton;

JButton highIButton;

JButton lowIButton;

JButton SCAN;

/**

* constraint construction for layout

*

* addGB adds a component to the panel that has a grid bag layout

*/

GridBagConstraints constraints = new GridBagConstraints();

void addGB(JPanel panel, Component component, int x, int y){

constraints.gridx =x;

constraints.gridy = y;

panel.add(component, constraints);

}

/**

* Build User Interface

*/

public void init()

{

//Generate buttons for each of the conditions

SCAN = new JButton("Begin Scanning");

highVButton = new JButton("1.00");

lowVButton = new JButton("0.07");

highIButton = new JButton("2.00");

lowIButton = new JButton("0.10");

//add action detection, the action listener is this

SCAN.addActionListener( this );

highVButton.addActionListener( this );

lowVButton.addActionListener( this );

highIButton.addActionListener( this );

lowIButton.addActionListener( this );

//create viewing panel with gridbag layout

JPanel panel = new JPanel(new GridBagLayout());

//create pane for text and add to content pane

getContentPane().add( "East", new JScrollPane (text));

//add buttons and headers to panel and add panel to pane

addGB(panel, new JLabel("Voltage (V)"),0,1);

addGB(panel, new JLabel("Current (nA)"),0,4);

addGB(panel, highVButton,0,3);

addGB(panel, lowVButton, 0,2);

addGB(panel, highIButton, 0,6);

addGB(panel, lowIButton,0,5);

addGB(panel, SCAN, 0, 0);

getContentPane().add("West" , panel);

//author

text.append("@Darin Bellisario\n");

}

/**

* Display image according to buttons pressed, called with repaint()

*/

public void paint(Graphics g){

//Draw Border

//Get URLs

try { IMGBorderTop = new URL("http://ase.tufts.edu/chemistry/sykes/Applets/STMapplet/IMGBorderTop.jpg");} catch (Exception e){}

try { IMGBorderRight = new URL("http://ase.tufts.edu/chemistry/sykes/Applets/STMapplet/IMGBorderRight.jpg");} catch (Exception e){}

try { IMGBorderLeft = new URL("http://ase.tufts.edu/chemistry/sykes/Applets/STMapplet/IMGBorderLeft.jpg");} catch (Exception e){}

try { IMGBorderBottom = new URL("http://ase.tufts.edu/chemistry/sykes/Applets/STMapplet/IMGBorderBottom.jpg");} catch (Exception e){}

// Draw

BORDERTOP = getImage(IMGBorderTop, "IMGBorderTop.jpg");

BORDERRIGHT = getImage(IMGBorderRight, "IMGBorderRight.jpg");

BORDERLEFT = getImage(IMGBorderLeft, "IMGBorderleft.jpg");

BORDERBOTTOM = getImage(IMGBorderBottom, "IMGBorderBottom.jpg");

g.drawImage(BORDERTOP,imageX - 49, imageY - 20, this );

g.drawImage(BORDERRIGHT,imageX + 613, imageY, this );

g.drawImage(BORDERLEFT,imageX - 49, imageY, this );

g.drawImage(BORDERBOTTOM,imageX, imageY + 613, this );

//Display Appropriate image (HIHV & LILV same image)

if( (highV == true && highI == true) || (lowV == true && lowI == true) ){

for (int i = 1; i<= NUM_FRMS; i++){

//Display first part of pic

try { hVhI = new URL("http://ase.tufts.edu/chemistry/sykes/Applets/STMapplet/LILV&HIHV" + i + "of" + MAX_FRMS + "%20copy.jpg");

picture = getImage(hVhI, "LILV&HIHV" + i + "of" + MAX_FRMS + "%20copy.jpg");

g.drawImage(picture,imageX,imageY,this);

//wait

Thread.sleep(FRMRATE);

}catch (Exception e){System.out.println("the wait thing ****** up");}

}

} else if( (highV == true && lowI == true) ){

for (int i = 1; i<= NUM_FRMS; i++){

//Display first part of pic

try { hVlI = new URL("http://ase.tufts.edu/chemistry/sykes/Applets/STMapplet/LIHV" + i + "of" + MAX_FRMS + "%20copy.jpg");

picture = getImage(hVhI, "LIHV" + i + "of" + MAX_FRMS + "%20copy.jpg");

g.drawImage(picture,imageX,imageY,this);

//wait

Thread.sleep(FRMRATE);

}catch (Exception e){System.out.println("the wait thing ****** up");}

}

} else if( (lowV == true && highI == true) ){

for (int i = 1; i<= NUM_FRMS; i++){

//Display first part of pic

try { lVhI = new URL("http://ase.tufts.edu/chemistry/sykes/Applets/STMapplet/HILV" + i + "of" + MAX_FRMS + "%20copy.jpg");

picture = getImage(hVhI, "HILV" + i + "of" + NUM_FRMS + "%20copy.jpg");

g.drawImage(picture,imageX,imageY,this);

//wait

Thread.sleep(FRMRATE);

}catch (Exception e){System.out.println("the wait thing ****** up");}

}

}

}

//change image based on what buttons pressed. When an action is performed, the actionlistener (this) calls actionperformed

public void actionPerformed (ActionEvent e){

if (e.getSource() == highVButton){

highV = true;

lowV = false;

text.append("Voltage on High\n");

}

if (e.getSource() == lowVButton){

lowV = true;

highV = false;

text.append("Voltage on Low\n");

}

if (e.getSource() == highIButton){

lowI = false;

highI = true;

text.append("Current on High\n");

}

if (e.getSource() == lowIButton){

lowI = true;

highI = false;

text.append("Current on Low\n");

}

if (e.getSource() == SCAN){

text.append("Scanning\n");

repaint();

}

}

}

Many Thanks for Any Aid!

-Darum

Daruma at 2007-7-29 12:23:25 > top of Java-index,Desktop,Core GUI APIs...
# 3

I've also tried this, which in the same vein doesn't work :-(

public class STMapplet extends JApplet implements ActionListener{

JPanel contentPane = new JPanel(new GridBagLayout());

setContentPane(contentPane);

}

Daruma at 2007-7-29 12:23:25 > top of Java-index,Desktop,Core GUI APIs...
# 4

You need to put your code into a method

> public class STMapplet extends JApplet implements

> ActionListener{

Public STMapplet () {

> JPanel contentPane = new JPanel(new

> w GridBagLayout());

> setContentPane(contentPane);

>

> }

}

tjacobs01a at 2007-7-29 12:23:25 > top of Java-index,Desktop,Core GUI APIs...
# 5

It worked! Thank you so much!

Daruma at 2007-7-29 12:23:25 > top of Java-index,Desktop,Core GUI APIs...