Skip to content

Commit 0aa49b4

Browse files
hansonrhansonr
authored andcommitted
JList, JComboBox rendering fixes
1 parent 7a94de9 commit 0aa49b4

File tree

6 files changed

+50
-26
lines changed

6 files changed

+50
-26
lines changed

sources/net.sf.j2s.java.core/src/swingjs/plaf/JSComboBoxUI.java

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
import java.awt.Insets;
1010
import java.awt.LayoutManager;
1111
import java.awt.Rectangle;
12+
import java.awt.Shape;
1213
import java.awt.event.ActionEvent;
1314
import java.awt.event.ActionListener;
1415
import java.awt.event.FocusEvent;
@@ -84,6 +85,7 @@ public class JSComboBoxUI extends JSLightweightUI {
8485

8586
private boolean hasCustomRenderer;
8687
private ListCellRenderer renderer;
88+
protected boolean contentsTainted;
8789

8890
public JSComboBoxUI() {
8991
isContainer = true;
@@ -1011,7 +1013,14 @@ public boolean isFocusTraversable(JComboBox c) {
10111013
// begin ComponentUI Implementation
10121014
@Override
10131015
public void paint(Graphics g, JComponent c) {
1014-
popup.updateCSS();
1016+
if (contentsTainted) {
1017+
popup.updateList();
1018+
contentsTainted = false;
1019+
} else {
1020+
popup.updateCSS();
1021+
}
1022+
popup.updateSelectedIndex();
1023+
popup.setPopupVisible(false);
10151024
// SwingJS -- not doing this for now.
10161025
// hasFocus = comboBox.hasFocus();
10171026
// if ( !comboBox.isEditable() ) {
@@ -1729,7 +1738,7 @@ public boolean isEnabled(Object c) {
17291738
//
17301739
private class Handler implements ActionListener, FocusListener, KeyListener, LayoutManager, ListDataListener,
17311740
PropertyChangeListener, ListSelectionListener, ItemListener {
1732-
1741+
17331742
@Override
17341743
public void itemStateChanged(ItemEvent e) {
17351744
popup.updateState(e, null);
@@ -1927,7 +1936,7 @@ public void contentsChanged(ListDataEvent e) {
19271936
if (comboBox.isEditable() && editor != null) {
19281937
comboBox.configureEditor(comboBox.getEditor(), comboBox.getSelectedItem());
19291938
}
1930-
1939+
contentsTainted = true;
19311940
isDisplaySizeDirty = true;
19321941
repaint();
19331942
}
@@ -2071,9 +2080,8 @@ public int selectionForKey(char aKey, ComboBoxModel aModel) {
20712080
}
20722081

20732082
public void repaint() {
2074-
// comboBox.repaint();
2083+
comboBox.repaint();
20752084
}
20762085

2077-
20782086

20792087
}

sources/net.sf.j2s.java.core/src/swingjs/plaf/JSComboPopupList.java

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -125,9 +125,7 @@ void updateList() {
125125
JComponent j = (JComponent) cbui.comboBox.getRenderer().getListCellRendererComponent(this,
126126
getModel().getElementAt(i), i, true, false);
127127
j.setSize(w, ui.getRowHeight(i));
128-
opts[i] = j.秘getUI().reInit(true);
129-
// this next is critical to clear the renderer.
130-
j.秘getUI().reInit(false);
128+
opts[i] = j.秘getUI().getListNode();
131129
}
132130
j2scb.j2sCB("updateList", opts);
133131
j2scb.j2sCB("setHeight", (h > JSComboBoxUI.MAX_HEIGHT ? JSComboBoxUI.MAX_HEIGHT : 0));

sources/net.sf.j2s.java.core/src/swingjs/plaf/JSComponentUI.java

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -723,6 +723,18 @@ protected void newID(boolean forceNew) {
723723
}
724724
}
725725

726+
/**
727+
* Retrieve the node for a list or table, and then clear this renderer
728+
* so that it can rebuild a new domNode for the next cell.
729+
* @return
730+
*/
731+
public DOMNode getListNode() {
732+
DOMNode node = reInit(true);
733+
reInit(false);
734+
return node;
735+
}
736+
737+
726738
/**
727739
* This must remain the only place that domNode is set to null;
728740
*/
@@ -3478,4 +3490,5 @@ public boolean processKeyEvent(KeyEvent e) {
34783490
}
34793491

34803492

3493+
34813494
}

sources/net.sf.j2s.java.core/src/swingjs/plaf/JSListUI.java

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,7 @@ public DOMNode updateDOMNode() {
132132
// setDataComponent(focusNode);
133133
// bindJSKeyEvents(focusNode, false);
134134
}
135+
setBackgroundImpl(jc.getBackground());
135136
if (needFilling) {
136137
fillDOM();
137138
}
@@ -342,19 +343,18 @@ protected void paintCell(Graphics g, int index, Rectangle rowBounds,
342343
cw = w;
343344
}
344345

345-
rendererPane.paintComponent(g, rendererComponent, list, cx, cy, cw, ch,
346+
JComponent r = (JComponent) rendererComponent;
347+
rendererPane.paintComponent(g, r, list, cx, cy, cw, ch,
346348
true);
347-
updateItemHTML(rendererComponent, index, cx, cy, cw, getRowHeight(index));
348-
349-
((JComponent) rendererComponent).秘getUI().reInit(false);
350-
349+
updateItemHTML((JComponent) r, index, cx, cy, cw, getRowHeight(index));
351350
}
352351

353-
private void updateItemHTML(Component c, int index, int left, int top, int width, int height) {
352+
private void updateItemHTML(JComponent c, int index, int left, int top, int width, int height) {
353+
DOMNode node = null;
354354
if (c != null) {
355355
c.setSize(width, height);
356-
}
357-
DOMNode node = (c == null ? null : ((JSComponentUI) ((JComponent) c).ui).getDOMNode());
356+
node = c.秘getUI().getListNode();
357+
}
358358
String myid = id + "_" + index;
359359
JQueryObject jnode = $((DOMNode) (Object) ("#" + myid));
360360
if (((DOMNode[]) (Object) jnode)[0] == null) {
@@ -389,6 +389,8 @@ protected void removeItemHTML(int i0, int i1) {
389389
*/
390390
@Override
391391
public void paint(Graphics g, JComponent c) {
392+
if (isTainted)
393+
updateDOMNode();
392394
super.paint(g, c);
393395
Shape clip = g.getClip();
394396
paintImpl(g, c);
@@ -401,7 +403,7 @@ private void paintImpl(Graphics g, JComponent c) {
401403
// It is the responsibility of the JScrollPane scrollbar will move the JList
402404
// to new x,y coordinates.
403405

404-
needFilling = true;
406+
needFilling = false;
405407
itemHTML = "";
406408
switch (layoutOrientation) {
407409
case JList.VERTICAL_WRAP:
@@ -1761,6 +1763,7 @@ protected ListSelectionListener createListSelectionListener() {
17611763

17621764
void redrawList() {
17631765
needFilling = true;
1766+
setTainted(true);
17641767
list.revalidate();
17651768
list.秘repaint();
17661769
}
@@ -3005,10 +3008,13 @@ public Dimension getPreferredSize(int rows) {
30053008
Object o = m.getElementAt(i);
30063009
int d = 0;
30073010
if (o instanceof Component) {
3011+
// was w +=... so these would be for left to right
30083012
d = ((Component) o).getPreferredSize().width;
30093013
} else if (o != null) {
3014+
// and this would not?
30103015
d = list.getFontMetrics(getFont()).stringWidth(o.toString());
30113016
}
3017+
// was included in o != null
30123018
if (d > w)
30133019
w = d;
30143020
}

sources/net.sf.j2s.java.core/src/swingjs/plaf/JSTableUI.java

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -231,14 +231,13 @@ public void propertyChange(PropertyChangeEvent e) {
231231
case "rowSorter":
232232
case "sorter":
233233
case "autoResizeMode":
234-
// case "ancestor":
235-
// if (havePainted) {
236-
// setTainted();
237-
//// rebuildTable();
238-
// table.repaint();
239-
// }
240-
return;
241234
case "tableCellEditor":
235+
case "gridColor":
236+
case "preferredSize":
237+
case "background":
238+
case "ancestor":
239+
case "tableData":
240+
// TODO ?
242241
return;
243242
}
244243
System.out.println("JTableUI property not handled: " + prop);

sources/net.sf.j2s.java.core/src/test/components/ListDemo.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -97,14 +97,14 @@ public ListDemo() {
9797
hireButton.addActionListener(hireListener);
9898
hireButton.setEnabled(false);
9999

100-
JButton gotoButton = new JButton("Blanco White");
100+
JButton gotoButton = new JButton("Pearly White");
101101
gotoButton.setPreferredSize(new Dimension(130, 20));
102102
gotoButton.addActionListener(new ActionListener() {
103103

104104
@Override
105105
public void actionPerformed(ActionEvent e) {
106106
list.setSelectedValue(gotoButton.getText(), true);
107-
gotoButton.setText(gotoButton.getText() == "Blanco White" ? "Jane Doe" : "Blanco White");
107+
gotoButton.setText(gotoButton.getText() == "Pearly White" ? "Jane Doe" : "Pearly White");
108108
}
109109

110110
});

0 commit comments

Comments
 (0)