Page 1 of 2 12 LastLast
Results 1 to 20 of 22
  1. #1
    sarwar1234 is offline Member
    Join Date
    Sep 2010
    Posts
    56
    Rep Power
    0

    Default Running a JPanel

    Hello,

    Actually I have a class MainPanel that implements a Jpanel..... and a class WindAnalysis with the main method....

    I am calling an object of that class in a main metho, But nothing appears on screen... No panel etc....

    The code of main class is:


    public class WindAnalysis extends JPanel {

    /**
    * @param args
    */
    public static void main(String[] args) {
    // Auto-generated method stub

    MainPanel mainPanel = new MainPanel();


    }

    }


    whereas the class MainPanel is as follows:

    public class MainPanel extends JPanel {

    private static JTabbedPane tabbedPane = null;

    public static final int WIDTH = 800;
    public static final int HEIGHT = 600;

    public static MainPanel instance = null;

    public OptionPanel optionPanel = null;

    public ChartPanel chartOnePanel[] = null; //chart one panel for wind 1 and 2
    public ChartPanel chartTwoPanel[] = null;
    public ChartPanel chartThreePanel[] = null;
    public ChartPanel chartFourPanel[] = null;
    public UserData userData = null;

    public MainPanel() {

    instance = this;

    setLayout(new BorderLayout());

    tabbedPane = new JTabbedPane();

    JTabbedPane windOnePane = new JTabbedPane();
    JTabbedPane windTwoPane = new JTabbedPane();

    userData = new UserData();

    chartOnePanel = new ChartPanel[2];
    chartTwoPanel = new ChartPanel[2];

    chartThreePanel = new ChartPanel[2];
    chartFourPanel = new ChartPanel[2];

    chartOnePanel[0] = new ChartPanel("Wind1chart1");
    chartOnePanel[1] = new ChartPanel("Wind2chart1");

    chartTwoPanel[0] = new ChartPanel("Wind1chart2");
    chartTwoPanel[1] = new ChartPanel("Wind2chart2");

    chartThreePanel[0] = new ChartPanel("Wind1chart3");
    chartThreePanel[1] = new ChartPanel("Wind2chart3");

    chartFourPanel[0] = new ChartPanel("Wind1chart4");
    chartFourPanel[1] = new ChartPanel("Wind2chart4");

    windOnePane.addTab("Frequency distribution",chartOnePanel[0]);
    windOnePane.addTab("Diurnal distribution",chartTwoPanel[0]);
    windOnePane.addTab("Prevalent wind direction",chartThreePanel[0]);
    windOnePane.addTab("Avg wind speed direction",chartFourPanel[0]);

    windTwoPane.addTab("Frequency distribution",chartOnePanel[1]);
    windTwoPane.addTab("Diurnal distribution",chartTwoPanel[1]);
    windTwoPane.addTab("Prevalent wind direction",chartThreePanel[1]);
    windTwoPane.addTab("Avg wind speed direction",chartFourPanel[1]);


    optionPanel = new OptionPanel();

    tabbedPane.setBorder(BorderFactory.createCompoundB order(BorderFactory
    .createEmptyBorder(15, 0, 0, 0), tabbedPane.getBorder()));

    // tabbedPane.addTab("Option",optionPanel);
    tabbedPane.addTab("User Data", userData);
    tabbedPane.addTab("Wind1 ", windOnePane);
    tabbedPane.addTab("Wind2", windTwoPane);


    setBorder(BorderFactory.createEmptyBorder(15, 15, 15, 15));

    add(tabbedPane, BorderLayout.CENTER);
    add(optionPanel, BorderLayout.NORTH);

    }

    }



    What should be done....

    I am very thankful for the cooperations....

    Best Regards,
    Sarwar

  2. #2
    berkeleybross's Avatar
    berkeleybross is offline Senior Member
    Join Date
    Feb 2010
    Location
    England
    Posts
    187
    Rep Power
    5

    Default

    You need to put panels etc inside a JFrame, and make the frame visible.

    Lesson: Using Swing Components (The Java™ Tutorials > Creating a GUI With JFC/Swing)

    berkeleybross

  3. #3
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,776
    Blog Entries
    7
    Rep Power
    21

    Default

    All JComponents should be stuck to a JFrame or JWindow or JDialog (a so called 'top level component'). They can't be displayed on their own.

    kind regards,

    Jos

    edit: too slow again, sigh ...
    Last edited by JosAH; 09-27-2010 at 04:03 PM.

  4. #4
    sarwar1234 is offline Member
    Join Date
    Sep 2010
    Posts
    56
    Rep Power
    0

    Default

    That worked....


    Great... Thanks.....

    And In order to make an executable file (I think jar file), do I need to add some thing... because after running it, I get .class files and they dont run directly.....

    As I am running this program in the Eclipse... so it is working...

    Any Idea....

    Many Thanks....


    Best Regards,
    Sarwar

  5. #5
    sarwar1234 is offline Member
    Join Date
    Sep 2010
    Posts
    56
    Rep Power
    0

    Default

    This is the new code and is working now...

    public class WindAnalysis{

    /**
    * @param args
    */
    public static void main(String[] args) {
    // Auto-generated method stub

    try {
    UIManager.setLookAndFeel(new WindowsLookAndFeel());
    }catch (Exception e) {
    }

    JFrame f = new JFrame("Wind Analysis");
    f.setSize(800, 600);

    MainPanel mainPanel = new MainPanel();

    f.add(mainPanel);

    f.setVisible(true);


    }

    }


    Many Thanks for the help....

  6. #6
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,224
    Rep Power
    20

    Default

    That code should strictly be in a SwingUtilities.invokeLater() call...can't guarantee the names there, though.

  7. #7
    sarwar1234 is offline Member
    Join Date
    Sep 2010
    Posts
    56
    Rep Power
    0

    Default

    Hello,

    I am trying to build an execuatble jar file for the application....

    I am follwing the method as given in the link below:

    FAQ How do I create an executable JAR file for a stand-alone SWT program? - Eclipsepedia

    But every time I get a jar file and i try to run it... It gives a warning/error window saying Fail to load main class... Any idea...

    or some other way to creat executable jar file for the application....

    Many Thanks....

    Best Regards,
    Sarwar

  8. #8
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,224
    Rep Power
    20

    Default

    Print the full text of the error.

  9. #9
    sarwar1234 is offline Member
    Join Date
    Sep 2010
    Posts
    56
    Rep Power
    0

    Default

    It says:

    Failed to load Main-Class manifest attribute from C:\User\....(jar file destination)


    Any idea....

    Do I need to creat the folder under the same Java project folder to get jar file.... because it says runtime folder


    Thanks

  10. #10
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,224
    Rep Power
    20

    Default

    Your Main-Class should point to your class, not physically, but as package.class.

    So "Main-Class: mypackage.myclass".

    Not "Main-Class: C:\Something\etc\mypackage\myclass".

  11. #11
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,899
    Rep Power
    25

    Default

    To get a better error message that you can copy and paste here, execute the java program in a command prompt window:
    java -jar YOURJARFILENAME

    Then copy all of the command prompt window and paste it here.
    To copy the contents of the command prompt window:
    Click on Icon in upper left corner
    Select Edit
    Select 'Select All' - The selection will show
    Click in upper left again
    Select Edit and click 'Copy'

    Paste here.

  12. #12
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,458
    Rep Power
    20

    Default

    Is it really easier to find how to post in garish colors that make unformatted code even more unreadable than it is to discover the correct way to post code?

    /rant

    db

  13. #13
    sarwar1234 is offline Member
    Join Date
    Sep 2010
    Posts
    56
    Rep Power
    0

    Default

    Hello...


    When I try to execute the jar file in command prompt.... It says

    Unable to access jar file WindAnalysis


    Note that the project also uses some external jar file libraries... And I inculde them in a subfolder in the same folder that contains my project jar file "windanalysis.jar"....

    I am very thankful for your cooperations.....

    Regards

  14. #14
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,899
    Rep Power
    25

    Default

    Please copy the full contents of the command prompt showing what you entered and the full error message.
    To copy the contents of the command prompt window:
    Click on Icon in upper left corner
    Select Edit
    Select 'Select All' - The selection will show
    Click in upper left again
    Select Edit and click 'Copy'

    Paste here.

  15. #15
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,458
    Rep Power
    20

    Default

    Norm is either incredibly patient, or colorblind ;)

  16. #16
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,899
    Rep Power
    25

    Default

    I ignore a lot of stuff. You're right about that first color. It is very annoying.

  17. #17
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,224
    Rep Power
    20

    Default

    In addition to what Norm has requested, also post the Manifest from you jar file.

  18. #18
    sarwar1234 is offline Member
    Join Date
    Sep 2010
    Posts
    56
    Rep Power
    0

    Default

    Hello....

    This is what I get while using the command prompt.....



    Directory of C:\Users\Logic Energy\Desktop\JarTest

    28/09/2010 01:09 <DIR> .
    28/09/2010 01:09 <DIR> ..
    28/09/2010 01:09 32,259 windspeed.jar
    28/09/2010 01:09 <DIR> windspeed_lib
    1 File(s) 32,259 bytes
    3 Dir(s) 81,739,833,344 bytes free

    C:\Users\Logic Energy\Desktop\JarTest>java -jar windspeed
    Unable to access jarfile windspeed

    C:\Users\Logic Energy\Desktop\JarTest>



    Just to make a few thing clear....
    Actually Its the same project.... At first It was divided in two packages under the one project "WindAnalysis".... Then just to avoid confusion, I have built exactly same project but within one default package and named that "WindSpeed" and so is the main class name "WindSpeed"......

    It runs in Eclipse.... Every thing is similar....

    In cmd, The sub folder windspeed_lib contains the external libraries jar files.... This is created when I choose the option Export> runnable jar in Eclipse.....

    One more thing... There is a folder called "resource" that contains image files / logos etc... and that is also used by the program.... Eclipse gives me error when that folder "resource" is not there.... But as soon as I copy and paste that folder in the "bin" folder of my project (in the workspace).... The project Runs... Do I need to copy and paste that folder in the folder JarTest that conatins that jar file "windspeed.jar" or what ...... ?????

    The manifest text file... well that is not in the target jar file folder "JatTest"... But yes that is in the folder "WindSpeedJar" that I created in Eclipse under my project.... and it contains the follwing text:

    Manifest-Version: 1.0
    Class-Path: swt.jar
    Main-Class: WindSpeed.WindSpeed



    Thanks a lot for your help....

    Regards,
    Sarwar

  19. #19
    sarwar1234 is offline Member
    Join Date
    Sep 2010
    Posts
    56
    Rep Power
    0

    Default

    Hello .....


    It has started working.....

    What I did is that

    1- I copied the swt.jar file in that folder as well.....

    2- I also copied the "resource" folder that contains the image files etc.. for project in that folder..

    3- Then I did the same thing.... Export> Runnable Jar.... (also selected the option "package required libraries into the runnable jar" )

    It is working Now.....


    Thanks very much for all your support.... :)

    Best Regards,
    Sarwar

  20. #20
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,224
    Rep Power
    20

    Default

    You don't need swt.jar.
    At least from the code you've shown us (I can't read the bizarre glowy greenness at the top) you don't use swt at all.

    This is why you shouldn't simply copy stuff unless you know what it is you're doing.

Page 1 of 2 12 LastLast

Similar Threads

  1. How do I add my JPanel
    By Chanbre in forum NetBeans
    Replies: 13
    Last Post: 08-06-2010, 11:04 PM
  2. Running a program in a JPanel
    By ttelloow in forum Advanced Java
    Replies: 5
    Last Post: 07-26-2010, 07:18 PM
  3. JPanel
    By Jitendra Shukla in forum AWT / Swing
    Replies: 11
    Last Post: 04-27-2010, 06:16 AM
  4. Replies: 0
    Last Post: 08-26-2009, 02:03 PM
  5. How to use Jpanel
    By Manfizy in forum NetBeans
    Replies: 0
    Last Post: 02-19-2009, 01:34 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •