セルを選択する
public void selectCell(int vrow, int vcol){
table.getSelectionModel().setSelectionInterval(vrow,vrow);
table.getColumnModel().getSelectionModel().setSelectionInterval(vcol,vcol);
}
エンターキーで、次の列へ移動する
public void setEnterForNextColumn(boolean enable){
String action = "";
if (enable){
action = "selectNextColumnCell";
} else {
action = "selectNextRowCell";
}
KeyStroke enterPress = KeyStroke.getKeyStroke(KeyEvent.VK_ENTER, 0, false);
InputMap im = table.getInputMap(JTable.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT);
im.put(enterPress,action);
}
列の幅の調整
setAutoResizeModeでAUTO_RESIZE_SUBSEQUENT_COLUMNSを設定した場合、
各列に指定された列幅に対して、同じ幅を追加(削除)して幅を合わせる。
例えば、
・テーブル幅100
・列1幅:20
・列2幅:10
とした場合、幅100に合わせるために 100 - (20 + 10) = 70 を各列に割り振る。
結果、
・列1幅:20 + (70 / 2) = 55
・列2幅:10 + (70 / 2) = 45
となる。(みたい)
もし、列1と列2を 2:1 の比率で表示したい場合は
・列1幅:60
・列2幅:30
くらいで設定するとおおよそ、 2:1 になる。
JTableで一行毎に色を変える
http://guijava.180r.com/pJTable%A4%C7%B0%EC%B9%D4%CB%E8%A4%CB%BF%A7%A4%F2%CA%D1%A4%A8%A4%EB.html
1 |
public class BirowTableRenderer extends DefaultTableCellRenderer{
private static final Color BIROW_COLOR = new Color(204, 204, 255);
private static final Color ORG_COLOR=Color.WHITE;
public Component getTableCellRendererComponent(JTable tb,
Object val,boolean isSelected,
boolean hasFocus,int r,int c){
if(r % 2 == 0){
setBackground(BIROW_COLOR);
}else{
setBackground(ORG_COLOR);
}
return super.getTableCellRendererComponent(tb,val,
isSelected,hasFocus,r,c);
}
} |
レンダラー設定
1 |
JTable tb = getTestTable(); tb.setDefaultRenderer(Object.class,new BirowTableRenderer()); |
再描画(更新)
revalidate で更新されない場合は
updateUI
を使ってみる。
[通知用URL]
Tweet
最終更新時間:2010年12月16日 23時00分50秒