简体   繁体   中英

How to connect combobox (String) to a JTextField on JFrame for Java

I am trying to do a hockey stats program and how do I connect the String that I created for the combobox to the JTextField. (For example, if Corey Perry on Anaheim has 3 goals, how do I put 3 goals on the Goals JTextField)? Also, if possible, how do I make -----Centre----- unclickable?

    public FantasyHockey()
    {
        String team[] = {"Anaheim Ducks", "Arizona Coyotes", "Boston Bruins", "Buffalo Sabres", "Calgary Flames", "Caroline Huricanes", "Chicago Blackhawks", "Colorado Avalanche", "Columbus Blue Jackets", "Dallas Stars", "Detroit Red Wings", "Edmonton Oilers", "Florida Panthers", "Los Angeles Kings", "Minnesota Wild", "Montreal Canadiens", "Nashville Predators", "New Jersey Devils", "New York Islanders", "New York Rangers", "Ottawa Senators", "Philadelphia Flyers", "Pittsburgh Penguins", "San Jose Sharks", "St. Louis Blues", "Tampa Bay Lightning", "Toronto Maple Leafs", "Vancouver Canucks", "Washington Capitals", "Winnipeg Jets"};
        teamName = new JComboBox( team );
        teamName.setBounds(50, 48, 166, 25);
        teamName.addActionListener( this );

        getContentPane().setLayout(null);
        getContentPane().add( teamName );


        playerName = new JComboBox();
        playerName.setBounds(241, 47, 191, 27);
        //playerName.setPrototypeDisplayValue("XXXXXXXXXX");
        getContentPane().add( playerName );

        String[] Anaheim = { "-----Centres-----", "Corey Perry","Ryan Getzlaf" };
        subItems.put(team[0], Anaheim);

        String[] Arizona = { "Max Domi" };
        subItems.put(team[1], Arizona);

        String[] Bruins = { "Tuukka Rask" };
        subItems.put(team[2], Bruins);
        teamName.setSelectedIndex(0);

        JLabel lblTeam = new JLabel("Team");
        lblTeam.setBounds(50, 22, 61, 16);
        getContentPane().add(lblTeam);

        JLabel lblPlayer = new JLabel("Player");
        lblPlayer.setBounds(241, 19, 61, 16);
        getContentPane().add(lblPlayer);

        Goals = new JTextField();
        Goals.setBounds(25, 181, 42, 25);
        getContentPane().add(Goals);
        Goals.setColumns(10);
        Goals.setEnabled(false);

        JLabel lblGoals = new JLabel("Goals");
        lblGoals.setBounds(25, 153, 61, 16);
        getContentPane().add(lblGoals);
    }

    public void actionPerformed(ActionEvent e)
    {
        String item = (String)teamName.getSelectedItem();
        Object o = subItems.get(item);

        if (o == null)
        {
            playerName.setModel( new DefaultComboBoxModel() );
        }
        else
        {
            playerName.setModel( new DefaultComboBoxModel( (String[])o ) );
        }
    }

    public void Goals(){
        if(teamName.getSelectedItem().equals("Anaheim Ducks")){
            if(playerName.getSelectedItem().equals("Corey Perry")){
                Goals.setText("a");
            }
        }
    }

    public static void main(String[] args)
    {
        JFrame frame = new FantasyHockey();
        frame.setDefaultCloseOperation( EXIT_ON_CLOSE );
        frame.pack();
        frame.setLocationRelativeTo( null );
        frame.setVisible( true );
        frame.setBounds(100, 100, 450, 300);
        frame.setResizable(true);
    }
}

if Corey Perry on Anaheim has 3 goals, how do I put 3 goals on the Goals JTextField

Then you need to store a custom Object in the ComboBoxModel. This object will have two pieces of information - playerName and playerGoals. The name will be displayed in the combo box and then when you click on the item you can get the object and then display the goals.

Check out ComboBox With Custom Renderer for an outline of this approach.

Also, if possible, how do I make -----Centre----- unclickable?

One option to display a prompt when no item is selected. Check out Combo Box Prompt for a suggestion.

Note: if you want to use these ideas you will need to modify the first solution to support the prompt, since both suggestion involve a custom renderer the code needs to combined into one.

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