隐藏/显示JTextField [英] Hide/Show JTextField

查看:277
本文介绍了隐藏/显示JTextField的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have a JTextField that in the begin it have to be hidden and in a specific situation it have to show.
This is my code:

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必须出现

the idea is:
when the user click a value to edit on the JList, the JTextField have to appear

推荐答案

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

After textField.setVisible(true); you need to call revalidate() and repaint() methods of container which contains your JTextField, if you try to show/hide components of visible container.

因此,只需将下一行添加到您的代码中,所有代码都将起作用:

So just add next lines to your code and all will be work:

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

这篇关于隐藏/显示JTextField的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆