Constructor for View Class 7 January 2019 OSU CSE 1 Tasks To Be - - PowerPoint PPT Presentation

constructor for view class
SMART_READER_LITE
LIVE PREVIEW

Constructor for View Class 7 January 2019 OSU CSE 1 Tasks To Be - - PowerPoint PPT Presentation

Constructor for View Class 7 January 2019 OSU CSE 1 Tasks To Be Performed A simple constructor for the view class has four main jobs: 1. Create the JFrame being extended 2. Set up the GUI widgets to be used and lay out these widgets


slide-1
SLIDE 1

Constructor for View Class

7 January 2019 OSU CSE 1

slide-2
SLIDE 2

Tasks To Be Performed

  • A simple constructor for the view class has four

main jobs:

  • 1. Create the JFrame being extended
  • 2. Set up the GUI widgets to be used and “lay out”

these widgets in the main application window

  • 3. Set up the observers by registering (in our

examples) the object being constructed, i.e., this, with each of the GUI widgets that might have events

  • f interest to the application
  • 4. Start the main application window

7 January 2019 OSU CSE 2

slide-3
SLIDE 3

1: Create the JFrame

super("Simple GUI Demo");

7 January 2019 OSU CSE 3

slide-4
SLIDE 4

External (GUI) Effect

this

7 January 2019 OSU CSE 4

slide-5
SLIDE 5

External (GUI) Effect

this

7 January 2019 OSU CSE 5

Nothing illustrated in these slides actually becomes visible to the user until later!

slide-6
SLIDE 6

2: Set Up GUI Widgets (Text)...

this.inputText = new JTextArea("", LINES_IN_TEXT_AREAS, LINE_LENGTHS_IN_TEXT_AREAS); ... JScrollPane inputTextScrollPane = new JScrollPane(this.inputText);

7 January 2019 OSU CSE 6

slide-7
SLIDE 7

2: Set Up GUI Widgets (Text)...

this.inputText = new JTextArea("", LINES_IN_TEXT_AREAS, LINE_LENGTHS_IN_TEXT_AREAS); ... JScrollPane inputTextScrollPane = new JScrollPane(this.inputText);

7 January 2019 OSU CSE 7

5 lines, each of length 20

slide-8
SLIDE 8

External (GUI) Effect

this.inputText

7 January 2019 OSU CSE 8

inputTextScrollPane

slide-9
SLIDE 9

External (GUI) Effect

this.inputText

7 January 2019 OSU CSE 9

inputTextScrollPane

The scrollbars in JScrollPanes actually arise only when needed; not needed yet, but illustrated here.

slide-10
SLIDE 10

2: Set Up GUI Widgets (Buttons)...

this.copyButton = new JButton("Copy Input");

7 January 2019 OSU CSE 10

slide-11
SLIDE 11

External (GUI) Effect

this.copyButton

7 January 2019 OSU CSE 11

slide-12
SLIDE 12

2: ... and Lay Out GUI Widgets

JPanel buttonPanel = new JPanel( new GridLayout( ROWS_IN_BUTTON_PANEL_GRID, COLUMNS_IN_BUTTON_PANEL_GRID)); ... buttonPanel.add(this.resetButton); buttonPanel.add(this.copyButton);

7 January 2019 OSU CSE 12

slide-13
SLIDE 13

2: ... and Lay Out GUI Widgets

JPanel buttonPanel = new JPanel( new GridLayout( ROWS_IN_BUTTON_PANEL_GRID, COLUMNS_IN_BUTTON_PANEL_GRID)); ... buttonPanel.add(this.resetButton); buttonPanel.add(this.copyButton);

7 January 2019 OSU CSE 13

1 row, 2 columns

slide-14
SLIDE 14

External (GUI) Effect

buttonPanel

7 January 2019 OSU CSE 14

slide-15
SLIDE 15

2: ... and Lay Out GUI Widgets

this.setLayout( new GridLayout( ROWS_IN_THIS_GRID, COLUMNS_IN_THIS_GRID)); ... this.add(inputTextScrollPane); this.add(buttonPanel); this.add(outputTextScrollPane);

7 January 2019 OSU CSE 15

slide-16
SLIDE 16

2: ... and Lay Out GUI Widgets

this.setLayout( new GridLayout( ROWS_IN_THIS_GRID, COLUMNS_IN_THIS_GRID)); ... this.add(inputTextScrollPane); this.add(buttonPanel); this.add(outputTextScrollPane);

7 January 2019 OSU CSE 16

3 rows, 1 column

slide-17
SLIDE 17

2: ... and Lay Out GUI Widgets

this.setLayout( new GridLayout( ROWS_IN_THIS_GRID, COLUMNS_IN_THIS_GRID)); ... this.add(inputTextScrollPane); this.add(buttonPanel); this.add(outputTextScrollPane);

7 January 2019 OSU CSE 17

Remember: the two buttons are in this panel.

slide-18
SLIDE 18

External (GUI) Effect

this

7 January 2019 OSU CSE 18

slide-19
SLIDE 19

External (GUI) Effect

this

7 January 2019 OSU CSE 19

Remember: none of this is visible to the user yet.

slide-20
SLIDE 20

3: Set Up the Observers

this.resetButton.addActionListener(this); this.copyButton.addActionListener(this);

7 January 2019 OSU CSE 20

slide-21
SLIDE 21

Internal (non-GUI) Effect

7 January 2019 OSU CSE 21

slide-22
SLIDE 22

4: Start the Main Window

this.pack(); this.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE); this.setVisible(true);

7 January 2019 OSU CSE 22

slide-23
SLIDE 23

External (GUI) Effect: Now Visible

this

7 January 2019 OSU CSE 23

slide-24
SLIDE 24

External (GUI) Effect: Now Visible

this

7 January 2019 OSU CSE 24

The only code you wrote that executes now is the callback method for the two buttons: this.actionPerformed.