0

New to java and swing so please explain fully in your answers.

I have a class with 5 ints in it.

on my jframe i have a button, when you click it it should open a dialog with 5 comboboxes and an ok button.

I want to pass an int to the dialog to tell it how many itmens to put in the first combo box, the other 4 have fixed values.

on pressing the ok the dialog shoudl get the values form the comboboxes and fill out a class instance and return that instance.

i have read a lot of tutorials, i understand i need a custom class but dont know how to make it in netbeans, new class just gives me a code editon and no design view.

I dont know how to call the custom class dialog or to pass it a varialble.

please dont link me here iv read it at least a hundred times.

EDIT: what iv tried.

I tried making a cunton Dialog in netbeans but its built it into the main frame. I tried making it in a class but i dont know how to call the class and im sure the class code is wrong anyway.

RobertB
  • 4,592
  • 1
  • 30
  • 29
Skeith
  • 2,512
  • 5
  • 35
  • 57

2 Answers2

2

The show__Dialog methods of JOptionPane accept Object for the "message". This can be anything: namely a custom JPanel that you have created.

Create a JPanel, put your comboboxes in it, show it in a JOptionPane, then retrieve what you need.

Here is an extremely short example:

enter image description here

SwingUtilities.invokeLater(new Runnable() {
    @Override
    public void run() {
        JPanel    panel = new JPanel();
        JCheckBox check = new JCheckBox("Yes or no?");
        panel.add(check);

        JOptionPane.showMessageDialog(null, panel);
        JOptionPane.showMessageDialog(null,
            "You entered " + (check.isSelected() ? "yes" : "no") + "."
        );
    }
});

(The JCheckBox could have also been passed to showMessageDialog directly.)

The JOptionPane doc is fairly specific about how message is handled. Look under "Parameters: message". According to the doc, you could even pass it a JComboBox[].

Radiodef
  • 37,180
  • 14
  • 90
  • 125
  • does it have to be all in one source ? can i make the jpanel a seperate .java ? – Skeith Nov 21 '14 at 16:50
  • Yes, you can extend JPanel. Or make a factory method. As long as `message` is a subtype of `java.awt.Component`, JOptionPane will know what to do with it. – Radiodef Nov 21 '14 at 16:58
1

One way to do this would be a custom Dialog with a method that accepts your int and has a return value of the class you want to get back.

Now you create a new Instance of this dialog in the calling class. This should set up the Dialog as far as possible but not switch it visible yet.

Now as the triggering method is called you switch the combo boxes to the right settings,set the dialog visible,wait for the Ok button to be pressed, assemble the return object and send it back.

Community
  • 1
  • 1
Dawnkeeper
  • 2,844
  • 1
  • 25
  • 41
  • I dont think you understand, i have 8 years C++ expierience and about 20 minutes of java, I want to do excatly what you say, its perfect i just have abssoultley no clue at all how to do it. i dont know how to create an instance in the callign class, or how to give the dialog a return type – Skeith Nov 21 '14 at 15:56
  • creating a new instance would be something like 'MyDialog dlg = new MyDialog();' and further down 'MyReturnClass ret = dlg.getUserInput(intToCompleteSetup);' to get the values you want – Dawnkeeper Nov 21 '14 at 16:02
  • I might have misinterpreted what you meant: you should extend a *JFrame* for this. – Dawnkeeper Nov 21 '14 at 16:13
  • You really should not extend JFrame to use it as a dialog box. (For a custom dialog box from scratch, extend JOptionPane or JDialog.) http://stackoverflow.com/questions/9554636/the-use-of-multiple-jframes-good-bad-practice – Radiodef Nov 21 '14 at 16:26