0

When a JTextField is in a JScrollPanel, if the panel has been scrolled, whenever the dropdown from a JComboBox is over the JTextField, the text field shows through the dropdown. JTextField shows through JComboBox Dropdown

This only happens after the content has been scrolled (not on startup of the application).

The main question is how can we fix this? Bonus points if the answer:

  • Is not a hack
  • Explains why is it happening in the first place

Things I've tried:

  • Moving the dropdown outside of the scrollpane (no change)
  • Adding a repaint to any and every container I could find on scroll (no change)
  • Different Layout managers for the content of the scrollpane (no change)

Code Example:

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class TextFieldShowsThrough{

    public static void main(String[] args){
        JFrame frame = new JFrame();
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.add(createScrollDemo());
        frame.pack();
        // For demonstration purposes
        frame.setSize(frame.getWidth() + 100, frame.getHeight() - 100);
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);
    }

    public static JScrollPane createScrollDemo(){
        final Box optionsPanel = Box.createVerticalBox();
        optionsPanel.add(createDropDown());
        optionsPanel.add(createTextField("Option1"));
        optionsPanel.add(createTextField("Option2"));
        optionsPanel.add(createTextField("Option3"));
        optionsPanel.add(createTextField("Option4"));
        optionsPanel.add(createTextField("Option5"));
        optionsPanel.add(Box.createVerticalGlue());
        JScrollPane result = new JScrollPane(optionsPanel);
        // Made attempts to fix here, but to no avail
        /*result.getVerticalScrollBar().addAdjustmentListener(new AdjustmentListener() {

            @Override
            public void adjustmentValueChanged(AdjustmentEvent e) {
                result.repaint();
            }
        });*/
        return result;
    }

    public static Box createDropDown(){
        Box b = Box.createVerticalBox();
        b.setAlignmentX(JLabel.LEFT_ALIGNMENT);
        b.add(new JLabel("Language"));
        JComboBox combo = new JComboBox(new String[]{"en", "fr", "es"});
        combo.setMaximumSize(new Dimension(500, 25));
        b.add(combo);
        return b;
    }

    public static Box createTextField(String label){
        Box mainBox = Box.createVerticalBox();
        mainBox.setOpaque(true);
        mainBox.setBackground(new Color((int)(Math.random() * 0x1000000)));  // because fun

        JLabel jLabel = new JLabel(label);
        jLabel.setAlignmentX(JLabel.LEFT_ALIGNMENT);
        mainBox.add(jLabel);

        Box secondaryBox = Box.createHorizontalBox();
        secondaryBox.setAlignmentX(JLabel.LEFT_ALIGNMENT);

        TextField tf = new TextField();
        tf.setMaximumSize(new Dimension(500, 25));
        secondaryBox.add(tf);

        mainBox.add(secondaryBox);

        return mainBox;
    }
}
mKorbel
  • 109,525
  • 20
  • 134
  • 319
Nick Rippe
  • 6,465
  • 14
  • 30

1 Answers1

3

That's because you're using a java.awt.TextField, which is heavy weight component, inside a light weight container. The popup window used by the JComboBox can also be a light weight component.

AWT components don't play well with Swing components, they have z-ordering issues.

Change TextField tf = new TextField(); to JTextField tf = new JTextField();

You should also avoid using setPreferred/Minimum/MaximumSize (see Should I avoid the use of set(Preferred|Maximum|Minimum)Size methods in Java Swing? for more details) and instead use layout constraints and sizing hints (like the columns property of the JTextField)

Community
  • 1
  • 1
MadProgrammer
  • 343,457
  • 22
  • 230
  • 366
  • Funny how you can miss that little "J" when looking at someone else's code. Easy fix, good answer. Will accept as soon as able. – Nick Rippe Nov 24 '15 at 23:00
  • 1
    I assume you mean [this](http://stackoverflow.com/questions/33899843/the-jscrollpane-content-doesnt-refesh-while-scrolling/33904482#33904482) code? – MadProgrammer Nov 24 '15 at 23:04
  • :P Yep - There were issues in the question (refresh stuff?) I couldn't reproduce, so I couldn't "fix" the question, but it did spark curiosity. – Nick Rippe Nov 24 '15 at 23:09