How to pass numeric values for command line arguments to a JComboBox?

2

How about, first post here and I would like to know if you could help me with this question. I have to make a calculator through JComboBoxes in Java and what I need to do is pass numbers to perform the operations, by means of arguments in the command line and that these appear in the JComboBox. I have tried to do it in different ways but it marks me errors in the types of data or that the content can not be passed. I wanted to put the numbers in a static way and try the addition operation but I have an error again with the data types.

This is the code that I carry with the numbers in a static way:

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

public class Calculadora implements ActionListener{
JFrame marco;
JPanel ventana;
JLabel lbl1;
JButton boton;
JComboBox<String> cb2;
JComboBox<Integer> cb1;
JComboBox<Integer> cb3;
String input;
String[] operadores = {"+","-","*","/"};
Integer[] numerosOperaciones = {1,2,3,4,5,6,7,8,9,10};
int resultado;

public static void main(String args[]){
    /*
        Parte la entrada utilizando el método split(String delim) de la clase String
        con este método puedes partir una cadena en 2 cadenas nuevas a partir del
        delimitador que tú le mandes. Por ejemplo, si tienes la cadena a=3, puedes
        usar el método split en esa cadena con el delimitador "=" y hacer esto te crea
        2 cadenas nuevas, "a" y "3", puedes usar estas cadenas para pasarlas a tu JComboBox.

        Ejemplo de uso para este código:

        Para i = 0 hasta args.length:
            letras[i] = args[i].split("=")[0];
            numeros[i] = args[i].split("=")[1];

        y de esta manera ya partiste tu entrada en 2 cadenas nuevas que puedes usar
        en tus JComboBox
     */
    String input = args[0];
    Calculadora grafico = new Calculadora(input);
}

public void actionPerformed(ActionEvent e){
    if(cb2.getSelectedItem()=="+"){
        resultado = (Integer.parseInt(cb1.getSelectedItem()) + Integer.parseInt(cb3.getSelectedItem()));
        lbl1.setText("= " + resultado);
    }
}

public Calculadora(String input){
    this.input = input;

    // No necesitas poner el índice a la fuerza, el JComboBox siempre te selecciona
    // el primer elemento de la lista que tú le mandes

    marco = new JFrame("Calculadora");
    ventana = new JPanel();
    ventana.setLayout(new GridLayout(2,3));
    lbl1 = new JLabel("Resultado");
    boton = new JButton("Respuesta");
    cb1 = new JComboBox<>(numerosOperaciones);
    cb1.setSelectedIndex(0);
    cb1.addActionListener(this);
    cb2 = new JComboBox<>(operadores);
    // Lo mismo que con el cb1, no ocupas ponerle el índice a la fuerza, la clase te lo pone predeterminado
    // en la primera posición de la lista.
    cb2.setSelectedIndex(1);
    cb2.addActionListener(this);
    cb3 = new JComboBox<>(numerosOperaciones);
    cb3.setSelectedIndex(0);
    cb3.addActionListener(this);
    boton.addActionListener(this);

    ventana.add(cb1);
    ventana.add(cb2);
    ventana.add(cb3);
    ventana.add(boton);
    ventana.add(lbl1);
    marco.getContentPane().add(ventana);
    marco.setVisible(true);
    marco.setSize(400,400);
    marco.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}

How can I pass the numbers from the command line to the Jcombobox? Is there something I'm not taking into account?

Thank you.

    
asked by Space_Shift 01.03.2017 в 00:30
source

1 answer

2

Imagine that you start your app in the following way:

javaw -jar App.jar a=1 b=3 c=5 d=2

Then you can extract your fixes in the main method like:

public static main(String... args){
    String[] nombres = new String[args.length];
    Integer[] numeros = new Integer[args.length];
    for (int i = 0; i<args.length; i++){
        String[] data = args[i].split("="); // te deja con los componentes separados por "="
        nombres[i] = data[0];
        numeros[i] = Integer.valueOf(data[1]); // quizas quieres chequear el formato aquí
    }
    // mas adelante puedes crear tu combo box como
    JComboBox<String> nombreBox = new JComboBox(nombres);
    JComboBox<Integer> numeroBox = new JComboBox(numeros);

This code creates you combo boxes with:

nombres[] = { "a", "b", "c", "d" };
numeros[] = { 1, 3, 5, 2 };

If you need to know more, do not hesitate to comment.

    
answered by 01.03.2017 / 03:52
source