Professional Documents
Culture Documents
html
Uneditable combo box, before (top) Editable combo box, before and after and after the button is clicked the arrow button is clicked
"ombo boxes re#uire little screen space, and their editable $text field% form is useful for letting the user #uickly choose a value without limiting the user to the displayed values. &ther components that can display oneof-many choices are groups of radio buttons and lists. 'roups of radio buttons are generally the easiest for users to understand, but combo boxes can be more appropriate when space is limited or more than a few choices are available. (ists are not terribly attractive, but they're more appropriate than combo boxes when the number of items is large $say, over )*% or when selecting multiple items might be valid.
Contoh Aplikasi
-ari gambar ini, package components berisi /rame bernama "ombo+ox.,ava. package components.images berisi lima buah gambar $l+ird.gif, "at.gif, -og.gif, 0ig.gif, 1abbit.gif%. .eting komponen!
3o 2 4
4omponen J3rame JPane& ($nt$5 diisi JComboBox dan JLabe&) JComboBox (mas$55an 5e JPane&) JLabe& (mas$55an 5e JPane&)
Contoh Aplikasi:
5. +uat pro,ect dengan sebuah frame $"ombo+ox-emo)% ). .truktur pro,ect!
6. .eting komponen! 3o 2 4 6 8 4omponen J3rame JLabe& JLabe& JComboBox JLabe& JLabe& 0roperti tit&e text text variab&e editab&e mode& text variab&e text border 3ilai ComboBox"emo4 :as$55an po&a wa5t$ ata$ pi&i% sat$ dari daftar cbPo&a diberi tanda cek (true) dikosongkan an''a&/Jam se5aran' &abe&;asi& (di5oson'5an) Compo$ndBorder( -nside Border( Empt# Border <=7=7=7=> !$tside Border( LineBorder
pac5a'e components+ // 5ode #an' ditamba%5an( import java.$ti&.?+ import java.text.*imp&e"ate3ormat+ import java.awt.?+ 8. 0ada konstruktor, tambahkan kode berikut $dicetak tebal%, p$b&ic ComboBox4() { initComponents()+ polaSekarang = pola !"#; for(int i="; i$pola .lengt%; i&&) cbPola.addItem(pola !i#); 1 9. 0ada bagian bawah $deklarasi variabel%, tambahkan kode berikut $dicetak tebal%, // @ariab&es dec&aration A do not modif# private javax.swin'.JComboBox cbPo&a+ private javax.swin'.JLabe& jLabe&2+ private javax.swin'.JLabe& jLabe&4+ private javax.swin'.JLabe& jLabe&6+ private javax.swin'.JLabe& &abe&;asi&+ // End of variab&es dec&aration String!# pola = ' (dd ))))) ****(+ (dd.)).**(+ ())/dd/**(+ (****.)).dd , -at- %%.mm.ss /(+ (000+ ))) d+ --**(+ (%.mm a(+ (1.mm.ss.SSS(+ (2.mm a+/(+ (****.))))).dd ,,, %%.mm aaa( 3; String polaSekarang;
:. Tambahkan method reformat$% berikut tepat di atas method main$%, /?? 3ormats and disp&a#s toda#Bs date. ?/ p$b&ic void reformat() { "ate toda# ) new "ate()+ *imp&e"ate3ormat formatter ) new *imp&e"ate3ormat(po&a*e5aran')+ tr# { *trin' date*trin' ) formatter.format(toda#)+
&abe&;asi&.set3ore'ro$nd(Co&or.b&ac5)+ &abe&;asi&.set ext(date*trin')+ 1 catc% (-&&e'a&Ar'$mentException iae) { &abe&;asi&.set3ore'ro$nd(Co&or.red)+ &abe&;asi&.set ext(.Error( . / iae.'et:essa'e())+ 1
;. Tambahkan event! action0erformed pada cb0ola! private void cbPo&aActionPerformed(java.awt.event.ActionEvent evt) { // !"! add #o$r %and&in' code %ere( javax.swin'.JComboBox cb ) (javax.swin'.JComboBox)evt.'et*o$rce()+ *trin' pi&i%an ) (*trin')cb.'et*e&ected-tem()+ po&a*e5aran' ) pi&i%an+ reformat()+ 1
"etting or (etting the 'tems in the $ombo )ox*s +enu $ustomi,ing the $ombo )ox*s peration "etting or (etting the 'tems in the $ombo )oxes*s +enu $ethod "urpose
$reate a combo box with the specified items in its menu. % combo box created with the default #$ombo)ox() #$ombo)ox($ombo)ox+odel) constructor has no items in the menu initially. 0ach of the other constructors initiali,es the menu from #$ombo)ox( bject-.) its argument: a model object! an array of objects! or #$ombo)ox(/ector) a @ector of objects. void add'tem( bject) void insert'tem%t( bject! int) %dd or insert the specified object into the combo box*s menu. The insert method places the specified object at the specified index! thus inserting it before the object currently at that index. These methods
re1uire that the combo box*s data model be an instance of :$tab&eComboBox:ode&. bject get'tem%t(int) bject get"elected'tem() void remove%ll'tems() void remove'tem%t(int) void remove'tem( bject) int get'tem$ount() void set+odel($ombo)ox+odel) $ombo)ox+odel get+odel() void set%ction(%ction) %ction get%ction() (et an item from the combo box*s menu. 2emove one or more items from the combo box*s menu. These methods re1uire that the combo box*s data model be an instance of :$tab&eComboBox:ode&. (et the number of items in the combo box*s menu. "et or get the data model that provides the items in the combo box*s menu. "et or get the Action associated with the combo box. 3or further information! see 4ow to 5se %ctions. "urpose
$ustomi,ing the $ombo )ox*s peration $ethod or Constructor %dd an action listener to the combo box. The listener*s actionPerformed method is called void when the user selects an item from the combo add%ction6istener(%ction6istener) box*s menu or! in an editable combo box! when the user presses 0nter. %dd an item listener to the combo box. The listener*s item*tateC%an'ed method is called void add'tem6istener('tem6istener) when the selection state of any of the combo box*s items change. void set0ditable(boolean) boolean is0ditable() "et or get whether the user can type in the combo box.
"et or get the object responsible for painting the selected item in the combo box. The renderer is void set2enderer(6ist$ell2enderer) used only when the combo box is uneditable. 'f 6ist$ell2enderer get2enderer() the combo box is editable! the editor is used to paint the selected item instead. "et or get the object responsible for painting and editing the selected item in the combo box. The editor is used only when the combo box is editable. 'f the combo box is uneditable! the renderer is used to paint the selected item instead.
This table shows the examples that use JComboBox and where those examples are described. Example
ComboBox"emo ComboBox"emo4 C$stomComboBox"emo ab&e0ender"emo
%here &escribed This section This section This section 4ow to 5se Tables (5sing a $ombo )ox as an 0ditor)
'otes 5ses an uneditable combo box. 5ses an editable combo box. &rovides a custom renderer for a combo box. "hows how to use a combo box as a table cell editor.