10
More GUIs, events, static

More GUIs, events, static. model.addElement(335); model.addElement(436); } private JTextArea lineDisplay = new JTextArea("Could have\nseveral lines\nhere");

Embed Size (px)

Citation preview

Page 1: More GUIs, events, static. model.addElement(335); model.addElement(436); } private JTextArea lineDisplay = new JTextArea("Could have\nseveral lines\nhere");

More GUIs, events, static

Page 2: More GUIs, events, static. model.addElement(335); model.addElement(436); } private JTextArea lineDisplay = new JTextArea("Could have\nseveral lines\nhere");

model.addElement(335);

model.addElement(436);

}

private JTextArea lineDisplay = new JTextArea("Could have\nseveral lines\nhere");

private JList<Integer> guiViewOfList; // The view

private DefaultListModel<Integer> model; // the Model

private void setLayout() {

this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); // Do this almost always

this.setSize(120, 180); // 120 pixels wide and 180 pixels tall

this.setLocation(110, 50); // upper left corner

// DefaultListModel and JList work together as model/view

guiViewOfList = new JList<Integer>();

guiViewOfList.setModel(model);

// Adds two elements to the center (BorderLayout.CENTER is default), only see

add(guiViewOfList);

add(lineDisplay);

• What is wrong with this picture?

Page 3: More GUIs, events, static. model.addElement(335); model.addElement(436); } private JTextArea lineDisplay = new JTextArea("Could have\nseveral lines\nhere");

Layout and Sizing Issues

• Use BorderLayout for now• If you have more than one element• Set the preferred size of the components• Add them to a new JPanel• Add that JPanel to the center of the JFrame

add(aPanel, BorderLayout.CENTER);

Page 4: More GUIs, events, static. model.addElement(335); model.addElement(436); } private JTextArea lineDisplay = new JTextArea("Could have\nseveral lines\nhere");

How to fix things?

// Solution? Set the preferred size, // add both to a JPanel, then add one JPanel

JPanel panel = new JPanel();

lineDisplay.setPreferredSize(new Dimension(150, 100));

guiViewOfList.setPreferredSize(new Dimension(50, 140));

guiViewOfList.setBackground(Color.CYAN);

panel.add(guiViewOfList);

panel.add(lineDisplay);

add(panel);

Page 5: More GUIs, events, static. model.addElement(335); model.addElement(436); } private JTextArea lineDisplay = new JTextArea("Could have\nseveral lines\nhere");

Can set fonts, background, and editable // Get a fixed pitch font lineDisplay.setFont(new Font("Courier", Font.BOLD, 14));

lineDisplay.setBackground(Color.CYAN);

lineDisplay.setEditable(false);

guiViewOfList.setBackground(Color.GRAY);

panel.setBackground(Color.RED);

Page 6: More GUIs, events, static. model.addElement(335); model.addElement(436); } private JTextArea lineDisplay = new JTextArea("Could have\nseveral lines\nhere");

Model / View relationship with list

• DefaultListModel implements ListModel and therefore can be used with JList, a graphical view of a list (construct with or set model)

// DefaultListModel and JList work together as model/view

guiViewOfList = new JList<Integer>();

guiViewOfList.setModel(model);

Page 7: More GUIs, events, static. model.addElement(335); model.addElement(436); } private JTextArea lineDisplay = new JTextArea("Could have\nseveral lines\nhere");

The JList is selectable, set and get

private class JListListener implements ListSelectionListener {

public void valueChanged(ListSelectionEvent arg0) {

int index = guiViewOfList.getSelectedIndex();

if (index >= 0 && index < model.size()) {

// The JList is listening for changes to the model

model.remove(index);

// after a remove, the JList updates itself

}

} }

Page 8: More GUIs, events, static. model.addElement(335); model.addElement(436); } private JTextArea lineDisplay = new JTextArea("Could have\nseveral lines\nhere");

Add a mouse listener to out panel

class ListenToMouse implements MouseListener

Page 9: More GUIs, events, static. model.addElement(335); model.addElement(436); } private JTextArea lineDisplay = new JTextArea("Could have\nseveral lines\nhere");

Difference between static and non-static• A static method can be called without instantiating it’s class

• Math.abs(5-12);

• This won’t compile (the constructor is private)• Math m = new Math();

• Use static methods when you don’t need instance variables

Page 10: More GUIs, events, static. model.addElement(335); model.addElement(436); } private JTextArea lineDisplay = new JTextArea("Could have\nseveral lines\nhere");

TemperatureConverter has static methodspublic class TemperatureConverter {

// Return the Celcius equivalent of Fahrenheit

public static double F2C(double f) {

return roundToOneDecimal((5.0 / 9.0) * (f - 32.0));

}

• Tests do not need to instantiate the class @Test

public void testF2C() {

assertEquals(100.0, TemperatureConverter.F2C(212.0), 0.1);

assertEquals(0.0, TemperatureConverter.F2C(32.0), 0.1);

assertEquals(37.0, TemperatureConverter.F2C(98.6), 0.1);

}