hi there,
am learning Swing and am playing with CardLayout.
i am controlling the currently displayed 'card' with next/previous
buttons.
i need to know what the currently displayed 'card' because, depending
on which 'card' is displayed the next/previous may need to skip a
'card', ie movement through the cards is not linear.
am unable to find any methods in the API that gets the currently
displayed 'card'.
any hints are greatly appreciated.
thanx for your help
Andrew Thompson - 20 May 2005 11:01 GMT
> am learning Swing and am playing with CardLayout.
CardLayout is an AWT layout.
> i need to know what the currently displayed 'card' because, depending
> on which 'card' is displayed the next/previous may need to skip a
> 'card', ie movement through the cards is not linear.
Something like this might get you started..
<sscce>
import java.awt.*;
public class TestCardLayout extends Panel {
CardLayout cl;
int num = 10;
Label[] label;
public TestCardLayout () {
super();
cl = new CardLayout();
setLayout( cl );
label = new Label[num];
for (int ii=0; ii<num; ii++) {
String s = "label " + (ii+1);
label[ii] = new Label( s );
this.add( label[ii], s );
System.out.println( "ii: " + ii + " \tstring: " + s );
}
cl.next( this );
cl.show( this, "label 5" );
System.out.println( "The index of the visible component is "
+ getVisibleComponent() );
}
public int getVisibleComponent() {
int i = 0;
while (i<num) {
if ( label[i].isVisible() ) return i;
else i++;
}
return -1;
}
public static void main(String[] args) {
Frame f = new Frame("Test Card Layout");
f.setLayout(new BorderLayout());
f.add( new TestCardLayout(), BorderLayout.CENTER );
f.pack();
f.setSize( f.getPreferredSize() );
f.setVisible(true);
}
}
</sscce>
HTH

Signature
Andrew Thompson
http://www.PhySci.org/codes/ Web & IT Help
http://www.PhySci.org/ Open-source software suite
http://www.1point1C.org/ Science & Technology
http://www.LensEscapes.com/ Images that escape the mundane
Thomas Weidenfeller - 20 May 2005 13:08 GMT
> i need to know what the currently displayed 'card' because, depending
> on which 'card' is displayed the next/previous may need to skip a
> 'card', ie movement through the cards is not linear.
>
> am unable to find any methods in the API that gets the currently
> displayed 'card'.
The easiest way is to do your own counting, which is rather simple. Give
the card names, and forget about next(), previous() and just use show().
But if you really want to query the setup via the Java API, then the
only way I am aware of is rather ugly:
JPanel panel = ...
...
for(int i = 0; i < panel.getComponentCount(); i++) {
if(panel.getComponent(i).isVisible()) {
//
// found currently shown component
// ...
}
}
/Thomas

Signature
The comp.lang.java.gui FAQ:
ftp://ftp.cs.uu.nl/pub/NEWS.ANSWERS/computer-lang/java/gui/faq
Christian Kaufhold - 21 May 2005 12:15 GMT
> i need to know what the currently displayed 'card' because, depending
> on which 'card' is displayed the next/previous may need to skip a
[quoted text clipped - 4 lines]
>
> any hints are greatly appreciated.
http://groups.google.com/groups?hl=de&lr=&frame=right&th=b191417de497efba&seekm=
1t3e416085i7d73n1f4%40simia.chka.de#link1
Christian

Signature
The cat in the hat came back, wrecked a lot of havoc on the way
Always had a smile and a reason to pretend
R.E.M., The Sidewinder Sleeps Tonight