?
Solved

Newbie needs some help

Posted on 2003-03-18
9
Medium Priority
?
181 Views
Last Modified: 2010-03-31
Hey pro's

I'm pretty new to programming and I need a little help with my developing. So far i've been putting all my code into one class. That does not produce very "friendly" code. But I'm having some problems splitting my code into different classes and using them together.
An example: I've made a simple app called Manager wich when run are supposed to call a simple dialog-panel wich askes the user for login and password. This dialog I want to put in a sepatate class. This ain't working.
Code:
public class ZoonOnManager extends JPanel implements ActionListener
{
    .
    .
    public static void main(String[] args)
    {
    .
    .
    }
    public ZoonOnManager()
    {
        TestDialog testdialog = new TestDialog(this);
    }
}
//Dialog code
class TestDialog extends JDialog
 {
     // Instance attributes used in this dialog
     private     JFrame          parentFrame;
     private     JScrollPane     scrollPane1;


     // Dialog constructor
     public TestDialog( JFrame parentFrame )
     {
          // Make sure we call the parent
          super( parentFrame );

          // Save the owner frame in case we need it later
          this.parentFrame = parentFrame;

          // Set the characteristics for this dialog instance
          setTitle( "Test Dialog" );
          setSize( 200, 200 );
          setDefaultCloseOperation( DISPOSE_ON_CLOSE );


          // Create a panel for the components
          JPanel topPanel = new JPanel();
          topPanel.setLayout( new BorderLayout() );
          getContentPane().add( topPanel );

         
     }

}
I know this is elementary, but I can't seem to get a grip on it. Some input would be appreciated.
THNX
0
Comment
Question by:taniar
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
9 Comments
 
LVL 35

Expert Comment

by:TimYates
ID: 8159290
public ZoonOnManager()
{
  TestDialog testdialog = new TestDialog( this );
  testdialog.show() ;
}

.... and then...

 public TestDialog( JFrame parentFrame )
 {
   // Make sure we call the parent
   super( parentFrame, "login Window", true );
0
 
LVL 1

Accepted Solution

by:
pronane earned 80 total points
ID: 8159310
Im not sure on this, but if you are passing a frame do you not have to pass the frame or have that class extend Jframe, your zoonmanager class extends jpanel, but you are not using a jframe you are using a jpanel, so should you not be extending jframe when you pass "this".
0
 

Expert Comment

by:krazeegook
ID: 8159375
First of all, it's not a good practice to pass GUI components around as arguments.

Second, passing in a GUI component does not automatically display that component on the screen. You must:

add these two lines at the end of TestDialog Constructor after getContentPane().add( topPanel );

pack();
setVisible(true);

or

after creating the TestDialog object call show() as follow:
   
public ZoonOnManager()
{
   TestDialog testdialog = new TestDialog(this);
   testdialog.show()
}




Besides that, you have JFrame as argument for constructor of TestDialog.("public TestDialog( JFrame parentFrame )
"), but you're passing in a JPanel. ZoonOnManager should extend JFrame.

0
Percona Live Europe 2017 | Sep 25 - 27, 2017

The Percona Live Open Source Database Conference Europe 2017 is the premier event for the diverse and active European open source database community, as well as businesses that develop and use open source database software.

 

Author Comment

by:taniar
ID: 8159482
>TimYates

I'm getting compilation errors in ZoonOnManager class when doing what you posted. Cannot resolve symbol on this line: TestDialog testdialog = new TestDialog( this );
It doesnt seem to like the class TestDialog
     
0
 

Author Comment

by:taniar
ID: 8159603
Actually this error comes if I try the other solutions as well. Any ideas?
0
 
LVL 35

Expert Comment

by:TimYates
ID: 8159709
Like pronane says, you need to pass a JFrame to the constructor of JDialog

You are passing "this" which is an instance of JPanel

You need to pass the main application frame, so you can do this:

JFrame getParentFrame( Component c )
{
  while( ( c != null ) && !( c instanceof JFrame ) )
    c = c.getParent() ;
  return c ;
}

then, construct your dialog by doing this:

TestDialog testdialog = new TestDialog( getParentFrame( this ) );

0
 

Author Comment

by:taniar
ID: 8159724
I have to abandon my post, but please post any any possible solutions and I will get back to you.
0
 

Author Comment

by:taniar
ID: 8236893
Thnx, sorry about the late answer. I've been to hospital. Thnx to everyone else that replied to, good to see experienced programmers helping a wannabe along;).
0
 
LVL 35

Expert Comment

by:TimYates
ID: 8236905
hope ur better :-)
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…
Suggested Courses
Course of the Month9 days, 15 hours left to enroll

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question