Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


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

1 More GUIs, events, static

2 model.addElement(335); model.addElement(436); } private JTextArea lineDisplay = new JTextArea("Could have\nseveral lines\nhere"); private JList guiViewOfList; // The view private DefaultListModel 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 (); 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?

3 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);

4 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);

5 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 );

6 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 (); guiViewOfList.setModel(model);

7 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 } } }

8 Add a mouse listener to out panel class ListenToMouse implements MouseListener

9 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

10 TemperatureConverter has static methods public 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); }


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

Similar presentations


Ads by Google