GUI problem

Hi all,

Am attempting GUI's for the first time in a long while and am stuck on a problem that i am hoping to get some advice on. I have A class that allows a user to check a box and click 'search'.

What i want to happen is that the class frame being displayed is cancelled and another frame from a new class is opened which displays what was selected.

I hope this makes sense, but have posted my code which should help more.

******Main Class**********

import javax.swing.*;

import java.awt.*;

import java.awt.event.*;

publicclass HelpMainextends JFrameimplements ActionListener

{

private JFrame frame =new JFrame("");

final String str ="Select Map";

private JLabel label01 =new JLabel(str);

private JCheckBox cb01 =new JCheckBox();

private JButton btnSearch =new JButton("Search");

public HelpMain()

{

setSize(200, 150);

Container c = getContentPane();

c.setBackground(Color.white);

c.setLayout(new GridBagLayout());

GridBagConstraints gbc =new GridBagConstraints();

gbc.insets =new Insets(2, 2, 2, 2);

gbc.gridx = 0; gbc.gridy = 1;

c.add(options(), gbc);

gbc.gridx = 0; gbc.gridy = 2;

c.add(request(), gbc);

btnSearch.addActionListener(this);

}

public JPanel options()

{

JPanel jp2 =new JPanel();

jp2.setBackground(Color.white);

jp2.setLayout(new GridBagLayout());

GridBagConstraints gbc =new GridBagConstraints();

gbc.insets =new Insets(2, 2, 2, 2);

gbc.anchor = gbc.WEST;

gbc.gridx = 0; gbc.gridy = 0;

jp2.add((label01), gbc);

gbc.gridx = 1; gbc.gridy = 0;

cb01.setBackground(Color.white);

jp2.add((cb01), gbc);

return jp2;

}

public JPanel request()

{

JPanel jp3 =new JPanel();

jp3.add(btnSearch);

return jp3;

}

publicvoid actionPerformed(ActionEvent ae)

{

Container c = getContentPane();

Object source = ae.getSource();

if(source == btnSearch)

{

// Coding that needs to go in to send str to the next class to be displayed

}

}

publicstaticvoid main(String args[])

{

HelpMain hlp =new HelpMain();

hlp.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

hlp.setVisible(true);

}

}

******Secondary class*********

import javax.swing.*;

import java.awt.*;

import java.awt.event.*;

publicclass HelpSecextends JFrame

{

JFrame frame =new JFrame();

JLabel labelMap =new JLabel("You selected this from the previous frame: ");

public HelpSec()

{

setSize(350, 150);

Container c = getContentPane();

c.setBackground(Color.white);

c.setLayout(new GridBagLayout());

GridBagConstraints gbc =new GridBagConstraints();

gbc.insets =new Insets(2, 2, 2, 2);

gbc.gridx = 0; gbc.gridy = 0;

c.add(labelMap, gbc);

}

}

Hope someone out there can help me out on this,

Cheers Dave

[5354 byte] By [oeggumpie2005a] at [2007-11-26 22:01:23]
# 1

First of all both of your classes extend JFrame so why is the first line of code in each class:

JFrame frame = new JFrame();

Its not needed and is confusing.

Now, how do you display the original frame. Your code is:

HelpMain hlp = new HelpMain();

hlp.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

hlp.setVisible(true);

So why can't you use the same code to display the HelpSec frame?

camickra at 2007-7-10 10:41:26 > top of Java-index,Desktop,Core GUI APIs...
# 2

I have taken out the JFrame frame = new JFrame();

in both classes, thankyou for heads up there.

I am still not understanding of the second part of your reply. You state that I should use the same code to display the HelpSec, which I have placed in where i had the comments in the HelpMain code:

sec = new HelpSec();

sec.setVisible(true);

The error i get now is:

D:\Dave's Folder\HelpMain.java:15: cannot resolve symbol

symbol : class HelpSec

location: class HelpMain

HelpSec sec;

^

D:\Dave's Folder\HelpMain.java:70: cannot resolve symbol

symbol : class HelpSec

location: class HelpMain

sec = new HelpSec();

^

2 errors

Tool completed with exit code 1

How is it not recognising the MainSec class? What am I doing wrong here?

oeggumpie2005a at 2007-7-10 10:41:26 > top of Java-index,Desktop,Core GUI APIs...
# 3

Basically what i want to be able to do at the moment is; when button 'search' is pressed, i want another frame, with the contents of HelpSec.class to be shown.

Even with the tip about placing in the code, which i gather would have looked like:

HelpSec sec = new HelpSec();

sec.setVisible(true);

there are still errors. I have no idea how to go about doing what i wish but would like to be able to know how.

Can someone please help me,

Cheers Dave

oeggumpie2005a at 2007-7-10 10:41:26 > top of Java-index,Desktop,Core GUI APIs...
# 4
Is that class in the same directory or on you class path?"cannot resolve symbol..." means that javac can not find that class anywhereand so does not know what "HelpSec" is...
jEti182a at 2007-7-10 10:41:26 > top of Java-index,Desktop,Core GUI APIs...
# 5

Hi all, I am sorry to have posted the problem that keep on giving me an error. I have found out now that there was software complications surrounding the compiling of the classes.

Thankyou to camickr for his reesponse earlier, the code recommended worked fine:

HelpSec sec = new HelpSec();

sec.setVisible(true);

Now that we have that sorted out the second part to my question was, is it possible to pass the string "select map" into HelpSec and be displayed in the new frame. If so could someone give some information as to how to do it.

Thankyou Dave

oeggumpie2005a at 2007-7-10 10:41:26 > top of Java-index,Desktop,Core GUI APIs...
# 6

> Now that we have that sorted out the second part to

> my question was, is it possible to pass the string

> "select map" into HelpSec and be displayed in the new

> frame. If so could someone give some information as

> to how to do it.

if(source == btnSearch)

{

HelpSec hlp2 = new HelpSec();

hlp2.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

hlp2.setVisible(true);

}

this above should do it for displaying the second class frame......

now for passing

change the constructor of the second class, and add an argument to it

lets say, a string..... get this string fill from the selection of first frame, n when you pass this string to second class, put it to as text in your label in your second frame..... :)

if you still did not get what i am saying this, is your alerted code......

import javax.swing.*;

import java.awt.*;

import java.awt.event.*;

public class HelpMain extends JFrame implements ActionListener

{

private JFrame frame = new JFrame("");

final String str = "Select Map";

String str2;

private JLabel label01 = new JLabel(str);

private JCheckBox cb01 = new JCheckBox();

private JButton btnSearch = new JButton("Search");

public HelpMain()

{

setSize(200, 150);

Container c = getContentPane();

c.setBackground(Color.white);

c.setLayout(new GridBagLayout());

GridBagConstraints gbc = new GridBagConstraints();

gbc.insets = new Insets(2, 2, 2, 2);

gbc.gridx = 0; gbc.gridy = 1;

c.add(options(), gbc);

gbc.gridx = 0; gbc.gridy = 2;

c.add(request(), gbc);

btnSearch.addActionListener(this);

}

public JPanel options()

{

JPanel jp2 = new JPanel();

jp2.setBackground(Color.white);

jp2.setLayout(new GridBagLayout());

GridBagConstraints gbc = new GridBagConstraints();

gbc.insets = new Insets(2, 2, 2, 2);

gbc.anchor = gbc.WEST;

gbc.gridx = 0; gbc.gridy = 0;

jp2.add((label01), gbc);

gbc.gridx = 1; gbc.gridy = 0;

cb01.setBackground(Color.white);

jp2.add((cb01), gbc);

return jp2;

}

public JPanel request()

{

JPanel jp3 = new JPanel();

jp3.add(btnSearch);

return jp3;

}

public void actionPerformed(ActionEvent ae)

{

Container c = getContentPane();

Object source = ae.getSource();

if(source == btnSearch)

{

str2 = label01.getText();

HelpSec hlp2 = new HelpSec(str2);

hlp2.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

hlp2.setVisible(true);

// Coding that needs to go in to send str to the next class to be displayed

}

}

public static void main(String args[])

{

HelpMain hlp = new HelpMain();

hlp.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

hlp.setVisible(true);

}

}

/******Secondary class*********/

class HelpSec extends JFrame

{

JFrame frame = new JFrame();

JLabel labelMap;

public HelpSec(String str)

{

setSize(350, 150);

labelMap = new JLabel(str);

Container c = getContentPane();

c.setBackground(Color.white);

c.setLayout(new GridBagLayout());

GridBagConstraints gbc = new GridBagConstraints();

gbc.insets = new Insets(2, 2, 2, 2);

gbc.gridx = 0; gbc.gridy = 0;

c.add(labelMap, gbc);

}

}

Freakenstiena at 2007-7-10 10:41:26 > top of Java-index,Desktop,Core GUI APIs...