Piotr Sobolewski
Piotr Sobolewski

Reputation: 2134

Text disappear after Component change size

I create a jTable, and TableCellRenderer, TableCellEditor on it. I need to put there editable (with text/html context type) JEditorPane. When i write some text inside and resize component, text disappear. What I doing wrong? Furthermore above this component I have got buttons with text edition: for example:

JButton bold = new JButton():
bold.setAction(new StyledEditorKit.BoldAction());

It is part of my custom model:

private JEditorPane editorTxtPane = new JEditorPane("text/html", "");
private JEditorPane rendererTxtPane = new JEditorPane("text/html", "");
private final JPanel editorPanel = new JPanel();
private final JPanel rendererPanel = new JPanel();
private final ArrayList<FocusListener> editorFocusListeners = new ArrayList<FocusListener>();

public SampleModel() {
    super();

    rendererTxtPane.setContentType("text/html");
    editorTxtPane.setContentType("text/html");

    rendererPanel.add(initCellControls(rendererPanel, rendererLabel));
    rendererPanel.add(rendererTxtPane);

    editorPanel.add(initCellControls(editorPanel, editorLabel));
    JScrollPane sp = new JScrollPane(editorTxtPane);
    sp.setBorder(null);
    editorPanel.add(sp);

    editorTxtPane.addFocusListener(new FocusAdapter() {

        @Override
        public void focusGained(FocusEvent e) {
            super.focusGained(e);
            e.setSource(editorTxtPane);
            for (int i = editorFocusListeners.size() - 1; i >= 0; i--) {
                editorFocusListeners.get(i).focusGained(e);
            }
        }

        @Override
        public void focusLost(FocusEvent e) {
            super.focusLost(e);
            e.setSource(editorTxtPane);
            for (int i = editorFocusListeners.size() - 1; i >= 0; i--) {
                editorFocusListeners.get(i).focusLost(e);
            }
        }
    });
}

It is my editor and renderer methods:

@Override
public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) {
    Comment c = data.get(row);
    rendererTxtPane.setText(c.getComment());
    return rendererPanel;
}

@Override
public Component getTableCellEditorComponent(JTable table, Object value, boolean isSelected, int row, int column) {
    Comment c = data.get(row);
    c.setNeedSave(true);
    editorTxtPane.setText(c.getComment());
    return editorPanel;
}

Upvotes: 2

Views: 248

Answers (2)

trashgod
trashgod

Reputation: 205785

This is not how Editors and Renderers work. In particular, the editor is only valid while the cell is being edited. Your TableModel should store each row's Document. After editing concludes, your model will be updated with the revised Document, as described here. You might compare what your doing with the example, which could form the basis of your sscce.

Upvotes: 1

GingerHead
GingerHead

Reputation: 8230

This may be the result of one of the two conditions below:

  1. The text component that you are embedding in your resisable component is being shifted out through the process of resising mechanism, so when you are resising it, the inner text component is disappearing
  2. Whenever the resising process is happening, the standard Swing repaint process is not being called by he platform, in the right moment, so you can call repaint manually through coding. The SWING platfrom usually calls the repaint method automatically whenever it notices a change in the overall GUI, but it's schedulled to be run after some other processes to complete, in this case calling repaint manually is inevitble

Upvotes: 0

Related Questions