The JScrollPane content doesn't refesh while scrolling

走远了吗. 提交于 2019-12-11 10:33:37

问题


I'm creating a simple gui of options that containing a JScrollPanel.

The problem is when I'm scrolling all the content of the JPanel inside my JScrollPanel doesn't refresh like that : Bad Refresh Scrolling

Another issues is that not all my text fields are well painted and the combobox fields are behind the first text field.

Here my code :

  • OptionsPanel.java the source of the problem

    package scroll;
    
    import java.awt.GridBagConstraints;
    import java.awt.GridBagLayout;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.awt.event.TextEvent;
    import java.awt.event.TextListener;
    
    import javax.swing.JPanel;
    import javax.swing.JScrollPane;
    
    import scroll.NavigationButtons.Navigation;
    
    public class OptionsPanel extends JPanel implements ActionListener, TextListener {
    
        private static final long serialVersionUID = 3800714599366218432L;
    
        private NavigationButtons navBtn;
    
        private ChangeOptionCB language;
    
        private ChangeOptionTF option1;
    
        private ChangeOptionTF option2;
    
        private ChangeOptionTF option3;
    
        private ChangeOptionTF option4;
    
        private ChangeOptionTF option5;
    
        private ChangeOptionTF option6;
    
        private ChangeOptionTF option7;
    
        private JScrollPane scrollPane;
    
        public OptionsPanel() {
            super();
    
            GridBagLayout layout = new GridBagLayout();
            setLayout(layout);
    
            GridBagConstraints constraint = new GridBagConstraints();
    
            JPanel optionsPanel = new JPanel();
            GridBagLayout l = new GridBagLayout();
            GridBagConstraints c = new GridBagConstraints();
    
            optionsPanel.setLayout(l);
    
    
    
            String[] langues = {"en", "fr", "es"};
            language = new ChangeOptionCB("Languages", langues);
            language.addTextListener(this);
            c.gridx = 0;
            c.gridy = 0;
            c.fill = GridBagConstraints.HORIZONTAL;
            c.anchor = GridBagConstraints.PAGE_START;
            c.weightx = 1;
            c.weighty = 0.1;
            optionsPanel.add(language, c);
    
            option1 = new ChangeOptionTF("option 1");
            option1.addTextListener(this);
            option1.setText("option 1");
            c.gridx = 0;
            c.gridy = 1;
            c.fill = GridBagConstraints.HORIZONTAL;
            c.anchor = GridBagConstraints.PAGE_START;
            c.weightx = 1;
            c.weighty = 0.1;
            optionsPanel.add(option1, c);
    
            option2 = new ChangeOptionTF("option 2");
            option2.addTextListener(this);
            option2.setText("option 2");
            c.gridx = 0;
            c.gridy = 2;
            c.fill = GridBagConstraints.HORIZONTAL;
            c.anchor = GridBagConstraints.PAGE_START;
            c.weightx = 1;
            c.weighty = 0.1;
            optionsPanel.add(option2, c);
    
            option3 = new ChangeOptionTF("option 3");
            option3.addTextListener(this);
            option3.setText("option 3");
            c.gridx = 0;
            c.gridy = 3;
            c.fill = GridBagConstraints.HORIZONTAL;
            c.anchor = GridBagConstraints.PAGE_START;
            c.weightx = 1;
            c.weighty = 0.1;
            optionsPanel.add(option3, c);
    
            option4 = new ChangeOptionTF("option 4");
            option4.addTextListener(this);
            option4.setText("option 4");
            c.gridx = 0;
            c.gridy = 4;
            c.fill = GridBagConstraints.HORIZONTAL;
            c.anchor = GridBagConstraints.PAGE_START;
            c.weightx = 1;
            c.weighty = 0.1;
            optionsPanel.add(option4, c);
    
            option5 = new ChangeOptionTF("option 5");
            option5.addTextListener(this);
            option5.setText("option 5");
            c.gridx = 0;
            c.gridy = 5;
            c.fill = GridBagConstraints.HORIZONTAL;
            c.anchor = GridBagConstraints.PAGE_START;
            c.weightx = 1;
            c.weighty = 0.1;
            optionsPanel.add(option5, c);
    
            option6 = new ChangeOptionTF("option 6");
            option6.addTextListener(this);
            option6.setText("option 6");
            c.gridx = 0;
            c.gridy = 6;
            c.fill = GridBagConstraints.HORIZONTAL;
            c.anchor = GridBagConstraints.PAGE_START;
            c.weightx = 1;
            c.weighty = 0.1;
            optionsPanel.add(option6, c);
    
            option7 = new ChangeOptionTF("option 7");
            option7.addTextListener(this);
            option7.setText("option 7");
            c.gridx = 0;
            c.gridy = 7;
            c.fill = GridBagConstraints.HORIZONTAL;
            c.anchor = GridBagConstraints.PAGE_START;
            c.weightx = 1;
            c.weighty = 0.1;
            optionsPanel.add(option7, c);
    
            scrollPane = new JScrollPane(optionsPanel);
    
            constraint.gridx = 0;
            constraint.gridy = 0;
            constraint.fill = GridBagConstraints.BOTH;
            constraint.weightx = 1;
            constraint.weighty = 1;
            add(scrollPane, constraint);
    
            navBtn = new NavigationButtons(NavigationButtons.EXIT);
            navBtn.addActionListener(this);
            constraint.gridx = 0;
            constraint.gridy = 1;
            constraint.fill = GridBagConstraints.HORIZONTAL;
            constraint.weightx = 1;
            constraint.weighty = 0.25;
            add(navBtn, constraint);
    
        }
    
        @Override
        public void actionPerformed(ActionEvent ae) {
            if (navBtn == ae.getSource()) {
                int id = ae.getID();
                if (Navigation.EXIT.getId() == id) {
                    System.out.println("Get out !!");
                } 
            }
        }
    
        @Override
        public void textValueChanged(TextEvent te) {
            if (language == te.getSource()) {
                System.out.println("The option as changed : "+language.getOption());
            }
            if (option1 == te.getSource()) {
                System.out.println("The option as changed : "+option1.getNewText());
            }
            if (option2 == te.getSource()) {
                System.out.println("The option as changed : "+option2.getNewText());
            }
            if (option3 == te.getSource()) {
                System.out.println("The option as changed : "+option3.getNewText());
            }
            if (option4 == te.getSource()) {
                System.out.println("The option as changed : "+option4.getNewText());
            }
            if (option5 == te.getSource()) {
                System.out.println("The option as changed : "+option5.getNewText());
            }
            if (option6 == te.getSource()) {
                System.out.println("The option as changed : "+option6.getNewText());
            }
            if (option7 == te.getSource()) {
                System.out.println("The option as changed : "+option7.getNewText());
            }
            scrollPane.revalidate();
            scrollPane.repaint();
        }
    
    }
    
  • The Main Class

    package scroll;
    
    import java.awt.Dimension;
    import javax.swing.JFrame;
    
    public class ScrollTest {
    
        public static void main(String[] args) {
    
            JFrame frame = new JFrame();
    
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            frame.setResizable(false);
            frame.setContentPane(new OptionsPanel());
            frame.setTitle("Scrool Test");
            frame.pack();
    
            Dimension dimension = new Dimension(691, 263);
            frame.setSize(dimension);
            frame.setPreferredSize(dimension);
            frame.setLocationRelativeTo(null);
    
            frame.pack();
            frame.setVisible(true);
        }
    }
    
  • The different stuff that you need :

    package scroll;
    
    import java.awt.GridBagConstraints;
    import java.awt.GridBagLayout;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.awt.event.TextEvent;
    import java.awt.event.TextListener;
    
    import javax.swing.JButton;
    import javax.swing.JComboBox;
    import javax.swing.JLabel;
    import javax.swing.JPanel;
    import javax.swing.SwingConstants;
    import javax.swing.event.EventListenerList;
    
    public class ChangeOptionCB extends JPanel implements ActionListener {
    
        private static final long serialVersionUID = 3355314012553851743L;
        private JComboBox cb;
        private JButton saveBtn;
    
        private EventListenerList listeners;
    
        public ChangeOptionCB(String label, String[] list) {
    
            listeners = new EventListenerList();
    
            GridBagLayout layout = new GridBagLayout();
            GridBagConstraints c = new GridBagConstraints();
            setLayout(layout);
    
            JLabel jLabel = new JLabel(label);
            jLabel.setHorizontalAlignment(SwingConstants.LEFT);
            c.fill = GridBagConstraints.LINE_START;
            c.gridx = 0;
            c.gridy = 0;
            c.weightx = 0.1;
            add(jLabel, c);
    
            cb = new JComboBox(list);
            c.fill = GridBagConstraints.HORIZONTAL;
            c.gridx = 1;
            c.gridy = 0;
            c.weightx = 0.8;
            add(cb, c);
    
    
            saveBtn = new JButton("Save");
            saveBtn.addActionListener(this);
    
            c.gridx = 2;
            c.gridy = 0;
            c.weightx = 0;
            c.anchor = GridBagConstraints.LINE_END;
            add(saveBtn, c);
    
        }
    
        @Override
        public void actionPerformed(ActionEvent ae) {
            if (ae.getSource() == saveBtn) {
                fireTextAsChange();
            }
        }
    
        public String getOption() {
            return (String) cb.getSelectedItem();
        }
    
        public void addTextListener(TextListener listener) {
            listeners.add(TextListener.class, listener);
        }
    
        public void removeActionListener(TextListener listener) {
            listeners.remove(TextListener.class, listener);
        }
    
        private void fireTextAsChange(){
            TextListener[] listenerList = (TextListener[])listeners.getListeners(TextListener.class);
    
            for(TextListener listener : listenerList){
                listener.textValueChanged(new TextEvent(this, 0));
            }
        }
    
    }
    
    package scroll;
    
    import java.awt.GridBagConstraints;
    import java.awt.GridBagLayout;
    import java.awt.TextField;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.awt.event.TextEvent;
    import java.awt.event.TextListener;
    
    import javax.swing.JButton;
    import javax.swing.JLabel;
    import javax.swing.JPanel;
    import javax.swing.event.EventListenerList;
    
    public class ChangeOptionTF extends JPanel implements ActionListener {
    
        private static final long serialVersionUID = 3355314012553851743L;
        private TextField tf;
        private JButton saveBtn;
    
        private EventListenerList listeners;
    
        public ChangeOptionTF(String label) {
    
            listeners = new EventListenerList();
    
            GridBagLayout layout = new GridBagLayout();
            GridBagConstraints c = new GridBagConstraints();
            setLayout(layout);
    
            JLabel jLabel = new JLabel(label);
            c.fill = GridBagConstraints.BOTH;
            c.gridx = 0;
            c.gridy = 0;
            c.weightx = 1;
            add(jLabel, c);
    
            tf = new TextField();
            c.fill = GridBagConstraints.HORIZONTAL;
            c.gridx = 0;
            c.gridy = 1;
            c.weightx = 0.5;
            add(tf, c);
    
    
            saveBtn = new JButton("Save");
            saveBtn.addActionListener(this);
    
            c.gridx = 2;
            c.gridy = 1;
            c.weightx = 0;
            c.anchor = GridBagConstraints.LINE_END;
            add(saveBtn, c);
    
        }
    
        @Override
        public void actionPerformed(ActionEvent ae) {
            if (ae.getSource() == saveBtn) {
                fireTextAsChange();
            }
        }
    
        public String getNewText() {
            return tf.getText();
        }
    
        public void addTextListener(TextListener listener) {
            listeners.add(TextListener.class, listener);
        }
    
        public void removeActionListener(TextListener listener) {
            listeners.remove(TextListener.class, listener);
        }
    
        private void fireTextAsChange(){
            TextListener[] listenerList = (TextListener[])listeners.getListeners(TextListener.class);
    
            for(TextListener listener : listenerList){
                listener.textValueChanged(new TextEvent(this, 0));
            }
        }
    
        public void setText(String text) {
            tf.setText(text);
        }
    
    }
    
    package scroll;
    
    import java.awt.GridBagConstraints;
    import java.awt.GridBagLayout;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    
    import javax.swing.JButton;
    import javax.swing.JPanel;
    import javax.swing.event.EventListenerList;
    
    public class NavigationButtons extends JPanel implements ActionListener {
    
        private static final long serialVersionUID = -4844499317626526067L;
    
        public enum Navigation {
            NEXT(1), CANCEL(0), EXIT(-1);
    
            private int id;
    
            private Navigation(int id) {
                this.id = id;
            }
    
            public int getId() {
                return id;
            }
        }
    
        public static int NEXT_CANCEL = 0;
        public static int CANCEL = 1;
        public static int EXIT = 2;
    
        private JButton cancel;
        private JButton next;
        private JButton exit;
    
        private EventListenerList listeners;
    
        public NavigationButtons(int type) {
    
            listeners = new EventListenerList();
    
            GridBagLayout layout = new GridBagLayout();
            GridBagConstraints c = new GridBagConstraints();
            setLayout(layout);
    
            if ((NEXT_CANCEL == type) || (CANCEL == type)) {
                cancel = new JButton("Cancel");
                cancel.addActionListener(this);
    
                c.gridwidth = 1;
                c.anchor = GridBagConstraints.LINE_START;
                c.weightx = 1;
                add(cancel, c);
            }
    
            if (NEXT_CANCEL == type) {
                next = new JButton("Next");
                next.addActionListener(this);
    
                c.gridwidth = 1;
                c.anchor = GridBagConstraints.LINE_END;
                c.weightx = 0;
                add(next, c);
            }
    
            if (EXIT == type) {
                exit = new JButton("Exit");
                exit.addActionListener(this);
    
                c.gridwidth = 1;
                c.anchor = GridBagConstraints.LINE_END;
                c.weightx = 1;
                add(exit, c);
            }
    
        }
    
        public void setNextEnable(boolean b) {
            next.setEnabled(b);
        }
    
        @Override
        public void actionPerformed(ActionEvent ae) {
            if (ae.getSource() == next) {
                fireActionPerformed(Navigation.NEXT);
            }
            if (ae.getSource() == cancel) {
                fireActionPerformed(Navigation.CANCEL);
            }
            if (ae.getSource() == exit) {
                fireActionPerformed(Navigation.EXIT);
            }
        }
    
        public void addActionListener(ActionListener listener) {
            listeners.add(ActionListener.class, listener);
        }
    
        public void removeActionListener(ActionListener listener) {
            listeners.remove(ActionListener.class, listener);
        }
    
        public void fireActionPerformed(Navigation nav){
            ActionListener[] listenerList = (ActionListener[])listeners.getListeners(ActionListener.class);
    
            for(ActionListener listener : listenerList){
                listener.actionPerformed(new ActionEvent(this, nav.getId(), null));
            }
        }
    }
    

What is wrong in my code that make this ugly refresh ? I don't understand.

Maybe I need to implement a kind of listener that repaint my frame each time I scroll ?

The more strange things is, if I replace

add(scrollPane, constraint);

by

add(optionsPane, constraint);

the content get out well (at least in this example).

Thank's you

Julien


回答1:


The basic problem is, you're using java.awt.TextField which is a heavy weight component inside a lighweight container. This is just asking for issues, they tend not to play well together.

Instead, use a javax.swing.JTextField

import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.TextEvent;
import java.awt.event.TextListener;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.event.EventListenerList;

public class ChangeOptionTF extends JPanel implements ActionListener {

    private static final long serialVersionUID = 3355314012553851743L;
    private JTextField tf;
    private JButton saveBtn;

    private EventListenerList listeners;

    public ChangeOptionTF(String label) {

        listeners = new EventListenerList();

        GridBagLayout layout = new GridBagLayout();
        GridBagConstraints c = new GridBagConstraints();
        setLayout(layout);

        JLabel jLabel = new JLabel(label);
        c.fill = GridBagConstraints.BOTH;
        c.gridx = 0;
        c.gridy = 0;
        c.weightx = 1;
        add(jLabel, c);

        tf = new JTextField();


来源:https://stackoverflow.com/questions/33899843/the-jscrollpane-content-doesnt-refesh-while-scrolling

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