Results 1 to 10 of 10
Like Tree5Likes
  • 1 Post By DarrylBurke
  • 1 Post By Fubarable
  • 2 Post By Fubarable
  • 1 Post By X75TIGER75X

Thread: Help with passing String[] from GUI class to GUI class

  1. #1
    X75TIGER75X is offline Member
    Join Date
    Apr 2012
    Posts
    19
    Rep Power
    0

    Default Help with passing String[] from GUI class to GUI class

    Hi everybody,
    I'm new here in Java Forums, I'm sorry if this question has already been answered before in the forums.

    I'm a fairly new to Java Programming, I've learned how to build a GUI class and ActionListeners and etc. I've learned a little about Exception and handling. But I didn't learn anything beyond that in my Programming class in my college.

    I'm writing a program that consists of a main GUI Frame class with 3 additional GUI Frame classes for custom pop-up dialogs, etc. When the button is clicked on the main GUI class, the main GUI class will create object instance of another GUI Frame that will ask for Folder Location, and options. Once the user is finished with setting on that GUI, user will hit "OK" and a String[] is supposed to be sent back to main GUI class by using getInformation() method in the class. The problem is, it seems like when GUI object is created, the main class keeps on going, invoked getInformation() and the other GUI class is not ready. I got a null String[]. Is there another way to get the String[] back to the main class?

    I wrote a sample code so you guys can understand the concept of what I'm trying to do

    Java Code:
    public class MainSoftwareGUI extends JFrame implements ActionListener{
    String[] information;
    //Constructor with GUI information goes here
    public void actionPerformed(ActionEvent action){
    if(action.getSource() == addButton){
    AddSoftwareGUI window = new AddSoftwareGUI(//necessary information to send);
    information = window.getInformation(); //Recieves String[]
    }//End of ActionPerformed method
    }//End of Class
    
    
    
    public class AddSoftwareGUI extends JFrame implements ActionListener{
    String[] information;
    public void actionPerformed(ActionEvent action){
    if(action.getSource() == okButton){
    //information changing
    information = new String[2];
    information[0] = "Information Import are supposed to";
    information[1] = "work";
    }
    }//End of ActionPerformed method
    public String[] getInformation(){
    return information;
    }//End of getInformation method
    }//End of Class

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

    Default Re: Help with passing String[] from GUI class to GUI class

    Make sure that the secondary GUI's, the ones that pop-up are modal dialogs such as a JDialog or a JOptionPane. Give them public getter methods, such as getFooTextFieldText() that returns the text held by the fooTextField, and after the dialog returns, query the class for the contents of its fields with the public methods that you've given it.

  3. #3
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,242
    Rep Power
    19

    Default Re: Help with passing String[] from GUI class to GUI class

    Don't use multiple JFrames. Use modal dialogs created by invoking the static methods of JOptionPane.
    How to Make Dialogs (The Java™ Tutorials > Creating a GUI With JFC/Swing > Using Swing Components)

    Post any Swing-related questions in the AWT/Swing section.

    db
    Fubarable likes this.
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  4. #4
    X75TIGER75X is offline Member
    Join Date
    Apr 2012
    Posts
    19
    Rep Power
    0

    Default Re: Help with passing String[] from GUI class to GUI class

    Quote Originally Posted by DarrylBurke View Post
    Don't use multiple JFrames. Use modal dialogs created by invoking the static methods of JOptionPane.
    How to Make Dialogs (The Java™ Tutorials > Creating a GUI With JFC/Swing > Using Swing Components)

    Post any Swing-related questions in the AWT/Swing section.

    db
    Thanks, you can move this thread to AWT/Swing section if you wish to.
    In meanwhile, I have another question. I was successful with Dialog Object way some of you guys just advised me then I ran to an obstacle. How can I make local ActionListener Object inside a method?
    I have a few local JRadioButton that I want to be kept local for memory management reasons.
    I've tried using this code
    Java Code:
    rb1.addActionListener(new ActionListener(){public void actionPerformed(ActionEvent ae){linktype = "J";});
    It could only accept final variables because it is a local anonymous class. Is there another way of making something like this?

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

    Default Re: Help with passing String[] from GUI class to GUI class

    Quote Originally Posted by X75TIGER75X View Post
    Thanks, you can move this thread to AWT/Swing section if you wish to.
    In meanwhile, I have another question. I was successful with Dialog Object way some of you guys just advised me then I ran to an obstacle. How can I make local ActionListener Object inside a method?
    I have a few local JRadioButton that I want to be kept local for memory management reasons.
    I've tried using this code
    Java Code:
    rb1.addActionListener(new ActionListener(){public void actionPerformed(ActionEvent ae){linktype = "J";});
    It could only accept final variables because it is a local anonymous class. Is there another way of making something like this?
    This doesn't make sense, nor does the use of local JRadioButtons for "memory management reasons". Please elaborate on the overall goal rather than how you're trying to solve it with code (because I'll bet there's a better way).

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

    Default Re: Help with passing String[] from GUI class to GUI class

    Quote Originally Posted by X75TIGER75X View Post
    I've tried using this code
    Java Code:
    rb1.addActionListener(new ActionListener(){public void actionPerformed(ActionEvent ae){linktype = "J";});
    It could only accept final variables because it is a local anonymous class. Is there another way of making something like this?
    By the way, the problem here is that inner classes cannot refer to local non-final variables. To solve this, consider making linktype a private field of the outer class.

  7. #7
    X75TIGER75X is offline Member
    Join Date
    Apr 2012
    Posts
    19
    Rep Power
    0

    Default Re: Help with passing String[] from GUI class to GUI class

    Quote Originally Posted by Fubarable View Post
    This doesn't make sense, nor does the use of local JRadioButtons for "memory management reasons". Please elaborate on the overall goal rather than how you're trying to solve it with code (because I'll bet there's a better way).
    Okay, from my experience of writing this program. I think that trying to pass the information between two classes is a pain in the ass. I want to write everything within in a class to make everything easier. I've had no problem creating Dialogs inside the method bodies but using ActionListener inside the class is problematic. actionPerformed method sits outside of the method where dialog is created. There are some elements like 3 JTextField and 2 JRadioButtons for an example, that I need to make it react to mouse click and be able to use getText(). In order to do that, I have to create those objects outside of the method body. That takes up some memory and makes my program to become messy. I don't want that. I want something like putting actionPerformed() method inside the method body with local variables. That would be helpful.
    Do you get the picture of my problem or do you need me to explain more?

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

    Default Re: Help with passing String[] from GUI class to GUI class

    No, that all makes sense, but for my money, I'd say just create another class to create a JPanel that holds your JTextfields and JRadioButtons, then give it "getter" methods to extract the information from these components, and then simply display it in a JOptionPane. For example, this class, PlayerEditorPanel.java, creates a JPanel that holds several JTextFields on it and has a getter method that allows retrieval of the information:

    Java Code:
    import java.awt.GridBagConstraints;
    import java.awt.GridBagLayout;
    import java.awt.Insets;
    import java.util.HashMap;
    import java.util.Map;
    
    import javax.swing.BorderFactory;
    import javax.swing.JLabel;
    import javax.swing.JPanel;
    import javax.swing.JTextField;
    
    class PlayerEditorPanel extends JPanel {
       private static final long serialVersionUID = 1L;
    
       enum FieldTitle {
          B_NUMBER("B Number"), FIRST_NAME("First Name"), LAST_NAME("Last Name");
          private String title;
    
          private FieldTitle(String title) {
             this.title = title;
          }
    
          public String getTitle() {
             return title;
          }
       };
    
       private static final Insets WEST_INSETS = new Insets(5, 0, 5, 5);
       private static final Insets EAST_INSETS = new Insets(5, 5, 5, 0);
       private Map<FieldTitle, JTextField> fieldMap = new HashMap<FieldTitle, JTextField>();
    
       public PlayerEditorPanel() {
          setLayout(new GridBagLayout());
          setBorder(BorderFactory.createCompoundBorder(
                BorderFactory.createTitledBorder("Player Editor"),
                BorderFactory.createEmptyBorder(5, 5, 5, 5)));
          GridBagConstraints gbc;
          for (int i = 0; i < FieldTitle.values().length; i++) {
             FieldTitle fieldTitle = FieldTitle.values()[i];
             gbc = createGbc(0, i);
             add(new JLabel(fieldTitle.getTitle() + ":", JLabel.LEFT), gbc);
             gbc = createGbc(1, i);
             JTextField textField = new JTextField(10);
             add(textField, gbc);
    
             fieldMap.put(fieldTitle, textField);
          }
       }
    
       private GridBagConstraints createGbc(int x, int y) {
          GridBagConstraints gbc = new GridBagConstraints();
          gbc.gridx = x;
          gbc.gridy = y;
          gbc.gridwidth = 1;
          gbc.gridheight = 1;
    
          gbc.anchor = (x == 0) ? GridBagConstraints.WEST : GridBagConstraints.EAST;
          gbc.fill = (x == 0) ? GridBagConstraints.BOTH
                : GridBagConstraints.HORIZONTAL;
    
          gbc.insets = (x == 0) ? WEST_INSETS : EAST_INSETS;
          gbc.weightx = (x == 0) ? 0.1 : 1.0;
          gbc.weighty = 1.0;
          return gbc;
       }
    
       public String getFieldText(FieldTitle fieldTitle) {
          return fieldMap.get(fieldTitle).getText();
       }
    
    }
    I can then demonstrate using this by throwing an object of the class above into a JOptionPane, and then when the option pane returns, extract the information that was placed into the JPanel by calling the getter methods:

    Java Code:
    import java.awt.*;
    import java.awt.event.*;
    
    import javax.swing.*;
    
    @SuppressWarnings("serial")
    public class ComplexOptionPane extends JPanel {
       private PlayerEditorPanel playerEditorPanel = new PlayerEditorPanel();
       private JTextArea textArea = new JTextArea(12, 30);
    
       public ComplexOptionPane() {
          textArea.setEditable(false);
          textArea.setFocusable(false);
          textArea.setFont(new Font(Font.MONOSPACED, Font.PLAIN, 16));
          JPanel bottomPanel = new JPanel();
          bottomPanel.add(new JButton(new AbstractAction("Get Player Information") {
    
             @Override
             public void actionPerformed(ActionEvent arg0) {
                int result = JOptionPane.showConfirmDialog(null, playerEditorPanel,
                      "Edit Player JOptionPane", JOptionPane.OK_CANCEL_OPTION,
                      JOptionPane.PLAIN_MESSAGE);
                if (result == JOptionPane.OK_OPTION) {
                   for (PlayerEditorPanel.FieldTitle fieldTitle : PlayerEditorPanel.FieldTitle
                         .values()) {
                      textArea.append(String.format("%10s: %s%n",
                            fieldTitle.getTitle(),
                            playerEditorPanel.getFieldText(fieldTitle)));
                   }
                }
             }
          }));
          setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));
          setLayout(new BorderLayout(5, 5));
          add(new JScrollPane(textArea), BorderLayout.CENTER);
          add(bottomPanel, BorderLayout.PAGE_END);
       }
    
       private static void createAndShowGui() {
          ComplexOptionPane mainPanel = new ComplexOptionPane();
    
          JFrame frame = new JFrame("ComplexOptionPane");
          frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
          frame.getContentPane().add(mainPanel);
          frame.pack();
          frame.setLocationByPlatform(true);
          frame.setVisible(true);
       }
    
       public static void main(String[] args) {
          SwingUtilities.invokeLater(new Runnable() {
             public void run() {
                createAndShowGui();
             }
          });
       }
    }
    X75TIGER75X likes this.

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

    Default Re: Help with passing String[] from GUI class to GUI class

    The other option is to use a private inner class which will give you access to the outer classes class fields, but again, I like the cleanliness and re-usability of making a separate stand-alone class.
    DarrylBurke and X75TIGER75X like this.

  10. #10
    X75TIGER75X is offline Member
    Join Date
    Apr 2012
    Posts
    19
    Rep Power
    0

    Default Re: Help with passing String[] from GUI class to GUI class

    I tried your example and it worked! Thank you!
    Last edited by X75TIGER75X; 05-01-2012 at 07:34 PM.
    Fubarable likes this.

Similar Threads

  1. Replies: 3
    Last Post: 04-27-2012, 02:41 PM
  2. Replies: 0
    Last Post: 03-27-2011, 05:49 AM
  3. please help with passing variables between class's
    By jasonwucinski in forum New To Java
    Replies: 4
    Last Post: 02-11-2011, 01:27 AM
  4. Replies: 8
    Last Post: 04-14-2010, 05:49 AM
  5. passing text from a class to a gui class
    By rob in forum AWT / Swing
    Replies: 2
    Last Post: 02-13-2009, 11:04 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
  •