More Swing - PowerPoint PPT Presentation

1 / 16
About This Presentation
Title:

More Swing

Description:

More Swing. COMP204, Bernhard Pfahringer. Animations: javax.swing.Timer ... import javax.swing.filechooser.*; fc = new JFileChooser ... – PowerPoint PPT presentation

Number of Views:41
Avg rating:3.0/5.0
Slides: 17
Provided by: bern8
Category:
Tags: more | swing

less

Transcript and Presenter's Notes

Title: More Swing


1
More Swing
  • COMP204, Bernhard Pfahringer

2
Animations javax.swing.Timer
  • ActionListener incrementYPosition new
    ActionListener()
  • public void actionPerformed(ActionEvent evt)
  • if (_y lt 400)
  • _y
  • if (_frame ! null) _frame.repaint()
  • else
  • _timer.stop()
  • _timer new javax.swing.Timer(10,incrementYPositi
    on)
  • _timer.start()

3
Safe loading of an ImageIcon
  • / Returns an ImageIcon, or null if the path
    was invalid. /
  • protected static ImageIcon createImageIcon(String
    path)
  • java.net.URL imgURL ButtonDemo.class.getReso
    urce(path)
  • if (imgURL ! null)
  • return new ImageIcon(imgURL)
  • else
  • System.err.println("Couldn't find file "
    path)
  • return null

4
ButtonDemo action method
  • public void actionPerformed(ActionEvent e)
  • if ("disable".equals(e.getActionCommand()))
  • b2.setEnabled(false)
  • b1.setEnabled(false)
  • b3.setEnabled(true)
  • else
  • b2.setEnabled(true)
  • b1.setEnabled(true)
  • b3.setEnabled(false)

5
Setting up a button
  • b1 new JButton("Disable middle button",
    leftButtonIcon)
  • b1.setVerticalTextPosition(AbstractButton.CENTER)
  • //aka LEFT, for left-to-right locales
  • b1.setHorizontalTextPosition(AbstractButton.LEADIN
    G)
  • b1.setMnemonic(KeyEvent.VK_D)
  • b1.setActionCommand("disable")
  • b1.addActionListener(this)
  • b1.setToolTipText("Click this button to disable
    the middle button.")

6
JCheckbox
  • Maintain/display labeled binary state (on/off,
    yes/no, selected/unselected)
  • getLabel(), setLabel(String),
  • isSelected(), setSelected()
  • also an ItemListener for an ItemEvent

7
Trivial example
  • import javax.swing.
  • import java.awt.
  • import java.awt.event.
  • public class CheckBoxTest extends JFrame
  • private JCheckBox cb new JCheckBox("off")
  • public static void main(String args)
  • CheckBoxTest w new CheckBoxTest()
  • w.show()

8
cont.
  • public CheckBoxTest()
  • setTitle("CheckBoxTest")
  • setSize(300,70)
  • cb.addItemListener(new CheckBoxTestListener())
  • add("Center", cb)
  • private class CheckBoxTestListener implements
    ItemListener
  • public void itemStateChanged(ItemEvent e)
  • cb.setLabel( (cb.isSelected()) ? "ON"
    "OFF")

9
RadioButtonDemo
  • public class RadioButtonDemo extends JPanel
  • implements
    ActionListener
  • static String birdString "Bird"
  • .
  • JRadioButton birdButton new JRadioButton(birdSt
    ring)
  • birdButton.setMnemonic(KeyEvent.VK_B)
  • birdButton.setActionCommand(birdString)
  • birdButton.setSelected(true)
  • ....
  • ButtonGroup group new ButtonGroup()
  • group.add(birdButton)
  • group.add(catButton)

10
cont.
  • birdButton.addActionListener(this)
  • JPanel radioPanel new JPanel(new GridLayout(0,
    1))
  • radioPanel.add(birdButton)
  • add(radioPanel, BorderLayout.LINE_START)
  • add(picture, BorderLayout.CENTER)
  • setBorder(BorderFactory.createEmptyBorder(20,20,2
    0,20))
  • public void actionPerformed(ActionEvent e)
  • picture.setIcon(createImageIcon("images/"

  • e.getActionCommand() ".gif"))

11
Dialogs
  • Special purpose window displayed shortly
  • To notify, or ask simple questions
  • Always attached to a Frame
  • Modal demands user response, prevents further
    actions show() wont return until dismissed, ?
    setVisible(false)
  • Nonmodal actions often placed into separate
    Thread
  • Dialog dig new Dialog(this,false)

12
Dialog example
  • public class DialogTest extends JFrame
  • public static void main(String args)
  • DialogTest w new DialogTest()
    w.show()
  • private JTextArea d new JTextArea()
  • private JCheckBox cb new JCheckBox("Modal
    Dialog?")
  • public DialogTest()
  • setTitle("DialogTest") setSize(300,220)
  • add("West",cb) add("East",new
    MakeButton()) add("South",d)

13
cont
  • private void makeDialog(boolean modalFlag)
  • final JDialog dialog new JDialog(this,modalF
    lag)
  • dialog.setSize(100,100)
  • dialog.add("North",new CountButton(1))
  • dialog.add("West",new CountButton(2))
  • dialog.add("East",new CountButton(3))
  • dialog.add("South", new ButtonAdapter("Hide")
  • public void pressed()
  • dialog.setVisible(false)
  • )
  • dialog.show()

14
cont.
  • private class MakeButton extends ButtonAdapter
  • public MakeButton() super("Make Dialog")
  • public void pressed() makeDialog(cb.isSele
    cted())
  • private class CountButton extends ButtonAdapter
  • public CountButton(int value) super(""
    value)
  • public void pressed() d.append("Button "
    getLabel() " pressed\n")

15
JFileChooser
  • import javax.swing.filechooser.
  • fc new JFileChooser()
  • public void actionPerformed(ActionEvent e)

    if (e.getSource()
    openButton)
  • int returnVal fc.showOpenDialog(FileChoo
    serDemo.this)
  • if (returnVal JFileChooser.APPROVE_OPTI
    ON)
  • File file fc.getSelectedFile()
  • // This is where a real application
    would open the file.
  • log.append("Opening "
    file.getName() "." newline)
  • else
  • log.append("Open command cancelled by
    user." newline)
  • log.setCaretPosition(log.getDocument().getLen
    gth())

16
JMenuBar
  • Attached to JFrame by setJMenuBar()
  • JMenuBar bar new JMenuBar()
  • setJMenuBar(bar)
  • Add menus
  • JMenu HelpMenu new JMenu(Help)
  • bar.add(helpMenu)
  • Add menu items
  • JMenuItem quitItem new JMenuItem(Quit)
  • quitItem.addActionListener(new QuitListener())
  • helpMenu.add(quitItem)
Write a Comment
User Comments (0)
About PowerShow.com