Combobox的ActionListener仅适用于声明后的第一列

时间:2016-11-26 11:46:57

标签: java swing combobox

我想通过更改第一个值的组合来更改我的第二个组合框元素,但它不起作用。 Combobox的ActionListener仅适用于声明后的第一列。如果我运行调试并按" Run Into" - 效果很好 这是我的代码:

import java.awt.Container;
import java.awt.Desktop;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.SwingConstants;


import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

public class face extends JFrame {

public face() {
    super("Test frame");

    setDefaultCloseOperation(EXIT_ON_CLOSE);
    //              READ FILE           //
    File f=new File("");
    String tname = f.getAbsolutePath()+File.separator+"1.txt";
    Scanner in = null;
    int i=0;
    List<String> list = new ArrayList<String>();
    try {
        in = new Scanner(new File(tname));
    } catch (FileNotFoundException e2) {
        e2.printStackTrace();
    }
    while (in.hasNextLine())
    list.add(in.nextLine());
    in.close();
    String[] teacherlist = list.toArray(new String[0]);
    JComboBox tcombo = new JComboBox(teacherlist);
    tcombo.setSelectedIndex(1);

    String item = (String)tcombo.getSelectedItem();
    JPanel panel = new JPanel();
    panel.add(tcombo);

    ActionListener actionListener = new ActionListener() {
     public void actionPerformed(ActionEvent e) {
            File r = new File("");
            //String item = (String)tcombo.getSelectedItem();
            String sname = r.getAbsolutePath()+File.separator+"aaaa.txt";
            List<String> slist = new ArrayList<String>();
            Scanner k;
            k=null;
            try {
                k = new Scanner(new File(sname));
            } catch (FileNotFoundException e2) {
                e2.printStackTrace();
            }
            while (k.hasNextLine())
            slist.add(k.nextLine());
            k.close();
            String[] subjectlist = slist.toArray(new String[0]);
            JComboBox scombo = new JComboBox(subjectlist);
            scombo.setVisible(true);
            panel.add(scombo);
       }
    };
    tcombo.addActionListener(actionListener);
    panel.add(but);
    label.setVisible(false);
    setContentPane(panel);
    }
public static void main(String[] args) 
{JFrame myWindow = new face();
myWindow.setVisible(true);
myWindow.setSize(500,500);
}}

1 个答案:

答案 0 :(得分:1)

我猜这个代码的问题是:

JComboBox scombo = new JComboBox(subjectlist);
//scombo.setVisible(true); // not needed components are visible by default
panel.add(scombo);

组合框的默认大小为(0,0),因此无需绘制任何内容。

每当您向可见的GUI添加组件时,基本逻辑是:

panel.add(...);
panel.revalidate();
panel.repaint();

然而,可能更好的解决方案是在创建框架时将两个组合框添加到GUI。然后,当您在第一个组合框中选择一个项目时,您只需更改&#34;数据&#34;在第二个组合框中。您可以通过在第二个组合框上调用setModel(...)方法来执行此操作。

查看Binding comboboxes in swing以获取此方法的示例。