Professional Documents
Culture Documents
14 - Java Swing Kontrole
14 - Java Swing Kontrole
JAVA
1
Swing komponente
JAVA
2
Kontejneri najvieg nivoa
Dialog
JAVA
3
Dialog (JOptionPane)
JAVA
4
Konstante (members)
JAVA
5
Tasteri (buttons)
JAVA
6
Ikone (icons)
JAVA
7
showMessageDialog
poruka (message)
naslov (title)
JOptionPane.showMessageDialog(this,"1. plda-
hiba!","A",JOptionPane.ERROR_MESSAGE);
JOptionPane.showMessageDialog(this,"2. plda-
informci!","B",JOptionPane.INFORMATION_MES
SAGE);
JAVA
9
Jedan primer nastavak...
JOptionPane.showMessageDialog(this,"3. plda-
figyelmeztets!","C",JOptionPane.WARNING_MES
SAGE);
JOptionPane.showMessageDialog(this,"4. plda-
krds!","D",JOptionPane.QUESTION_MESSAGE);
JAVA
10
Jedan primer nastavak...
JOptionPane.showMessageDialog(this,"5. plda-
hiba!","E",JOptionPane.PLAIN_MESSAGE);
JAVA
11
Confirm Dialog (JOptionPane)
poruka (message)
naslov (title)
JAVA
12
Confirm Dialog (JOptionPane)
JAVA
13
showOptionDialog metod
JAVA
14
Primer:
int response;
response = JOptionPane.showConfirmDialog(null, "This is an example of
an confirm dialog box.", "Example", JOptionPane.YES_NO_OPTION,
JOptionPane.INFORMATION_MESSAGE);
if (response == JOptionPane.YES_OPTION)
{
// odabrano je Yes
}
else if (response == JOptionPane.NO_OPTION)
{
// odabrano je No
}
else
{
// prozor je zatvoren bez klika na tastere
}
JAVA
15
showOptionDialog
JAVA
16
showOptionDialog primer
JOptionPane.showOptionDialog(null,
Tekst ...",Naslov...",
JOptionPane.YES_NO_OPTION,
JOptionPane.QUESTION_MESSAGE,
new ImageIcon("smalllogo.gif"), //(mesto)+ime slike
opcije, //natpisi na tasterima
opcije[1] //pokazuje koji je taster default
);
JAVA
17
showOptionDialog
JAVA
20
JAVA
21
Ako izostavimo opcije, i na to mesto stavimo null dobijamo
sledeu formu:
JAVA
22
Kod prethodnog primera:
import javax.swing.*;
import java.awt.*;
// b)
// confirm dialog
JOptionPane.showConfirmDialog(this,"Ovde treba da je pitanje");
JAVA
23
if (JOptionPane.showConfirmDialog(this,"Pitanje?","...",
JOptionPane.YES_NO_CANCEL_OPTION,JOptionPane.QUESTION_MESSAGE)==JOptionPane.NO_OPTION)
{//
}
// c)
String[] opciok = {"U redu", "Problem!"};
JAVA
24
// d)
String osszegStr = JOptionPane.showInputDialog(null,
"Koliko je 3+2?","Matematika",
JOptionPane.QUESTION_MESSAGE);
try {
int osszeg = Integer.parseInt(osszegStr);
}
catch (Exception ex) {
}
JAVA
25
//ako dobijemo string
if ((s != null) && (s.length() > 0)) {
setTitle("Volim kajganu sa... " + s + "!");
return;
}
//ako ne dobijemo nista nazad
setTitle("Kliknuto je na Cancel!");
}
public static void main(String[] args) {
new Dialog();
}
}
JAVA
26
JFrame Object
JAVA
27
JFrame Object- osobine, metode
i dogaaji
Osobine (properties) opisuju izgled i vrednosti kontrole,
metodi (methods) su akcije koje mogu da se primene na
kontrole, a dogaaji (events) nastaju kada se neto uradi
kontroli (npr. klikne na kontrolu), najee od strane
korisnika.
JAVA
28
Osobine (properties)
JAVA
29
Metode
JAVA
30
Dogaaji
addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent e)
{
exitForm(e);
}
});
JAVA
31
Koraci pri stvaranju prozora
JAVA
32
Primer
JAVA
33
Centriranje prozora
JAVA
34
Centriranje prozora
JAVA
35
Centriranje prozora
Ovaj kod treba da se nalazi posle pack metode u kodu koji uspostavlja
prozor, kako bi se koristila odgovarajua veliina prozora
JAVA
36
GridBagLayout manager
JAVA
37
GridBagLayout manager...
JAVA
38
JAVA
39
GridBagLayout manager...
JAVA
40
GridBagLayout manager...
gridConstraints.ipadx = 5;
gridConstraints.ipady = 5;
JAVA
41
GridBagLayout manager...
JAVA
42
GridBagLayout manager...
JAVA
43
Kontejner opte namene
JAVA
44
JPanel Control
JAVA
45
JPanel Control
JAVA
46
Osnovne kontrole
JAVA
47
JButton Control
Osobine (Properties):
text string koji se prikazuje na tasteru.
font ime fonta, stil i veliina.
background boja pozadine.
foreground boja teksta.
icon slika koja se prikazuje na tasteru
enabled ako je false, taster je vidljiv ali ne moe da se klikne na njega.
visible ako je false, skriva taster.
JAVA
48
JButton Control . . .
Metode:
setText postavlja tekst na taster.
setFont postavlja font.
setBackground postavlja boju pozadine.
setForeground postavlja boju teksta.
setEnabled postavlja logiku vrednost koja ukazuje na to da li
se moe ili ne kliknuti na taster.
setVisible postavlja logiku vrednost da li je taster vidljiv ili ne.
doClick generie klik dogaaj za taster.
JAVA
49
JLabel Control
JAVA
50
JLabel Control
Metode:
setText postavlja tekst labele.
setFont postavlja font.
setBackground postavlja boju pozadine labele.
setForeground postavlja boju teksta.
setOpaque ako je true, mogu se primeniti boje pozadine.
setHorizontalAlignment postavlja poravnanje teksta.
setVerticalAlignment postavlja vertikalno poravnanje teksta.
setBorder koristi se za postavljanje okvira oko labele.
Dogaaj:
mouseClicked dogaaj (MouseEvent) koji se aktivira kada se klikne na
labelu. Dodaje se sa MouseListener koristei
MouseAdapter.
JAVA
51
JLabel Control
JAVA
52
JTextField Control
JAVA
53
JTextField Control
Metode:
setText postavlja tekst u tekst polje.
getText preuzima tekst iz tekst polja.
setFont postavlja font.
setBackground postavlja boju pozadine.
setForeground postavlja boju teksta.
setColumns postavlja broj kolona.
setHorizontalAlignment postavlja horizontalno poravnanje.
setEditable ako je postavljeno na false, tekst polje ne moe se editovati.
Za horizontalAlignment mogue su sledee vrednosti:
SwingConstants.LEFT tekst levo poravnat
SwingConstants.CENTER tekst centriran
SwingConstants.RIGHT tekst desno poravnat
JAVA
54
JTextArea Control
Ova kontrola omoguava prikaz teksta u vie linija. Prikazani tekst moe se
menjati.
Osobine:
text String displayed in text area.
font Font name, style, size.
background Text area background color.
foreground Color of text.
columns Displayed width of text area.
rows Displayed height of text area.
lineCount Number of lines of text.
lineWrap Boolean variable indicating if text should be word wrapped
default is false.
wrapStyleWord If true (and lineWrap is true), words are wrapped at word
boundaries.
editable Indicates whether text in the text area is read-only.
JAVA
55
JTextArea Control
Metode:
setText Sets the text area text.
getText Retrieves the text area text.
setFont Sets font name, style, size.
setBackground Sets the text area background color.
setForeground Sets color of text.
setColumns Sets the number of columns.
setRows Sets the number of rows.
setLineWrap Turns line wrap on and off.
setWrapStyleWord Turns wrap style on and off.
setEditable If set to false, text area cannot be edited.
JAVA
56
JCheckBox Control
JAVA
57
JCheckBox Control
Metode:
setText Sets the check box text.
setFont Sets font name, style, size.
setBackground Sets the check box background color.
setForeground Sets color of text.
setOpaque If true, background colors can be applied.
setSelected Sets whether box is selected or not.
isSelected If true, check box is selected.
doClick Generates a click event for a check box.
JAVA
58
JRadioButton Control
JAVA
59
JRadioButton Control
Metode:
setText Sets the radio button text.
setFont Sets font name, style, size.
setBackground Sets the button background color.
setForeground Sets color of text.
setOpaque If true, background colors can be applied.
setSelected Sets whether button is selected or not.
isSelected If true, radio button is selected.
doClick Generates a click event for a radio button.
JAVA
60
JRadioButton Control
JAVA
61
Niz kontrola
JAVA
62
Primer:
JAVA
63
Primer:
JAVA
64
Primer:
JAVA
65
Postavljanje osnovnog prozora
JAVA
66
Kod osnovnog prozora:
/*
* Pizza.java
*/
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class Pizza extends javax.swing.JFrame
{
public Pizza()
{
setTitle("Pizza Order");
setResizable(false);
JAVA
67
Kod osnovnog prozora:
getContentPane().setLayout(new GridBagLayout());
Dimension screenSize =
Toolkit.getDefaultToolkit().getScreenSize();
setBounds((int) (0.5 * (screenSize.width - getWidth())),
(int) (0.5 * (screenSize.height - getHeight())),
getWidth(), getHeight());
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
pack();
}
}
JAVA
68
Pravljenje panela sizePanel
JAVA
69
sizePanel, postavljanje kontrola i
dodavanje odgovarajuih dogaaja
sizePanel.setLayout(new GridBagLayout());
sizePanel.setBorder(BorderFactory.createTitledBorder("Size"));
smallRadioButton.setText("Small");
smallRadioButton.setSelected(true);
sizeButtonGroup.add(smallRadioButton);
GridBagConstraints gridConstraints = new GridBagConstraints();
gridConstraints.gridx = 0;
gridConstraints.gridy = 0;
gridConstraints.anchor = GridBagConstraints.WEST;
sizePanel.add(smallRadioButton, gridConstraints);
JAVA
70
sizePanel...
smallRadioButton.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
sizeRadioButtonActionPerformed(e);
}
});
mediumRadioButton.setText("Medium");
sizeButtonGroup.add(mediumRadioButton);
gridConstraints = new GridBagConstraints();
gridConstraints.gridx = 0;
gridConstraints.gridy = 1;
gridConstraints.anchor = GridBagConstraints.WEST;
sizePanel.add(mediumRadioButton, gridConstraints);
JAVA
71
sizePanel...
mediumRadioButton.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
sizeRadioButtonActionPerformed(e);
}
});
largeRadioButton.setText("Large");
largeRadioButton.setSelected(true);
sizeButtonGroup.add(largeRadioButton);
gridConstraints = new GridBagConstraints();
gridConstraints.gridx = 0;
gridConstraints.gridy = 2;
JAVA
72
sizePanel...
gridConstraints.anchor = GridBagConstraints.WEST;
sizePanel.add(largeRadioButton, gridConstraints);
largeRadioButton.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
sizeRadioButtonActionPerformed(e);
}
});
gridConstraints = new GridBagConstraints();
gridConstraints.gridx = 0;
gridConstraints.gridy = 0;
getContentPane().add(sizePanel, gridConstraints);
JAVA
73
sizePanel
JAVA
74
Rezultat prethodnog koda
JAVA
75
crustPanel
JAVA
76
crustPanel, dodavanje kontrola i
odgovarajuih dogaaja
crustPanel.setLayout(new GridBagLayout());
crustPanel.setBorder(BorderFactory.createTitledBorder("Crust"));
thinRadioButton.setText("Thin Crust");
thinRadioButton.setSelected(true);
crustButtonGroup.add(thinRadioButton);
gridConstraints = new GridBagConstraints();
gridConstraints.gridx = 0;
gridConstraints.gridy = 0;
gridConstraints.anchor = GridBagConstraints.WEST;
crustPanel.add(thinRadioButton, gridConstraints);
JAVA
77
crustPanel...
thinRadioButton.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
crustRadioButtonActionPerformed(e);
}
});
thickRadioButton.setText("Thick Crust");
crustButtonGroup.add(thickRadioButton);
gridConstraints = new GridBagConstraints();
gridConstraints.gridx = 0;
gridConstraints.gridy = 1;
gridConstraints.anchor = GridBagConstraints.WEST;
crustPanel.add(thickRadioButton, gridConstraints);
JAVA
78
crustPanel...
thickRadioButton.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
crustRadioButtonActionPerformed(e);
}
});
gridConstraints = new GridBagConstraints();
gridConstraints.gridx = 0;
gridConstraints.gridy = 1;
getContentPane().add(crustPanel, gridConstraints);
JAVA
79
crustPanel...
JAVA
80
Rezultat prethodnog koda
JAVA
81
toppingsPanel
JAVA
82
toppingsPanel
toppingsPanel.setLayout(new GridBagLayout());
toppingsPanel.setBorder(BorderFactory.createTitledBorder("Toppings"));
cheeseCheckBox.setText("Extra Cheese");
gridConstraints = new GridBagConstraints();
gridConstraints.gridx = 0;
gridConstraints.gridy = 0;
gridConstraints.anchor = GridBagConstraints.WEST;
toppingsPanel.add(cheeseCheckBox, gridConstraints);
mushroomsCheckBox.setText("Mushrooms");
gridConstraints = new GridBagConstraints();
gridConstraints.gridx = 0;
gridConstraints.gridy = 1;
JAVA
83
toppingsPanel
gridConstraints.anchor = GridBagConstraints.WEST;
toppingsPanel.add(mushroomsCheckBox, gridConstraints);
olivesCheckBox.setText("Olives");
gridConstraints = new GridBagConstraints();
gridConstraints.gridx = 0;
gridConstraints.gridy = 2;
gridConstraints.anchor = GridBagConstraints.WEST;
toppingsPanel.add(olivesCheckBox, gridConstraints);
onionsCheckBox.setText("Onions");
gridConstraints = new GridBagConstraints();
gridConstraints.gridx = 1;
gridConstraints.gridy = 0;
gridConstraints.anchor = GridBagConstraints.WEST;
toppingsPanel.add(onionsCheckBox, gridConstraints);
peppersCheckBox.setText("Green Peppers");
JAVA
84
gridConstraints = new GridBagConstraints();
gridConstraints.gridx = 1;
gridConstraints.gridy = 1;
gridConstraints.anchor = GridBagConstraints.WEST;
toppingsPanel.add(peppersCheckBox, gridConstraints);
tomatoesCheckBox.setText("Tomatoes");
gridConstraints = new GridBagConstraints();
gridConstraints.gridx = 1;
gridConstraints.gridy = 2;
gridConstraints.anchor = GridBagConstraints.WEST;
toppingsPanel.add(tomatoesCheckBox, gridConstraints);
gridConstraints = new GridBagConstraints();
gridConstraints.gridx = 1;
gridConstraints.gridy = 0;
gridConstraints.gridwidth = 2;
getContentPane().add(toppingsPanel, gridConstraints);
JAVA
85
Rezultat prethodnog koda
JAVA
86
Dodavanje dva radio button-a i
dva tastera
static ButtonGroup whereButtonGroup = new ButtonGroup();
static JRadioButton eatInRadioButton = new JRadioButton();
static JRadioButton takeOutRadioButton = new JRadioButton();
static JButton buildButton = new JButton();
static JButton exitButton = new JButton();
whereButtonGroup = new ButtonGroup();
eatInRadioButton = new JRadioButton();
takeOutRadioButton = new JRadioButton();
buildButton = new JButton();
exitButton = new JButton();
JAVA
87
Postavljanje kontrola i njihovih
metoda
eatInRadioButton.setText("Eat In");
eatInRadioButton.setSelected(true);
whereButtonGroup.add(eatInRadioButton);
gridConstraints = new GridBagConstraints();
gridConstraints.gridx = 1;
gridConstraints.gridy = 1;
gridConstraints.anchor = GridBagConstraints.WEST;
getContentPane().add(eatInRadioButton, gridConstraints);
eatInRadioButton.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
whereRadioButtonActionPerformed(e);
}
});
JAVA
88
Postavljanje kontrola i njihovih
metoda
takeOutRadioButton.setText("Take Out");
whereButtonGroup.add(takeOutRadioButton);
gridConstraints = new GridBagConstraints();
gridConstraints.gridx = 2;
gridConstraints.gridy = 1;
gridConstraints.anchor = GridBagConstraints.WEST;
getContentPane().add(takeOutRadioButton, gridConstraints);
JAVA
89
Postavljanje kontrola i njihovih
metoda
takeOutRadioButton.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
whereRadioButtonActionPerformed(e);
}
});
buildButton.setText("Build Pizza");
gridConstraints = new GridBagConstraints();
gridConstraints.gridx = 1;
gridConstraints.gridy = 2;
getContentPane().add(buildButton, gridConstraints);
buildButton.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
buildButtonActionPerformed(e);
}
});
JAVA
90
Postavljanje kontrola i njihovih
metoda
exitButton.setText("Exit");
gridConstraints = new GridBagConstraints();
gridConstraints.gridx = 2;
gridConstraints.gridy = 2;
getContentPane().add(exitButton, gridConstraints);
exitButton.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
exitButtonActionPerformed(e);
}
});
JAVA
91
private void whereRadioButtonActionPerformed(ActionEvent e)
{
}
private void buildButtonActionPerformed(ActionEvent e)
{
}
private void exitButtonActionPerformed(ActionEvent e)
{
}
JAVA
92
Rezultat prethodnog koda
JAVA
93
Deklarisanje etiri globalne
promenljive
static String pizzaSize;
static String pizzaCrust;
static String pizzaWhere;
static JCheckBox[] topping = new JCheckBox[6];
JAVA
94
Inicijalizacija promenljivih
// Initialize parameters
pizzaSize = smallRadioButton.getText();
pizzaCrust = thinRadioButton.getText();
pizzaWhere = eatInRadioButton.getText();
// Define an array of topping check boxes
topping[0] = cheeseCheckBox;
topping[1] = mushroomsCheckBox;
topping[2] = olivesCheckBox;
topping[3] = onionsCheckBox;
topping[4] = peppersCheckBox;
topping[5] = tomatoesCheckBox;
JAVA
95
Metode za svaku od tri grupe
radio button-a:
private void sizeRadioButtonActionPerformed(ActionEvent e)
{
pizzaSize = e.getActionCommand();
}
private void crustRadioButtonActionPerformed(ActionEvent e)
{
pizzaCrust = e.getActionCommand();
}
private void whereRadioButtonActionPerformed(ActionEvent e)
{
pizzaWhere = e.getActionCommand();
}
JAVA
96
Poruka koja se javlja u message
box-u
private void buildButtonActionPerformed(ActionEvent e)
{
// This procedure builds a confirm dialog box that displays your pizza type
String message;
message = pizzaWhere + "\n";
message += pizzaSize + " Pizza" + "\n";
message += pizzaCrust + "\n";
// Check each topping using the array we set up
for (int i = 0; i < 6; i++)
{
if (topping[i].isSelected())
{
message += topping[i].getText() + "\n";
}
}
JOptionPane.showConfirmDialog(null, message, "Your Pizza",
JOptionPane.DEFAULT_OPTION, JOptionPane.INFORMATION_MESSAGE);
}
JAVA
97
private void exitButtonActionPerformed(ActionEvent e)
{
System.exit(0);
}
JAVA
98
Kraj
JAVA
99
JList Control
JAVA
100
JList Control
Metode:
setFont Sets font name, style, size.
setBackground Sets the list background color.
setForeground Sets color of text.
setVisibleRowCount Sets number of rows to display.
clearSelection Unselects specified in the list.
getSelectedIndex Returns a value indicating whether the
specified item is selected.
getSelectedIndices Returns an integer array of indices of selected
items.
setSelectedIndex Selects specified item in a list.
setSelectionMode Establishes selection mode.
isSelectionEmpty Checks to see if any items are selected.
JAVA
101
JList Control
JAVA
102
JList Control
JAVA
103
JList Control
JAVA
104
JList Control
JAVA
105
JList Control
myList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
JAVA
106
JScrollPane Control
JAVA
107
JScrollPane Control
Metode:
setEnabled Sets whether panel is enabled.
setPreferredSize Establishes size of scroll pane.
setViewportView Establishes component hosted by the scroll pane.
setHorizontalScrollBarPolicy Establishes how horizontal scroll bar (if any) is
displayed.
setVerticalScrollBarPolicy Establishes how horizontal scroll bar (if any) is
displayed.
JAVA
108
JScrollPane Control
JAVA
109
JScrollPane Control
JAVA
110
JScrollPane Control
JAVA
111
JComboBox Control
JAVA
112
JComboBox Control
Osobine:
model Establishes the items in the drop-down list portion.
itemCount Number of items in combo box.
font Font name, style, size.
background Combo box background color.
foreground Color of text.
editable Specifies if selection can be typed by user (default is false).
maximumRowCount Number of rows to display in drop-down box (scroll bar
will automatically appear if there are more items than space).
selectedIndex Zero-based index of the currently selected item in combo box.
selectedItem Currently selected item in the combo box.
JAVA
113
JComboBox Control
Metode:
setEditable Establishes whether selected item can be edited.
getItemCount Number of items in combo box .
setMaximumRowCount Sets number of items to display in drop-down box.
setFont Sets font name, style, size.
setBackground Sets the combo box background color.
setForeground Sets color of text.
getSelectedItem Retrieves the selected item.
setSelectedIndex Selects specified item in combo box.
Dogaaj:
actionPerformed dogaaj nastaje kada korisnik naini izbor ili pritisne
<Enter>. Dodat sa ActionListener.
JAVA
114
JComboBox Control
JAVA
115
JComboBox Control
JAVA
116
Primer: Flight Planner
JAVA
117
U prozor se postavlja scroll panel (u kome se nalazi list
kontrola), dva combo box-a, tri labele i dva tastera.
GridBagLayout manager ima sledei izgled:
JAVA
118
Kod osnovnog prozora
/*
* Flight.java
*/
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class Flight extends JFrame
{
public static void main(String args[])
{
// construct frame
new Flight().show();
}
public Flight()
{
// create frame
setTitle("Flight Planner");
setResizable(false);
JAVA
119
Kod osnovnog prozora
addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent e)
{
exitForm(e);
}
});
getContentPane().setLayout(new GridBagLayout());
pack();
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
setBounds((int) (0.5 * (screenSize.width - getWidth())), (int) (0.5 *
(screenSize.height - getHeight())), getWidth(), getHeight());
}
private void exitForm(WindowEvent e)
{
System.exit(0);
}
}
JAVA
120
Scroll panel sa list kontrolom u
kojoj su imena gradova
static JLabel citiesLabel = new JLabel();
static JList citiesList = new JList();
static JScrollPane citiesScrollPane = new JScrollPane();
JAVA
121
Pozicioniranje i dodavanje
kontrola u prozor
GridBagConstraints gridConstraints;
citiesLabel.setText("Destination City");
gridConstraints = new GridBagConstraints();
gridConstraints.gridx = 0;
gridConstraints.gridy = 0;
gridConstraints.insets = new Insets(10, 0, 0, 0);
getContentPane().add(citiesLabel, gridConstraints);
citiesScrollPane.setPreferredSize(new Dimension(150, 100));
citiesScrollPane.setViewportView(citiesList);
gridConstraints = new GridBagConstraints();
gridConstraints.gridx = 0;
gridConstraints.gridy = 1;
gridConstraints.insets = new Insets(10, 10, 10, 10);
getContentPane().add(citiesScrollPane, gridConstraints);
JAVA
122
Dodavanje elemenata u list kontrolu i
postavljanje elementa koji e biti prvi
prikazan
DefaultListModel citiesListModel = new DefaultListModel();
citiesListModel.addElement("San Diego");
citiesListModel.addElement("Los Angeles");
citiesListModel.addElement("Orange County");
citiesListModel.addElement("Ontario");
citiesListModel.addElement("Bakersfield");
citiesListModel.addElement("Oakland");
citiesListModel.addElement("Sacramento");
citiesListModel.addElement("San Jose");
citiesListModel.addElement("San Francisco");
citiesListModel.addElement("Eureka");
citiesListModel.addElement("Eugene");
citiesListModel.addElement("Portland");
citiesListModel.addElement("Spokane");
citiesListModel.addElement("Seattle");
citiesList.setModel(citiesListModel);
citiesList.setSelectedIndex(0);
JAVA
123
Dodavanje combo box-a
Dodavanje combo box-a u kome se bira sedite:
static JLabel seatLabel = new JLabel();
static JComboBox seatComboBox = new JComboBox();
seatComboBox.addItem("Aisle");
seatComboBox.addItem("Middle");
seatComboBox.addItem("Window");
seatComboBox.setSelectedIndex(0);
JAVA
125
Nakon kompajliranja i pokretanja aplikacije list panel se
izgubio. Ovakve udne stvari se deavaju kada se postavi
eljena veliina (preferred sizes) za jednu kontrolu.
Preferred size je postavljena za scroll panel koji sadri list
kontrolu gradova, ali ne i za combo box. GridBagLayout ima
problema sa tim, pa je potrebno postaviti preferred size i za
combo box.
JAVA
126
seatComboBox.setPreferredSize(new Dimension(100, 25));
JAVA
127
Dodavanje meal combo box-a i
dva tastera
static JLabel mealLabel = new JLabel();
static JComboBox mealComboBox = new JComboBox();
static JButton assignButton = new JButton();
static JButton exitButton = new JButton();
mealLabel.setText("Meal Preference");
gridConstraints = new GridBagConstraints();
gridConstraints.gridx = 2;
gridConstraints.gridy = 0;
gridConstraints.insets = new Insets(10, 0, 0, 0);
JAVA
128
getContentPane().add(mealLabel, gridConstraints);
mealComboBox.setEditable(true);
gridConstraints = new GridBagConstraints();
gridConstraints.gridx = 2;
gridConstraints.gridy = 1;
gridConstraints.insets = new Insets(10, 10, 0, 10);
gridConstraints.anchor = GridBagConstraints.NORTH;
getContentPane().add(mealComboBox, gridConstraints);
assignButton.setText("Assign");
gridConstraints = new GridBagConstraints();
gridConstraints.gridx = 1;
JAVA
129
gridConstraints.gridy = 2;
gridConstraints.insets = new Insets(0, 0, 10, 0);
getContentPane().add(assignButton, gridConstraints);
assignButton.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
assignButtonActionPerformed(e);
}
});
exitButton.setText("Exit");
JAVA
130
gridConstraints = new GridBagConstraints();
gridConstraints.gridx = 2;
gridConstraints.gridy = 2;
gridConstraints.insets = new Insets(0, 0, 10, 0);
getContentPane().add(exitButton, gridConstraints);
exitButton.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
exitButtonActionPerformed(e);
}
});
JAVA
131
Kod za dodavanje izbora jela u meal combo box:
mealComboBox.addItem("Chicken");
mealComboBox.addItem("Mystery Meat");
mealComboBox.addItem("Kosher");
mealComboBox.addItem("Vegetarian");
mealComboBox.addItem("Fruit Plate");
mealComboBox.setSelectedItem("No Preference");
JAVA
132
Na kraju, dodaju se prazni dogaaji za dva tastera:
private void assignButtonActionPerformed(ActionEvent e)
{
}
private void exitButtonActionPerformed(ActionEvent e)
{
}
JAVA
133
private void assignButtonActionPerformed(ActionEvent e)
{
// Build message box that gives your assignment
String message;
message = "Destination: " + citiesList.getSelectedValue() +
"\n";
message += "Seat Location: " +
seatComboBox.getSelectedItem() + "\n";
message += "Meal: " + mealComboBox.getSelectedItem() +
"\n";
JOptionPane.showConfirmDialog(null, message, "Your
Assignment", JOptionPane.DEFAULT_OPTION,
JOptionPane.INFORMATION_MESSAGE);
}
JAVA
134
Kod za exitButton
JAVA
135