简体   繁体   中英

JFrame refuses to repaint()

I really need help. I have a JFrame with some JTextFields, which I have disabled. I want them to be enabled by a single click to a JToggleButton ("Edit") which is also on the JFrame. So, in the costructor, I pass a boolean isEditable parameter, which is a field, to the .isEditable() method for all the JTextFields, so that after the JFrame shows and the editButton is clicked all that happens is that the isEditable value changes to true , and the whole JFrame is repainted. The problem is that the JFrame is refusing to repaint! What can I do? I would be really grateful for any help I get. Thanks :)
Code:

package job;

import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.GridLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;

import javax.swing.BorderFactory;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JSeparator;
import javax.swing.JTextField;
import javax.swing.JToggleButton;
import javax.swing.SwingConstants;
import javax.swing.border.EmptyBorder;

public class TestFrame extends JFrame implements ItemListener{

    private JPanel contentPane;
    private boolean saveEnabled = false;
    private boolean editEnabled = false; 
    private String actionCommand;
    private Node n;

    /**
     * Launch the application.
     */
    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    TestFrame frame = new TestFrame();
                    frame.setVisible(true);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }

    /**
     * Create the frame.
     */
    public TestFrame() {
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        contentPane = new JPanel();
        contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
        contentPane.setLayout(new BorderLayout(0, 0));
        setContentPane(contentPane);

        setBounds(50, 60, 600, 640);

        JLabel detailsBanner = new JLabel();

        contentPane.add(detailsBanner, BorderLayout.NORTH);
        JPanel contentDetail = new JPanel();
        contentDetail.setLayout(new BoxLayout(contentDetail, BoxLayout.PAGE_AXIS));
        JPanel namePanel = new JPanel();
        namePanel.setLayout(new BoxLayout(namePanel, BoxLayout.LINE_AXIS));
        namePanel.setBorder(BorderFactory.createEmptyBorder(10, 0, 0, 10));
        String nameString = "Sample";
        setTitle(nameString);
        JLabel nameText = new JLabel(nameString);
        namePanel.add(nameText);
        contentDetail.add(namePanel);
        JSeparator sep = new JSeparator(SwingConstants.HORIZONTAL);
        contentDetail.add(sep);
        JPanel commanderPanel = new JPanel();
        commanderPanel.setLayout(new BoxLayout(commanderPanel, BoxLayout.PAGE_AXIS));

        JPanel commanderNamePanel = new JPanel();
        JLabel commanderTitle = new JLabel("Commander");
        JTextField commanderName = new JTextField("Sample");
        commanderName.setEnabled(editEnabled);
        commanderNamePanel.setLayout(new GridLayout(1,2));
        commanderNamePanel.add(commanderTitle);
        commanderNamePanel.add(commanderName);
        commanderPanel.add(commanderNamePanel);

        contentPane.add(commanderPanel);

        JPanel  mainAuxButtonPanel = new JPanel();
        mainAuxButtonPanel.setLayout(new BorderLayout(0, 0));
        JPanel auxButtonPanel = new JPanel();
        GridBagLayout auxButtonsLayout = new GridBagLayout();
        JPanel sepPanel = new JPanel();
        GridBagLayout sepLayout = new GridBagLayout();
        GridBagConstraints c = new GridBagConstraints();
        c.insets = new Insets(30, 0, 0, 0);
        c.fill = GridBagConstraints.HORIZONTAL;
        sepPanel.setLayout(sepLayout);
        sepPanel.add(new JSeparator(SwingConstants.HORIZONTAL), c);
        mainAuxButtonPanel.add(sepPanel, BorderLayout.NORTH);

        c.fill = GridBagConstraints.NONE;
        c.insets = new Insets(10, 5, 0, 0);
        auxButtonPanel.setLayout(auxButtonsLayout);
        JButton back = new JButton("Back");
        auxButtonsLayout.setConstraints(back, c);
        back.addActionListener(new ActionListener(){
            @Override
            public void actionPerformed(ActionEvent arg0) {
                dispose();
            }
        });
        auxButtonsLayout.setConstraints(back, c);
        auxButtonPanel.add(back);

        JToggleButton edit = new JToggleButton("Edit");
        auxButtonsLayout.setConstraints(edit, c);
        edit.addItemListener(this);
        auxButtonPanel.add(edit);

        JButton save = new JButton("Save");
        save.setEnabled(saveEnabled);
        auxButtonsLayout.setConstraints(save, c);
        back.addActionListener(new ActionListener(){
            @Override
            public void actionPerformed(ActionEvent arg0) {
            }
        });
        auxButtonPanel.add(save);
        mainAuxButtonPanel.add(auxButtonPanel, BorderLayout.EAST);

        contentPane.add(mainAuxButtonPanel, BorderLayout.SOUTH);
    }

    @Override
    public void itemStateChanged(ItemEvent e) {
        if (e.getStateChange() == ItemEvent.SELECTED){
            editEnabled = true;
            System.out.println(editEnabled);
        }else{
            editEnabled = false;
            System.out.println(editEnabled);
        }
        System.out.println("repaintin...");
        contentPane.repaint();
    }
}

I don't know why you need to repaint JFrame.

Say you have a JTextField named filed,simply you can add an actionlistener and add the code like this :

filed.setEnabled(!filed.isEnabled());

What's more,maybe you should call repaint method on the right component ,but not on the JFrame.

Or try revalidate() and some method like that .

update:

What I mean ,you should not update a component 's enabled state in repaint method.

And maybe there is no need to use a editEnabled variable to save state ,please try this way and modify it to meet your need:

JToggleButton edit = new JToggleButton("Edit");
        auxButtonsLayout.setConstraints(edit, c);
        edit.addActionListener(new ActionListener(){

            @Override
            public void actionPerformed(ActionEvent e) {
                // TODO Auto-generated method stub
                commanderName.setEnabled(!commanderName.isEnabled());
            }
        });
        auxButtonPanel.add(edit);

If you have many JTextFields,just iterate it .

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