0

I need to take all the textField, comboBox, jlst, and jslider input from the first frame and make it into a summary in the second frame after the Submit button is picked. I can't figure out the best way to do this. My assignment requires a popup second frame with a textArea and exit button that brings you back to the first frame.

import javax.swing.*;

import java.awt.*;
import java.awt.event.*;
import java.util.Hashtable;

public class Ch10Asg extends JFrame
{
private String[] flightNames = {"342", "4324", "939", "104", "222"};
private String[] cardTypes ={"Mastercard", "Visa", "American Express", "Discover"};
private String[] Dates ={"8/1/2013", "8/2/2013", "8/3/2013", "8/4/2013", "8/5/2013"};
private JTextField jtfFirst = new JTextField(10); 
private JTextField jtfLast = new JTextField(10);
private JTextField jtfAddress = new JTextField(15);
private JTextField jtfCity = new JTextField(15); 
private JTextField jtfState = new JTextField(2);
private JTextField jtfZip = new JTextField(5);
private JTextField jtfCard = new JTextField(16);
private JComboBox jcbo = new JComboBox(flightNames);
private JComboBox jcbcctype = new JComboBox(cardTypes);
private JList jlst = new JList(Dates);
private JSlider jsldHort = new JSlider(JSlider.HORIZONTAL,0,4,0);
private JButton jbtExit = new JButton ("EXIT");
private JButton jbtClear = new JButton ("CLEAR");
private JButton jbtSubmit = new JButton ("SUBMIT");
private JTextField jtfStart = new JTextField();
private JTextField jtfEnd = new JTextField();

public Ch10Asg() 
{
    JPanel p1 = new JPanel(new GridLayout(3,1));
    p1.add(new JLabel("First Name")); 
    p1.add(jtfFirst); 
    p1.add(new JLabel("Last Name")); 
    p1.add(jtfLast); 
    p1.add(new JLabel("Address")); 
    p1.add(jtfAddress); 
    p1.add(new JLabel("City")); 
    p1.add(jtfCity); 
    p1.add(new JLabel("State"));
    p1.add(jtfState);
    p1.add(new JLabel("ZIP")); 
    p1.add(jtfZip);

    JPanel p2 = new JPanel(new GridLayout(3,1));
    p2.add(new JLabel("Select Flight Number"));
    p2.add(jcbo);
    p2.add(new JLabel("Select Date"));
    p2.add(jlst);
    p2.add(new JLabel("Select Time"));

    jsldHort.setMajorTickSpacing(1);
    jsldHort.setPaintTicks(true);

    Hashtable<Integer, JLabel> labels = new Hashtable<Integer, JLabel>();
    labels.put(0, new JLabel("4:00am"));
    labels.put(1, new JLabel("5:00am"));
    labels.put(2, new JLabel("8:00am"));
    labels.put(3, new JLabel("11:00am"));
    labels.put(4, new JLabel("5:00pm"));
    jsldHort.setLabelTable(labels);
    jsldHort.setPaintLabels(true);
    p2.add(jsldHort);

    JPanel p4 = new JPanel(new GridLayout(3,1));
    p4.add(new JLabel("Starting City"));
    p4.add(jtfStart);
    p4.add(new JLabel("Ending City"));
    p4.add(jtfEnd);
    p4.add(new JLabel("Select Card Type"));
    p4.add(jcbcctype);
    p4.add(new JLabel("Enter Number"));
    p4.add(jtfCard);
    p4.add(jbtExit);
    p4.add(jbtClear);
    p4.add(jbtSubmit);



    add(p1, BorderLayout.NORTH);
    add(p2, BorderLayout.CENTER);
    add(p4, BorderLayout.SOUTH);




    jbtExit.addActionListener(new ActionListener()
    {@Override
        public void actionPerformed(ActionEvent arg0) {
            // TODO Auto-generated method stub
            System.exit(0);

        }});

    jbtClear.addActionListener(new ActionListener()
    {@Override
        public void actionPerformed(ActionEvent arg0) {
            // TODO Auto-generated method stub
        jtfFirst.setText("");
        jtfLast.setText(""); 
        jtfAddress.setText("");
        jtfCity.setText(""); 
        jtfState.setText(""); 
        jtfZip.setText(""); 
        jtfCard.setText("");
        }});



    jbtSubmit.addActionListener(new ActionListener()
    {@Override
        public void actionPerformed(ActionEvent arg0) {
            new Infopane(jtfFirst.getText());
            //dispose();
        }});





}//ENDOFCONSTRUCTOR

public static void main(String[] args) 
{
    Ch10Asg frame = new Ch10Asg();
    frame.pack();
    frame.setLocationRelativeTo(null); // Center the frame
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setTitle("Flights");
    frame.setVisible(true); 
    frame.setSize(700,450);


}//ENDOFMAIN

}//ENDOFCLASS




class Infopane extends JFrame
{

private JTextArea Info = new JTextArea();
private JButton jbtExit1 = new JButton ("EXIT");


public Infopane(String jtfFirst)
{
JPanel s1 = new JPanel();
add(Info, BorderLayout.NORTH);
//Info.setFont(new Font("Serif", Font.PLAIN, 14));
Info.setEditable(false);
JScrollPane scrollPane = new JScrollPane(Info);
//setLayout(new BorderLayout(5,5));
add(scrollPane);
add(jbtExit1, BorderLayout.SOUTH);


pack();
setLocationRelativeTo(null); // Center the frame
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setTitle("Flight Summary");
setVisible(true);   
setSize(700,450);

jbtExit1.addActionListener(new ActionListener()
{@Override
    public void actionPerformed(ActionEvent arg0) {
        System.exit(0);
    }});




}


}
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
Kyle
  • 33
  • 3
  • 9
  • Try to give more accurate problems, what have you tried? What portion of code is giving you trouble? – Bono Jul 27 '13 at 15:04
  • The problem I am having is getting anything from the first frame to show up in the textArea of the second. I was given an example to do it similar to what I have in this code. – Kyle Jul 27 '13 at 15:07
  • 1
    See [The Use of Multiple JFrames, Good/Bad Practice?](http://stackoverflow.com/a/9554657/418556) Please direct the attention of whoever set this nonsense assignment to it. – Andrew Thompson Jul 27 '13 at 15:07
  • Assignment directions "8. The informational frame uses a text area to display all information and contains a close button to exit the frame and return to the main window when user is done viewing information." – Kyle Jul 27 '13 at 15:11
  • Confirm with the lecturer that by 'frame' they mean a `JFrame` as opposed to a 'free floating, top level window'. – Andrew Thompson Jul 27 '13 at 15:23
  • @AndrewThompson "a submit button when the user clicks submit, a new window should pop up using a text area to display all information in nicely formatted form." He has always been very clear and great with directions so I do feel like I am missing something here. Here he says "window" and then further down he says "informational frame" – Kyle Jul 27 '13 at 15:34
  • OK - it might be he is using the generic terms for 'window' & 'frame'. I would regard a `JDialog` as being one. Note that `JDialog` actually has a `java.awt.Window` in its ancestor hierarchy. `JDialog` does extend from `Window`. Using a modal `JDialog` (or a `JOptionPane`) would make this task easy, and would make for a much better GUI. I suggest you use one of those. If he objects (I suspect not), point out to him, what I pointed out to you. – Andrew Thompson Jul 27 '13 at 15:46
  • @AndrewThompson It is working well but it is displaying the number of the comboboxes, jlist, and jslider rather than the String that is attached to that number. For example it says "Flight #: 2" when it should say "Flight #: 4324" It's not pulling the actual label. Is it because I created the private strings at the top of the program? That's how it showed in the book. – Kyle Jul 27 '13 at 17:54

1 Answers1

2

Suggestions:

  • The second window shouldn't be a second JFrame but rather a modal JDialog since it truly is a dialog of the first window. This way, you don't have to fret about the second window closing the entire application when it is closed.
  • You already have an idea of what you should be doing -- passing information from one object to another -- since you're passing the String held by the first window's first name's JTextField into the second class: jtfFirst.getText()
  • Instead of passing just one JTextField's text, consider creating a third data class that holds all the information that you want to pass from one class to the other, create an object of this in the submit's ActionListener, and pass it into the new class.
  • Alternatively, you could give your first class getter methods, and just pass an instance of the first object into the second object, allowing the second object to extract info from the first by calling its getter methods.

Edit

For example here are snippets of a code example that works. The main JPanel is called MainPanel:

class MainPanel extends JPanel {

   // my JTextFields
   private JTextField fooField = new JTextField(10);
   private JTextField barField = new JTextField(10);

   public MainPanel() {
      add(new JLabel("Foo:"));
      add(fooField);
      add(new JLabel("Bar:"));
      add(barField);
      add(new JButton(new AbstractAction("Submit") {
         {
            putValue(MNEMONIC_KEY, KeyEvent.VK_S);
         }
         @Override
         public void actionPerformed(ActionEvent evt) {
            DialogPanel dialogPanel = new DialogPanel(MainPanel.this);

            // code deleted....
            // create and show a JDialog here
         }
      }));
      add(new JButton(new AbstractAction("Exit") {
         {
            putValue(MNEMONIC_KEY, KeyEvent.VK_X);
         }
         @Override
         public void actionPerformed(ActionEvent e) {
            Window win = SwingUtilities.getWindowAncestor((JButton)e.getSource());
            win.dispose();
         }
      }));
   }

   // getter methods to get information held in the fields.
   public String getFooText() {
      return fooField.getText();
   }

   public String getBarText() {
      return barField.getText();
   }
}

And in the dialog panel:

class DialogPanel extends JPanel {
   private JTextArea textarea = new JTextArea(10, 15);
   private MainPanel mainPanel;

   public DialogPanel(MainPanel mainPanel) {
      this.mainPanel = mainPanel; // actually don't even need this in your case

      // extract the information from the origianl class
      textarea.append("Foo: " + mainPanel.getFooText() + "\n");
      textarea.append("Bar: " + mainPanel.getBarText() + "\n");

      add(new JScrollPane(textarea));
      add(new JButton(new AbstractAction("Exit") {
         {
            putValue(MNEMONIC_KEY, KeyEvent.VK_X);
         }
         @Override
         public void actionPerformed(ActionEvent e) {
            Window win = SwingUtilities.getWindowAncestor((JButton)e.getSource());
            win.dispose();
         }
      }));
   }
}
Hovercraft Full Of Eels
  • 283,665
  • 25
  • 256
  • 373
  • "8. The informational frame uses a text area to display all information and contains a close button to exit the frame and return to the main window when user is done viewing information."<---assignment directions-------- I thought that by passing the String like that as an example it would work and that I was just missing something simple. – Kyle Jul 27 '13 at 15:13
  • @Kyle: you're not using the String that you're passing it. And yes you could pass in Strings into your other class's constructor, but that would make for one **huge** constructor parameter list. Just don't do it. I suggest that you pass in a reference, `this`, to the original object and give the original object getter methods. – Hovercraft Full Of Eels Jul 27 '13 at 15:20
  • I figured out that by leaving my code exactly the same and adding a "Info.setText(jtfFirst);" under the public Infopane it displayed that text in the textArea of the new frame. – Kyle Jul 27 '13 at 15:21
  • @Kyle: yes, that's great for one String, but again, you don't want a monster constructor parameter list. Please look at my sample code above to see what I'm suggesting. – Hovercraft Full Of Eels Jul 27 '13 at 15:27
  • It is working well but it is displaying the number of the comboboxes, jlist, and jslider rather than the String that is attached to that number. For example it says "Flight #: 2" when it should say "Flight #: 4324" It's not pulling the actual label. – Kyle Jul 27 '13 at 17:51
  • @Kyle: then you're extracting the information from the comboboxes wrong. Look at the the JComboBox API, look at the method you're using, and you'll see why it is giving you this result. Look again at the API for another similar method that will get you the *item* you need, call `toString()` on the object returned, and I'll bet things will work better. The API should be the first place to go to for similar problems. Luck. – Hovercraft Full Of Eels Jul 27 '13 at 17:56
  • I figured it all out except for the Jslider. I have setLabeltable(labels) and setPaintLabels(true). Then I called it using jsldHort.getValue(); and parameterized it as an Object. It still spits out the integer – Kyle Jul 27 '13 at 21:18