Home > database >  tried saving a JOptionPane with a combo box result in a String, but it gives me "void cannot be
tried saving a JOptionPane with a combo box result in a String, but it gives me "void cannot be

Time:05-08

here is the code that gives me error:

private void btnLLamada1ActionPerformed(java.awt.event.ActionEvent evt) {                                            
    minutos = Integer.parseInt(JOptionPane.showInputDialog(null, "numero de minutos"));
    String[] lista = {"Local", "Internacional", "Celular"};
    JComboBox optionList = new JComboBox(lista);
    optionList.setSelectedIndex(0);
    tp = JOptionPane.showMessageDialog(null, optionList, "Que tipo de llamada va a usar",
            JOptionPane.QUESTION_MESSAGE);
}                                    

CodePudding user response:

JOptionPane.showMessageDialog(...) does not return anything hence it returns void. And nothing (void) cannot be converted into a string. You want to use the method showInputDialog. For inspiration look at (docs.oracle.com/javase/tutorial/uiswing/components/….

A short executable hack to show the dialogs:

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

public class Test {

    void initGui() {
        int minutos = Integer.parseInt(JOptionPane.showInputDialog(null, "Numero de minutos"));
        System.out.println("Numero de minutos: "   minutos);
        
        String[] lista = { "Local", "Internacional", "Celular" };
        JComboBox<String> optionList = new JComboBox<String>(lista);
        optionList.setSelectedIndex(0);
        String tipo = JOptionPane.showInputDialog(null, optionList, "Que tipo de llamada va a usar", JOptionPane.QUESTION_MESSAGE);
        System.out.println("tipo de llamada: "   tipo);
    }
    public static void main(String[] args) {
        javax.swing.SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                new Test().initGui();
            }
        });
    }
}
                
  • Related