Hi,
If the selected (active) card of an ULCCardPane is replaced by the
setComponentAt() - method
it cannot be selected (made active) by setSelectedName() or
setSelectedComponent() methods,
another card has to be selected first and then the replaced card has to
be reselected to be shown.
The same effect can be observed when setComponentAt() is replaced by
removeCard() - add() card methods.
See attached code snippet.
Thanks and regards, Christoph
import com.ulcjava.base.application.*;
import com.ulcjava.base.application.util.*;
import com.ulcjava.base.application.event.*;
import com.ulcjava.base.development.DevelopmentRunner;
import javax.swing.UIManager;
/**
* ULCCardPane: Problem with showing a replaced selected card. If the selected
(active) card is replaced by the
* setComponentAt() - method it cannot be selected (made active) by
setSelectedName() or setSelectedComponent() methods,
* another card has to be selected first and then the replaced card has to be
reselected to be shown. This error occurs
* only if there is more than one card.
* To illustrate the bug press button "Replace Active Card" (observe
System.out) and then scroll forward in the 5 cards
* with button "Forward" to finally see the replaced card. With the fix behind
button "Replace Active Card (Fix)" the replaced
* card can be shown immediately.
* The same effect can be observed when setComponentAt() is replaced by
removeCard() - add() card methods.
*
* Version: ULC 6.1.2 and earlier, java 1.5
*
* Thanks for your support.
* --Christoph , 30.12.2006
*/
public class ULCCardPaneSnippet extends AbstractApplication {
ULCFrame frame = new ULCFrame("ULCCardPane - replace active (selected) card");
ULCBorderLayoutPane rootBox = new ULCBorderLayoutPane() ;
ULCToolBar toolbar = new ULCToolBar();
ULCCardPane cards = new ULCCardPane();
ULCButton button1 = new ULCButton("Backward");
ULCButton button2 = new ULCButton("Forward");
ULCButton button3 = new ULCButton("Replace Active Card");
ULCButton button4 = new ULCButton("Replace Active Card (Fix)");
ULCButton button5 = new ULCButton("Replace by Remove/Add");
ULCButton button6 = new ULCButton("Replace by Remove/Add (Fix)");
int ncards=5; // number of cards
public void start() {
frame.setDefaultCloseOperation(ULCFrame.TERMINATE_ON_CLOSE);
frame.setSize(new Dimension(900, 200));
frame.add(rootBox);
rootBox.add(toolbar, ULCBorderLayoutPane.NORTH);
rootBox.add(cards, ULCBorderLayoutPane.CENTER);
toolbar.setFloatable(false);
toolbar.add(button1);
toolbar.add(button2);
toolbar.addSeparator();
toolbar.add(button3);
toolbar.add(button4);
toolbar.add(button5);
toolbar.add(button6);
// set up cards -------------------------------------------
for (int i=0; i<ncards; i++) {
cards.add(new ULCLabel(" card number "+String.valueOf(i)),
String.valueOf(i));
}
button1.addActionListener(new IActionListener() {
public void actionPerformed(ActionEvent e) {
int i = Integer.parseInt(cards.getSelectedName()); i--;
if (i<0) i=ncards-1;
cards.setSelectedName(String.valueOf(i));
}
});
button2.addActionListener(new IActionListener() {
public void actionPerformed(ActionEvent e) {
int i = Integer.parseInt(cards.getSelectedName()); i++;
if (i>=ncards) i=0;
cards.setSelectedName(String.valueOf(i));
}
});
button3.addActionListener(new IActionListener() {
public void actionPerformed(ActionEvent e) {
int i = Integer.parseInt(cards.getSelectedName());
cards.setComponentAt(String.valueOf(i), new ULCLabel(" card number
"+String.valueOf(i)+" - card replaced") );
cards.setSelectedName(String.valueOf(i));
System.out.println("selected name="+cards.getSelectedName());
}
});
button4.addActionListener(new IActionListener() {
public void actionPerformed(ActionEvent e) {
int i = Integer.parseInt(cards.getSelectedName());
cards.setComponentAt(String.valueOf(i), new ULCLabel(" card number
"+String.valueOf(i)+" - card replaced with fix") );
int j=i+1; if (j>=ncards) j=0;
cards.setSelectedName(String.valueOf(j));
System.out.println("selected name="+cards.getSelectedName());
cards.setSelectedName(String.valueOf(i));
System.out.println("selected name="+cards.getSelectedName());
}
});
button5.addActionListener(new IActionListener() {
public void actionPerformed(ActionEvent e) {
int i = Integer.parseInt(cards.getSelectedName());
cards.removeCard(String.valueOf(i));
cards.add(new ULCLabel(" card number "+String.valueOf(i)+" - card
replaced"), String.valueOf(i) );
cards.setSelectedName(String.valueOf(i));
System.out.println("selected name="+cards.getSelectedName());
}
});
button6.addActionListener(new IActionListener() {
public void actionPerformed(ActionEvent e) {
int i = Integer.parseInt(cards.getSelectedName());
cards.removeCard(String.valueOf(i));
cards.add(new ULCLabel(" card number "+String.valueOf(i)+" - card
replaced with fix"), String.valueOf(i) );
int j=i+1; if (j>=ncards) j=0;
cards.setSelectedName(String.valueOf(j));
System.out.println("selected name="+cards.getSelectedName());
cards.setSelectedName(String.valueOf(i));
System.out.println("selected name="+cards.getSelectedName());
}
});
//
------------------------------------------------------------------------------------------------------
frame.setVisible(true);
}
public static final String METAL =
"javax.swing.plaf.metal.MetalLookAndFeel";
public static final String MOTIF =
"com.sun.java.swing.plaf.motif.MotifLookAndFeel";
public static final String WINDOWS =
"com.sun.java.swing.plaf.windows.WindowsLookAndFeel";
public static void main(String[] args) {
try {
UIManager.setLookAndFeel(WINDOWS);
}
catch (Exception ex) {}
DevelopmentRunner.setApplicationClass(ULCCardPaneSnippet.class);
DevelopmentRunner.main(args);
}
}