Results 1 to 10 of 10
  1. #1
    cfc1252 is offline Member
    Join Date
    Jan 2012
    Posts
    11
    Rep Power
    0

    Default Opening new windows

    I am playing around with GUI components in java. I have a main JFrame and when a button is clicked I want to open a new window. Do you create the new JFrame in the action Performing method or somewhere else? What is the correct way to deal with this? Thanks

  2. #2
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default Re: Opening new windows

    You would probably want a JDialog of some sort. The main emphasis on the How to Make Dialogs page of Oracle's Tutorial is on simple dialogs using JOptionPane, but JDialog is also discussed.

    JDialog is a subclass of Window, so it can be used to some extent like JFrame. It is also a subclass of Dialog which gives it some useful behaviour: you can optionally make it modal, and it will do the right thing when the main window is minimised etc.

    Multiple JFrames are quite possible, too. You have to make sure you handle what happens when a window is closed.

    asctionPerformed() is one place you could create a Window instance. If the window is displaying state that you want to be persistent in some way (a bunch of options set by the user, eg. And you want them to be in the state the user left them when the window was last used.) then you can create the window elsewhere and have an instance variable which you use to setVisible(true/false) as you wish.

  3. #3
    cfc1252 is offline Member
    Join Date
    Jan 2012
    Posts
    11
    Rep Power
    0

    Default Re: Opening new windows

    Thank you for the reply! That was what I was looking for.

    Off topic now: I have an arraylist of Cars. I am trying to display an item out of the arraylist in JTextArea. But the arraylist has a generic of Car. Hence why I am having problems displaying an item from the arraylist. The error I get is that the text in the the JTextArea must be type String not Car. I have tried to the toString() method but it still had the same problem.

    How can i get around this?

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

    Default Re: Opening new windows

    What are you seeing in the JTextArea when you are using the toString()?

  5. #5
    cfc1252 is offline Member
    Join Date
    Jan 2012
    Posts
    11
    Rep Power
    0

    Default Re: Opening new windows

    It won't compile. The error says"The method setText(String) in the type JTextComponent is not applicable for the arguments (Car)"

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

    Default Re: Opening new windows

    WHich means you are not using toString().
    You're trying to pass the Car object into that method, not a String representation of it.

  7. #7
    cfc1252 is offline Member
    Join Date
    Jan 2012
    Posts
    11
    Rep Power
    0

    Default Re: Opening new windows

    *Facepalm* It works now. I can see why it wasn't converting it now! Thank you for pointing it out.

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

    Default Re: Opening new windows

    Code blindness is a terrible thing, but we all get afflicted with it now and again...

  9. #9
    cfc1252 is offline Member
    Join Date
    Jan 2012
    Posts
    11
    Rep Power
    0

    Default Re: Opening new windows

    Off topic again: how can you update the main GUI screen? i.e. I have a JLabel that shows total number of cars in arraylist. When new cars are added, how can I update that JLabel?

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

    Default Re: Opening new windows

    Quote Originally Posted by cfc1252 View Post
    Off topic again: how can you update the main GUI screen? i.e. I have a JLabel that shows total number of cars in arraylist. When new cars are added, how can I update that JLabel?
    Cleaner I think is to display the list in a JList, and to update the JList, all you have to do is add or remove items from its model. Otherwise you'll need to set the text on the JLabel when your data is changed.

    Regardless, you'll need to listen for changes to know when to react, and for this consider using the appropriate listener type if you will be responding to a user interaction of some sort (for example an ActionListener if you're responding to a button press) or a a PropertyChangeListener if listening for changes to a class field.

Similar Threads

  1. opening an URL -
    By Sparky in forum New To Java
    Replies: 3
    Last Post: 02-07-2011, 03:58 PM
  2. Windows java vs Windows AIX
    By sysp in forum New To Java
    Replies: 0
    Last Post: 01-26-2011, 05:32 AM
  3. Replies: 4
    Last Post: 10-07-2009, 09:14 AM
  4. Opening the Application
    By jasmu in forum Advanced Java
    Replies: 6
    Last Post: 04-22-2009, 06:21 AM
  5. Opening a workspace twice
    By javaplus in forum Eclipse
    Replies: 0
    Last Post: 01-08-2008, 09:56 AM

Posting Permissions

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