简体   繁体   中英

Java array accessing it from another class and using as a combobox

My code is used to book a bus with jpanel, I need to add array from a second class to my first.The array needs to be pre-filled which I can do however. I don't know to access it from the second class. My code is to use arrays and turn them into a combo-box. I've done this with the others but I need to make an array in another and pass the array back to my first class and turn it into the combobox. I just need help with access it.

I just added the extra code but unsure. Can you show where it should go if I got it wrong otherwise it's going to take awhile. First class is OK 2nd class has lost of errors, not sure what's causing it this time.

代码2红线

first class:

package learning;
    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.*;
    import java.util.ArrayList;
    import java.util.Arrays;
    import java.util.List;


    @SuppressWarnings("unused")
    public class test {

        String[] items = {"Tipperary","Cork","Limerick","Dublin"};
        JComboBox c = new JComboBox(items);
        JButton b = new JButton("From");
        JLabel l = new JLabel("Choose orgin");

        JComboBox h = new JComboBox(items); // To  destination
        JButton f = new JButton("To");
        JLabel g = new JLabel("Choose destination");

        String[] items2 = {"window","aisle"};
        JComboBox m = new JComboBox(items2); 
        JButton  n = new JButton("Seat");
        JLabel  o = new JLabel("choose seat");

        String[] items3 = {"Single","return"};
        JComboBox x = new JComboBox(items3); 
        JButton  y= new JButton("Ticket");
        JLabel  z = new JLabel("choose Ticket");


        JComboBox xx = new JComboBox(); 
        JButton  yy = new JButton("Ticket");
        JLabel  zz = new JLabel("choose Ticket");


        public test(){

        frame();

        }
         public void frame(){

        JFrame wolf = new JFrame();//frame
        wolf.setVisible(true);
        wolf.setSize(350,350);
        wolf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE );

        JPanel p = new JPanel();

        p.add(c);//
        p.add(b);//
        p.add(l);//lable1
        p.add(h);//
        p.add(f);//
        p.add(g);//lable 2
        p.add(m);//
        p.add(n);//
        p.add(o);//lable 2
        p.add(x);//
        p.add(y);//
        p.add(z);//lable 2
        p.add(xx);//
        p.add(yy);//
        p.add(zz);//lable 2


        wolf.add(p);

        b.addActionListener(new ActionListener(){
            public void actionPerformed(ActionEvent e){
        String s = c.getSelectedItem().toString();
            l.setText(s);
            }
        });


         f.addActionListener(new ActionListener(){
                public void actionPerformed(ActionEvent e){
            String s = h.getSelectedItem().toString();
                g.setText(s);
                }
            });

         n.addActionListener(new ActionListener(){
                public void actionPerformed(ActionEvent e){
            String s = m.getSelectedItem().toString();
                o.setText(s);
                }
            });

         y.addActionListener(new ActionListener(){
                public void actionPerformed(ActionEvent e){
            String s = x.getSelectedItem().toString();
                z.setText(s);
                }
            });
             }
         {
         }
    public static void main(String[]args){

        nextstep myNextstep = new nextstep();
        List List = (myNextstep).getList();


        new test();

        }

    }

code 2:

     package learning;
    import java.util.List;
    import java.util.ArrayList;

    public class nextstep {
    private int number1 = 1;
    private int number2 = 2;
    private int number3 = 3;
    private int number4 = 4;
    private int number5 = 5;
    private int number6 = 6;
    private int number7 = 7;
    private int number8 = 8;
    private int number9 = 9;
    private int number10 = 10;
    private int number11 = 11;
    private int number12 = 12;
    private int number13 = 13;
    private int number14 = 14;
    private int number15 = 15;
    private int number16 = 16;

    public List <Integer> getList() { return list; }

    public nextstep() {
       list = new ArrayList<Integer>();
       list.add(number1);
       list.add(number2);
       list.add(number3);
       list.add(number4);
       list.add(number5);
       list.add(number6);
       list.add(number7);
       list.add(number8);
       list.add(number9);
       list.add(number10);
       list.add(number11);
       list.add(number12);
       list.add(number13);
       list.add(number14);
       list.add(number15);
       list.add(number16);
    }
    public List <Integer> getList() 
    { 
        return list; }
    }

It seems to be just a matter of creating an object of the second class and request the list with the provided method:

nextstep myNextstep = new nextstep();
List<Integer> myList = myNextstep.getList();

JComboBox c = new JComboBox(items); //this is wrong you need to set a new combobox model with your arrays do it like this change the other comboboxes accordingly

c.setModel(new DefaultComboBoxModel(item));

// if you want to add what you got from the second class getlist is returning an arraylist if you want to add them to your combobox you need to convert that to a array by toArray();

 c.setModel(new nextstep().getList().toArray());

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM