Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

showInputDialog Question

Status
Not open for further replies.

JSET22

Programmer
Joined
May 31, 2003
Messages
1
Location
CA
HI,
I use a showInputDialog to display a comboBox for the user to choose an item in it. But i want him to be able to add a new Value in this combo box but i don't find how to do that.

Here is my code

Object[] possibleValues = {"1","2","3"};

Object selectedValue = JOptionPane.showInputDialog(null,"Enter IP", "Connection",JOptionPane.PLAIN_MESSAGE, null,possibleValues, possibleValues[0]);

Tahnks for your help
 
You will need to create your own option pane using JComboBox.


Sean McEligot
 
I have created a little test program. You can leave out the okButton and the cancelButton. I just added them to show you can have them if you want. If you want to use them, you have to add an ActionListener and ...make some other changes...

Code:
package com.ecc.tektips;

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

public class ComboOptionPane extends JPanel {

  private JComboBox combo;
  private JOptionPane pane;

  private String[] possibleValues = {
    "192.34.32.102", "192.34.32.120", "192.34.32.134", "192.34.32.135" };

  public ComboOptionPane() {
    combo = new JComboBox(possibleValues);
    combo.setEditable(true);
    this.add(combo); //, BorderLayout.CENTER);

    combo.addItemListener(new ItemListener() {
      public void itemStateChanged(ItemEvent evt) {
        System.out.println("Combo selected index = " + combo.getSelectedIndex());
        System.out.println("Combo selected item  = " + combo.getSelectedItem());
        getPane().setValue(combo.getSelectedItem());
      }
    });

  }

  public void setPane(JOptionPane pane) {
    this.pane = pane;
  }

  public JOptionPane getPane() {
    return pane;
  }

  public static void main(String[] args) {
    ComboOptionPane c = new ComboOptionPane();
    JButton okButton = new JButton("OK"); // Not used
    JButton cancelButton = new JButton("Cancel"); // Not used

    JOptionPane pane = new JOptionPane("Select or enter an IP address",JOptionPane.QUESTION_MESSAGE,JOptionPane.DEFAULT_OPTION,
          null, new Object[] { c, okButton, cancelButton });

    c.setPane(pane);
    JDialog d = pane.createDialog(null,"Select IP");
    d.setVisible(true);
    String rc = (String)pane.getValue();
    System.out.println("Selected value = " + rc);
  }

}
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top