0

My Program will allow the user to navigate between three forms. Main Menu, Customer information and a database.

I need the main menu objects to be usable with the Customer forms submit button. For example:

There is a selection of comboboxes and I want to:

String variable = jComboMainMenu1.getSelectedItem().toString;

How do I import the objects into the customer form in order to use the values selected?

Edit:

**The error I get is that combox is private to the jframe. There is no way of changing that, as netbeans won't allow me to?

Says //variable declarations - do not modify.

**I tried:

public String car = jComboBox1.getSelectedItem().toString(); (This will store the value from the combobox in a variable as you already know).

However swing seems to be SUPER restrictive, because even my jbutton for the frame is restricted to private and this means that the variable cannot be made public to be used with buttons on other frames.

What is frustrating is that it is restricting me the programmer from changing the code:

private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) { <--Can not be changed to public.

    new CustomerForm().setVisible(true);
    setVisible(false);

    String car = jComboBox1.getSelectedItem().toString();

}                                        


// Variables declaration - do not modify                                                 <-- can't be changed either.
private javax.swing.JButton jButton1;
private javax.swing.JComboBox jComboBox1;
private javax.swing.JComboBox jComboBox2;
private javax.swing.JComboBox jComboBox3;
private javax.swing.JComboBox jComboBox4;
private javax.swing.JComboBox jComboBox5;
private javax.swing.JLabel jLabel1;
private javax.swing.JLabel jLabel2;
private javax.swing.JLabel jLabel3;
private javax.swing.JLabel jLabel4;
private javax.swing.JLabel jLabel5;
private javax.swing.JLabel jLabel6;
private javax.swing.JTextField jTextField1;
// End of variables declaration                   

** I have tried editing the template in the IDE itself, however the means to change the grayed area seems to be heavily encapsulated.

Something like this should be very simple surely? :P

JSint
  • 101
  • 1
  • 11
  • 2
    See [The Use of Multiple JFrames, Good/Bad Practice?](http://stackoverflow.com/q/9554636/418556) It is most likely that one should be a `JFrame`, while all other top level containers are (possibly modal) `JDialog` windows. – Andrew Thompson Dec 04 '15 at 11:21
  • I will still have the same problem though. This time using a jDialog object in a jframe? There is also another problem. private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) { NewJDialogCarRental.setVisible(true); setVisible(false); } this code no longer works with jDialog lke it did with the frames – JSint Dec 04 '15 at 11:49
  • *"I will still have the same problem though"* That's why my comment was not an answer. It did not address the direct question. As a tip towards solving this though, it is all about OOP in general. The trick is either to keep or gain a reference to the field in question (or possibly just the model that provides the data) and use its methods to get the value. On a wider design note though, it is best to encapsulate the fields themselves and access their data via methods of the class in which they are declared. So really, this has less to do with Swing and more to do with OOP. – Andrew Thompson Dec 04 '15 at 11:54
  • Its a shame that this is such an issue and there is no simple solution to what is a simple problem. like declare the variables a public. call the form like a class. But it looks like I will just need to fit everything on one form which I know how to do already, just wanted to gain more skills. I'm doing this for uni. – JSint Dec 04 '15 at 12:03
  • 1
    *"Its a shame that this is such an issue and there is no simple solution to what is a simple problem."* You're estimation of 'simple' when it comes to a well designed, multi-window desktop app. may be ..naive. But a comment. OOP is the type of stuff a programmer should work out in simple command line apps - probably right around the time of 'loops and conditional structures'. Developing a GUI is often an order of magnitude more difficult than doing the equivalent thing in a command line app. – Andrew Thompson Dec 04 '15 at 12:13

1 Answers1

1

Edit: adjusted to combo box

If you want to have access to a combo box of your main form from your customer form, you can pass a reference to that combo box; for example when you create the customer form. (Another way to link the main form to the customer form is to add an action listener to the combo box in the main form that calls a method in the customer form that gets the selected item passed as a parameter.)

For example (using Java 8):

import java.awt.BorderLayout;
import javax.swing.*;

public class MainMenu {
    private CustomerForm customerForm;

    public static void main(String[] arguments) {
        SwingUtilities.invokeLater(() -> new MainMenu().createAndShowGui());
    }

    private void createAndShowGui() {
        JFrame frame = new JFrame("Stack Overflow");
        frame.setBounds(100, 100, 800, 600);
        frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);

        JPanel panel = new JPanel(new BorderLayout());
        String[] comboBoxItems = {"one", "two", "three"};
        JComboBox<String> jComboBox1 = new JComboBox<>(comboBoxItems);
        panel.add(jComboBox1, BorderLayout.NORTH);
        JButton button = new JButton("Do it!");
        panel.add(button, BorderLayout.SOUTH);
        frame.getContentPane().add(panel);

        Database database = new Database();
        customerForm = new CustomerForm(database, jComboBox1);

        button.addActionListener(actionEvent -> customerForm.doSomething());

        frame.setVisible(true);
    }
}


class CustomerForm {
    private Database database;
    private JComboBox<String> comboBox;

    public CustomerForm(Database database, JComboBox<String> comboBox) {
        this.database = database;
        this.comboBox = comboBox;
    }

    public void doSomething() {
        System.out.println();
        System.out.println("CustomerForm.doSomething");
        System.out.println("database: " + database);

        System.out.println("comboBox.getSelectedItem().toString(): "
                           + comboBox.getSelectedItem().toString());
    }
}


class Database {
}
Freek de Bruijn
  • 3,552
  • 2
  • 22
  • 28
  • I need to be able to use a combobox input from one frame in another frames submit button. Do you think you could explain what I need to do in detail, so I learn it please? – JSint Dec 04 '15 at 17:02
  • The example above uses a radio button menu item instead of a combo box, but the general idea is the same. You can provide access to a private field of a class by for example passing a reference to it to another object (shown above) or by creating a public getter method. – Freek de Bruijn Dec 04 '15 at 17:07