隐藏/显示JTextField

时间:2014-01-04 14:53:17

标签: java swing jtextfield

我有一个JTextField,在开始时它必须被隐藏,并且在特定情况下它必须显示。
这是我的代码:

package StudentNotes;

import java.awt.BorderLayout;

public class EditCourse extends JDialog {
    private JTextField textField;

    /**
     * Create the dialog.
     */
    public EditCourse(JDialog mainFrame, final StudApp studAppObj) {
        super(mainFrame, ModalityType.APPLICATION_MODAL);
        setPreferredSize(new Dimension(330, 200));
        setTitle("Edit course");
        setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
        setAlwaysOnTop(true);

        ArrayList<Corso> listCourses = studAppObj.getCorsi();
        listCourses.toArray();

        String[] listData = { "one", "two", "three", "four",
                              "five", "six", "seven" };
        final JList list = new JList(listData);
        list.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
        list.getSelectionModel().addListSelectionListener(new ListSelectionListener() {
            @Override
            public void valueChanged(ListSelectionEvent e) {

                if (e.getValueIsAdjusting() == true) {
                    textField.setVisible(true); // it does not show (why?)

                }
            }
        });
        JScrollPane scrollPane = new JScrollPane(list);
        scrollPane.setSize(new Dimension(118, 40));

        textField = new JTextField();
        textField.setVisible(false); // it is invisible (OK)
        textField.setColumns(10);
        GroupLayout groupLayout = new GroupLayout(getContentPane());
        groupLayout.setHorizontalGroup(
            groupLayout.createParallelGroup(Alignment.LEADING)
                .addGroup(groupLayout.createSequentialGroup()
                    .addGap(108)
                    .addGroup(groupLayout.createParallelGroup(Alignment.LEADING)
                        .addComponent(textField, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
                        .addComponent(scrollPane, GroupLayout.PREFERRED_SIZE, 108, GroupLayout.PREFERRED_SIZE))
                    .addContainerGap(108, Short.MAX_VALUE))
        );
        groupLayout.setVerticalGroup(
            groupLayout.createParallelGroup(Alignment.LEADING)
                .addGroup(groupLayout.createSequentialGroup()
                    .addGap(21)
                    .addComponent(scrollPane, GroupLayout.PREFERRED_SIZE, 64, GroupLayout.PREFERRED_SIZE)
                    .addGap(18)
                    .addComponent(textField, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
                    .addContainerGap(49, Short.MAX_VALUE))
        );
        getContentPane().setLayout(groupLayout);
        pack();
        setLocationRelativeTo(null);
        setResizable(false);
        setVisible(true);
    }
}

这个想法是:
当用户单击要在JList上编辑的值时,必须显示JTextField

2 个答案:

答案 0 :(得分:7)

textField.setVisible(true);之后,如果您尝试显示/隐藏可见容器的组件,则需要调用包含revalidate()的容器的repaint()JTextField方法。

所以只需在代码中添加下一行,所有内容都可以正常工作:

 EditCourse.this.revalidate();
 EditCourse.this.repaint();

答案 1 :(得分:0)

在设置文本字段后,您是否尝试过使用textField.revalidate()然后textField.repaint()