Note that there are some explanatory texts on larger screens.

plurals
  1. PO'SwingUtilities.updateComponentTreeUI(this)' removes custom Document from JComboBox
    primarykey
    data
    text
    <p>I have an editable JComboBox and JTextField. Both with custom Documents. Here is the code:</p> <pre><code>import java.awt.*; import javax.swing.*; import javax.swing.text.AttributeSet; import javax.swing.text.BadLocationException; import javax.swing.text.PlainDocument; public class SwingUtilStrangeBehav extends JFrame { public SwingUtilStrangeBehav() { JComboBox&lt;String&gt; combo = new JComboBox&lt;&gt;(new String[]{"a", "b", "c"}); combo.setEditable(true); ((JTextField)combo.getEditor().getEditorComponent()).setDocument(new PlainDocument() { private static final long serialVersionUID = 1L; @Override public void insertString(int offs, String str, AttributeSet a) throws BadLocationException { System.out.println("New text inserted into combo!"); super.insertString(offs, str, a); } }); JTextField field = new JTextField(); field.setDocument(new PlainDocument() { private static final long serialVersionUID = 1L; @Override public void insertString(int offs, String str, AttributeSet a) throws BadLocationException { System.out.println("New text inserted into text!"); super.insertString(offs, str, a); } }); Container c = getContentPane(); c.setLayout(new BoxLayout(c, BoxLayout.PAGE_AXIS)); c.add(combo); c.add(Box.createRigidArea(new Dimension(0, 5))); c.add(field); //SwingUtilities.updateComponentTreeUI(this); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); pack(); setLocationRelativeTo(null); setVisible(true); } public static void main(String arg[]) { java.awt.EventQueue.invokeLater(new Runnable() { @Override public void run() { new SwingUtilStrangeBehav(); } }); } } </code></pre> <p>Then I input some text in JComboBox or JTextField I get the following output in my console, for example:</p> <p><em>New text inserted into combo!</em><br> <em>New text inserted into text!</em></p> <p>That's great! When I uncomment the following line <strong>SwingUtilities.updateComponentTreeUI(this);</strong> and run this programm, I can only get this output:<br><br> <em>New text inserted into text!</em></p> <p>It seems that JComboBox's Document was removed. Why was custom Document removed and how to solve this problem? I want that custom Document would still be in JComboBox after executing <strong>SwingUtilities.updateComponentTreeUI(this);</strong>. </p> <p>FYI: I use <strong>SwingUtilities.updateComponentTreeUI(this);</strong> to apply new Font to Container.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload