Dynamic joptionpane in Java: combobox

Asked

Viewed 45 times

0

Is it possible to make Joptionpane dynamic ? I’m having difficulty registering new entries in the countrys array[].

I’m using Java 1.8 sdk

Code

import javax.swing.JOptionPane;
import javax.swing.ImageIcon;

public class Javaapp {

    public static void main(String[] args) {

        ImageIcon icon = new ImageIcon("flag.png");

        String countrys[]={"India","America","Australia"};// tornar dinamico

        String name=(String)JOptionPane.showInputDialog(null,
                "Country name",
                "Country Confirmation",
                JOptionPane.PLAIN_MESSAGE,
                icon,
                countrys,
                countrys[0]);

        System.out.println(name);
    }
}

Codigo Pais

public class Countrys {

    private String nomepais;

    public String getNomepais() {
        return nomepais;
    }

    public void setNomepais(String nomepais) {
        this.nomepais = nomepais;
    }
    
    public Countrys(){

    }
}

1 answer

0


import javax.swing.JOptionPane;
import javax.swing.ImageIcon;
import java.util.ArrayList;
import java.util.List;

public class Javaapp {

    public static void main(String[] args) {

        ImageIcon icon = new ImageIcon("flag.png");
        List<String> countries2 = new ArrayList<String>();
        countries2.add("India");
        countries2.add("America");
        countries2.add("Australia");
        countries2.add("Brasil");

        String[] arrayteste = (String[]) countries2.toArray(new String[countries2.size()]);

        String name2=(String)JOptionPane.showInputDialog(null,
                "Country name",
                "Country Confirmation",
                JOptionPane.PLAIN_MESSAGE,
                icon,
                arrayteste,
                arrayteste[0]);
        System.out.println(name2);

    }
}

Browser other questions tagged

You are not signed in. Login or sign up in order to post.