Hide/Show JTextField

Deadly 提交于 2020-02-29 07:32:25

问题


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);
    }
}

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


回答1:


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();



回答2:


Have you tried using textField.revalidate() and then textField.repaint() after setting the text field to be visible?



来源:https://stackoverflow.com/questions/20922442/hide-show-jtextfield

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!