Results 1 to 12 of 12
  1. #1
    dottbin is offline Member
    Join Date
    Apr 2011
    Posts
    8
    Rep Power
    0

    Lightbulb call JFrame 2 from JFrame1: help

    hi all,
    I created with Netbeans a project with two java files:
    myproject
    -myframe.java
    -choose.java

    each java files work fine on standalone. Moreover in myframe.java I created JFrame1 and in choose.java I created JFrame2
    Now I need to launch JFrame2 from JFrame1(that is implemented in myframe.java).
    my code is on button "START" present in JFrame1 is:
    ************
    private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {

    try {
    String file = jTextField1.getText();
    InputStream out = new FileInputStream(file);
    Workbook book = WorkbookFactory.create(out);
    String sheet=book.getSheetName(0);
    JFrame w=new .choose();
    w.setVisible(true);
    .....

    ************
    but in this way I see only JFrame2 without buttons and Jtextfield.
    How can I do to solve my issue?
    I tried to solve it with thread but I didn't find anything on how to launch a JFrame with thread.

    Can someone help me please?

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    I'm not making much sense out of your question, and it may be hard to help you without code, but I'm afraid that a lot of your code will be auto-generated and thus very big and ugly. If you haven't done so, I suggest that you go through the Swing tutorials to learn how to create Swing GUI's by hand and without auto-generation since if you do this, you will understand Swing a lot better including using NetBeans to autogenerate your code.

    Luck.

  3. #3
    dottbin is offline Member
    Join Date
    Apr 2011
    Posts
    8
    Rep Power
    0

    Default

    Quote Originally Posted by Fubarable View Post
    I'm not making much sense out of your question, and it may be hard to help you without code, but I'm afraid that a lot of your code will be auto-generated and thus very big and ugly. If you haven't done so, I suggest that you go through the Swing tutorials to learn how to create Swing GUI's by hand and without auto-generation since if you do this, you will understand Swing a lot better including using NetBeans to autogenerate your code.

    Luck.
    hi Fubarable,
    thanks for your reply but believe me, I studied Java as you suggested but I decided to use Netbeans as it helps to create immediately JFrame.
    I try to explain better with code and images:

    JFrame1 is associated to file myframe.java as you can see from image:
    [IMG][/IMG]

    when I push butoom "CREA FILE" with follwing code:
    Java Code:
    private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {                                         
    
            try {
                String file = jTextField1.getText();
    InputStream out = new FileInputStream(file);
    Workbook book = WorkbookFactory.create(out);
    String sheet=book.getSheetName(0);
    JFrame w=new .choose();
    w.setVisible(true);
    where choose is the name of second file choose.java and this is the code:
    Java Code:
    public class choose extends javax.swing.JFrame {
    
        /** Creates new form choose */
        public choose() {
            initComponents();
        }
    
    ...
     public static void main(String args[]) {
            java.awt.EventQueue.invokeLater(new Runnable() {
               public void run() {
                    new choose().setVisible(true);
                }
    
            });
        }
    and JFrame2 is associated to file choose.java:
    [IMG][/IMG]

    So my question is:
    is it possible to launch JFrame2 when I click on buttom "CREA FILE"? or have I to create both JFrames in same file myframe.java?
    Last edited by dottbin; 04-06-2011 at 08:44 PM.

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    This may be a stupid question, but why do you even have a "JFrame2"/choose? Why not simply use one of the JOptionPanes to get a simple String input like this? And while I believe that you've done some studying, we all would benefit from further study, trust me. Also, you'll want to use the standard Java naming conventions when creating classes.

  5. #5
    dottbin is offline Member
    Join Date
    Apr 2011
    Posts
    8
    Rep Power
    0

    Default

    Quote Originally Posted by Fubarable View Post
    This may be a stupid question, but why do you even have a "JFrame2"/choose?
    I learned from my collegues at work the respect for each one, so even if someone asks to me a possible stupid question, I never said to him that his question may be a 'stupid question'

    Quote Originally Posted by Fubarable View Post
    Why not simply use one of the JOptionPanes to get a simple String input like this?
    You are right like I can use ajTextfield to solve it. But it is not my goal.

    Anyway I'm interested to understand if it is possible in Java to open a second Frame form (like I've done in VB.net). It is not matter what JFrame2 does, the matter is how to launch second JFrame.

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

    Default

    The same way you 'launch' any JFrame. Construct it and setVisible(true)

    db

  7. #7
    dottbin is offline Member
    Join Date
    Apr 2011
    Posts
    8
    Rep Power
    0

    Default

    Quote Originally Posted by Darryl.Burke View Post
    The same way you 'launch' any JFrame. Construct it and setVisible(true)
    db
    thanks for replay.
    I did as you said
    Java Code:
    JFrame w=new .choose();
    w.setVisible(true);
    but when I click on buttom, JFrame2 appears without buttom and JtextField as it should be (see by image posted). So I'm trying to understand how to launch this frame with its all components.

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

    Default

    To get better help sooner, post a SSCCE that clearly demonstrates your problem.

    db

  9. #9
    dottbin is offline Member
    Join Date
    Apr 2011
    Posts
    8
    Rep Power
    0

    Default

    Quote Originally Posted by Darryl.Burke View Post
    To get better help sooner, post a SSCCE that clearly demonstrates your problem.

    db
    I'll do it in evening.

    I posted as I understood what is problem.
    When I click on buttom1 present on JFrame1 in order to launch also JFrame2 (as when I click on buttom1 also a txt file is created ) , JFrame2 is freezed so I cannot see it.
    Meanwhile I want that JFrame1 is stopped, wait for value inserted in JFrame2 and then Jframe1 continues to create txt file.
    I found some informations that I need to use concurrency (in particular event dispatch thread) and it is difficult.
    My goal is in general to understand how I can launch a frame with a thread.

    Is it possible?

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

    Default

    I want that JFrame1 is stopped, wait for value inserted in JFrame2 and then Jframe1 continues to create txt file.
    Use a modal JDialog or a static method of JOptionPane that shows one.

    db

  11. #11
    dottbin is offline Member
    Join Date
    Apr 2011
    Posts
    8
    Rep Power
    0

    Default

    Quote Originally Posted by Darryl.Burke View Post
    Use a modal JDialog or a static method of JOptionPane that shows one.

    db
    Sorry Darryl,
    can I implement a list(multiselection) with several values with JDialog?
    as you and Fubarable said with JOptionPane I can insert only a single value:
    Java Code:
    String inputvalue=JOptionPane.showInputDialog("Insert Source RNCid");
    If your answer is yes, where can I find some example?
    Last edited by dottbin; 04-07-2011 at 09:43 PM.

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

    Default

    JOptionPane I can insert only a single value:
    That 'single value' can be any object, notably a component like JList or even a JPanel with a whole lot of components in it. Read the API and the tutorial.

    db

Similar Threads

  1. call by value and call by reference in java
    By sandeepsai39 in forum New To Java
    Replies: 7
    Last Post: 08-12-2010, 12:03 PM
  2. Replies: 5
    Last Post: 06-14-2010, 07:10 PM
  3. Passing data from one JFrame to another JFrame
    By tarami in forum New To Java
    Replies: 3
    Last Post: 08-06-2009, 06:44 PM
  4. Replies: 4
    Last Post: 04-23-2009, 12:02 AM
  5. Replies: 1
    Last Post: 01-30-2009, 07:44 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
  •