スクロール可能なリスト用widgetであるHListGroupのサンプルコード。
Main.java
import java.awt.Dimension;
import java.awt.Font;
import java.awt.event.FocusEvent;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import javax.tv.xlet.Xlet;
import javax.tv.xlet.XletContext;
import org.havi.ui.HListElement;
import org.havi.ui.HListGroup;
import org.havi.ui.HScene;
import org.havi.ui.HSceneFactory;
import org.havi.ui.HScreen;
import org.havi.ui.HVisible;
import org.havi.ui.event.HFocusListener;
public class Main implements Xlet {
public void initXlet(XletContext context){
try {
Font font = new Font(null, java.awt.Font.PLAIN, 48);
HScene hs= HSceneFactory.getInstance().getFullScreenScene(
HScreen.getDefaultHScreen().getDefaultHGraphicsDevice());
hs.setLayout(null);
HListGroup hlg = new HListGroup(null, 100, 100, hs.getWidth()-200, hs.getHeight()-200);
hlg.setFont(font);
hlg.setBordersEnabled(true);
hlg.setHorizontalAlignment(HVisible.HALIGN_LEFT);
hlg.setVerticalAlignment(HVisible.VALIGN_TOP);
hlg.setLabelSize(new Dimension(hs.getWidth()-200,50));
for( int i = 0; i < 100; i++ ){
hlg.addItem(new HListElement("テスト-"+Integer.toString(i)), i);
}
hs.add(hlg);
hs.validate();
hlg.addHFocusListener(new HFocusListener(){
public void focusGained(FocusEvent arg0) {
HListGroup hlg = (HListGroup)arg0.getComponent();
// フォーカスがきたら、自動的に0番目のitemを選択させる
hlg.setSelectionMode(true);
hlg.dispatchEvent(new KeyEvent(hlg, KeyEvent.KEY_PRESSED, 0L, 0, KeyEvent.VK_ENTER, ' '));
}
public void focusLost(FocusEvent arg0) {}
});
hlg.addKeyListener( new KeyListener(){
public void keyPressed(KeyEvent arg0) {}
public void keyReleased(KeyEvent arg0) {
if( arg0.getKeyCode() == KeyEvent.VK_ENTER ){
HListGroup hlg = (HListGroup)arg0.getComponent();
int index = hlg.getCurrentIndex();
hlg.setItemSelected(index, !hlg.isItemSelected(index) ); // itemを選択状態にする
}
}
public void keyTyped(KeyEvent arg0) {}
});
hs.setVisible(true);
hlg.requestFocus();
} catch (Throwable thr){
}
}
public void startXlet(){ }//
public void pauseXlet(){ }
public void destroyXlet(boolean unconditional){}
}
関連する項目
- Interactive TV Standards: HAViのウィジェットについて詳しい