From e0a3b009b6c2912f2f7dc951361ac9a6f6a0ada0 Mon Sep 17 00:00:00 2001 From: David Livingstone Date: Thu, 11 Jan 2024 09:39:02 +0000 Subject: [PATCH] SwingCodeExamplesNB as a Maven Netbeans Project --- SwingCodeExamplesNB/pom.xml | 13 +++ .../javateaching/basicgui/BasicGUITest.java | 61 +++++++++++ .../basicgui/BorderLayoutDemo.java | 35 ++++++ .../javateaching/basicgui/ComboLayout.java | 53 +++++++++ .../main/java/javateaching/basicgui/Demo.java | 19 ++++ .../javateaching/basicgui/GridLayoutTest.java | 35 ++++++ .../java/javateaching/basicgui/LeanGui.java | 42 ++++++++ .../basicgui/LeanGuiWithScroll.java | 51 +++++++++ .../guimoar/FlowLayoutExample.java | 49 +++++++++ .../guimoar/GridLayoutExample.java | 56 ++++++++++ .../java/javateaching/guimoar/GridTo500.java | 32 ++++++ .../javateaching/guimoar/MixAndMatchDemo.java | 18 ++++ .../guimoar/MixAndMatchWindow.java | 46 ++++++++ .../layoutmanagers/BorderLayoutExample.java | 51 +++++++++ .../java/javateaching/layoutmanagers/Cat.java | 60 +++++++++++ .../layoutmanagers/CombiningLayouts.java | 40 +++++++ .../layoutmanagers/ComponentShowcase.java | 57 ++++++++++ .../layoutmanagers/ConstructorDemo.java | 68 ++++++++++++ .../layoutmanagers/Controller.java | 83 ++++++++++++++ .../layoutmanagers/FlowLayoutExample.java | 54 ++++++++++ .../layoutmanagers/GridLayoutExample.java | 54 ++++++++++ .../layoutmanagers/GridTo500.java | 38 +++++++ .../layoutmanagers/LayoutManagers.java | 37 +++++++ .../layoutmanagers/MigLayoutExample.java | 50 +++++++++ .../layoutmanagers/ScrollPaneDemo.java | 36 +++++++ .../java/javateaching/leanconsole/GCDemo.java | 43 ++++++++ .../javateaching/leanconsole/LeanConsole.java | 52 +++++++++ .../leanconsole/LeanConsoleWithInput.java | 101 ++++++++++++++++++ .../java/singletonexample/MySingleton.java | 57 ++++++++++ .../singletonexample/SingletonExample.java | 37 +++++++ 30 files changed, 1428 insertions(+) create mode 100644 SwingCodeExamplesNB/pom.xml create mode 100644 SwingCodeExamplesNB/src/main/java/javateaching/basicgui/BasicGUITest.java create mode 100644 SwingCodeExamplesNB/src/main/java/javateaching/basicgui/BorderLayoutDemo.java create mode 100644 SwingCodeExamplesNB/src/main/java/javateaching/basicgui/ComboLayout.java create mode 100644 SwingCodeExamplesNB/src/main/java/javateaching/basicgui/Demo.java create mode 100644 SwingCodeExamplesNB/src/main/java/javateaching/basicgui/GridLayoutTest.java create mode 100644 SwingCodeExamplesNB/src/main/java/javateaching/basicgui/LeanGui.java create mode 100644 SwingCodeExamplesNB/src/main/java/javateaching/basicgui/LeanGuiWithScroll.java create mode 100644 SwingCodeExamplesNB/src/main/java/javateaching/guimoar/FlowLayoutExample.java create mode 100644 SwingCodeExamplesNB/src/main/java/javateaching/guimoar/GridLayoutExample.java create mode 100644 SwingCodeExamplesNB/src/main/java/javateaching/guimoar/GridTo500.java create mode 100644 SwingCodeExamplesNB/src/main/java/javateaching/guimoar/MixAndMatchDemo.java create mode 100644 SwingCodeExamplesNB/src/main/java/javateaching/guimoar/MixAndMatchWindow.java create mode 100644 SwingCodeExamplesNB/src/main/java/javateaching/layoutmanagers/BorderLayoutExample.java create mode 100644 SwingCodeExamplesNB/src/main/java/javateaching/layoutmanagers/Cat.java create mode 100644 SwingCodeExamplesNB/src/main/java/javateaching/layoutmanagers/CombiningLayouts.java create mode 100644 SwingCodeExamplesNB/src/main/java/javateaching/layoutmanagers/ComponentShowcase.java create mode 100644 SwingCodeExamplesNB/src/main/java/javateaching/layoutmanagers/ConstructorDemo.java create mode 100644 SwingCodeExamplesNB/src/main/java/javateaching/layoutmanagers/Controller.java create mode 100644 SwingCodeExamplesNB/src/main/java/javateaching/layoutmanagers/FlowLayoutExample.java create mode 100644 SwingCodeExamplesNB/src/main/java/javateaching/layoutmanagers/GridLayoutExample.java create mode 100644 SwingCodeExamplesNB/src/main/java/javateaching/layoutmanagers/GridTo500.java create mode 100644 SwingCodeExamplesNB/src/main/java/javateaching/layoutmanagers/LayoutManagers.java create mode 100644 SwingCodeExamplesNB/src/main/java/javateaching/layoutmanagers/MigLayoutExample.java create mode 100644 SwingCodeExamplesNB/src/main/java/javateaching/layoutmanagers/ScrollPaneDemo.java create mode 100644 SwingCodeExamplesNB/src/main/java/javateaching/leanconsole/GCDemo.java create mode 100644 SwingCodeExamplesNB/src/main/java/javateaching/leanconsole/LeanConsole.java create mode 100644 SwingCodeExamplesNB/src/main/java/javateaching/leanconsole/LeanConsoleWithInput.java create mode 100644 SwingCodeExamplesNB/src/main/java/singletonexample/MySingleton.java create mode 100644 SwingCodeExamplesNB/src/main/java/singletonexample/SingletonExample.java diff --git a/SwingCodeExamplesNB/pom.xml b/SwingCodeExamplesNB/pom.xml new file mode 100644 index 0000000..ed08b24 --- /dev/null +++ b/SwingCodeExamplesNB/pom.xml @@ -0,0 +1,13 @@ + + + 4.0.0 + javateaching + SwingCodeExamplesNB + 1.0-SNAPSHOT + jar + + UTF-8 + 17 + 17 + + \ No newline at end of file diff --git a/SwingCodeExamplesNB/src/main/java/javateaching/basicgui/BasicGUITest.java b/SwingCodeExamplesNB/src/main/java/javateaching/basicgui/BasicGUITest.java new file mode 100644 index 0000000..1ecd3e0 --- /dev/null +++ b/SwingCodeExamplesNB/src/main/java/javateaching/basicgui/BasicGUITest.java @@ -0,0 +1,61 @@ +/* + * To change this license header, choose License Headers in Project Properties. + * To change this template file, choose Tools | Templates + * and open the template in the editor. + */ +package javateaching.basicgui; + +import java.awt.FlowLayout; +import javax.swing.ButtonGroup; +import javax.swing.JButton; +import javax.swing.JCheckBox; +import javax.swing.JComboBox; +import javax.swing.JFrame; +import javax.swing.JLabel; +import javax.swing.JRadioButton; +import javax.swing.JTextField; +import javax.swing.WindowConstants; + +/** + * + * @author paul + */ +public class BasicGUITest { + public static void main(String[] args) + { + JFrame ourwindow = new JFrame("Yay! Window"); + ourwindow.setLayout(new FlowLayout()); + + JLabel label = new JLabel("Enter your name"); + JTextField text = new JTextField("Bill Bloggs"); + JCheckBox cbox = new JCheckBox("Enable"); + + JRadioButton rbut = new JRadioButton("Leave"); + JRadioButton rbut2 = new JRadioButton("Remain"); + ButtonGroup brexit = new ButtonGroup(); + brexit.add(rbut); + brexit.add(rbut2); + + String[] comboOptions = {"red","green","blue"}; + JComboBox comboBox = new JComboBox(comboOptions); + + JButton buttonOK = new JButton("OK"); + + ourwindow.add(label); + ourwindow.add(text); + ourwindow.add(cbox); + ourwindow.add(rbut); + ourwindow.add(rbut2); + ourwindow.add(comboBox); + ourwindow.add(buttonOK); + + for (int i = 0; i < 50; i++) ourwindow.add(new JButton("Button")); + + ourwindow.setSize(600,400); + + ourwindow.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); + + ourwindow.setVisible(true); + } +} + diff --git a/SwingCodeExamplesNB/src/main/java/javateaching/basicgui/BorderLayoutDemo.java b/SwingCodeExamplesNB/src/main/java/javateaching/basicgui/BorderLayoutDemo.java new file mode 100644 index 0000000..83e77ef --- /dev/null +++ b/SwingCodeExamplesNB/src/main/java/javateaching/basicgui/BorderLayoutDemo.java @@ -0,0 +1,35 @@ +/* + * To change this license header, choose License Headers in Project Properties. + * To change this template file, choose Tools | Templates + * and open the template in the editor. + */ +package javateaching.basicgui; + +import java.awt.BorderLayout; +import javax.swing.JButton; +import javax.swing.JFrame; +import javax.swing.WindowConstants; + + +/** + * + * @author paul + */ +public class BorderLayoutDemo +{ + public static void main(String[] args) + { + JFrame frame = new JFrame("Border Layout"); + frame.setLayout(new BorderLayout()); + frame.add(new JButton("North"),BorderLayout.NORTH); + frame.add(new JButton("South"),BorderLayout.SOUTH); + frame.add(new JButton("East"),BorderLayout.EAST); + frame.add(new JButton("West"),BorderLayout.WEST); + frame.add(new JButton("Center"),BorderLayout.CENTER); + + frame.setSize(400,400); + frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); + frame.setVisible(true); + } +} + diff --git a/SwingCodeExamplesNB/src/main/java/javateaching/basicgui/ComboLayout.java b/SwingCodeExamplesNB/src/main/java/javateaching/basicgui/ComboLayout.java new file mode 100644 index 0000000..d425031 --- /dev/null +++ b/SwingCodeExamplesNB/src/main/java/javateaching/basicgui/ComboLayout.java @@ -0,0 +1,53 @@ +/* + * To change this license header, choose License Headers in Project Properties. + * To change this template file, choose Tools | Templates + * and open the template in the editor. + */ +package javateaching.basicgui; + +import javateaching.leanconsole.LeanConsole; +import java.awt.BorderLayout; +import java.awt.GridLayout; +import javax.swing.JButton; +import javax.swing.JFrame; +import javax.swing.JPanel; +import javax.swing.WindowConstants; + +/** + * + * @author paul + */ +public class ComboLayout +{ + public static void main(String[] args) + { + JFrame frame = new JFrame("Combo Layout"); + frame.setLayout(new BorderLayout()); + frame.add(new JButton("North"),BorderLayout.NORTH); + frame.add(new JButton("South"),BorderLayout.SOUTH); + frame.add(new JButton("East"),BorderLayout.EAST); + frame.add(new JButton("West"),BorderLayout.WEST); + + JPanel middlePanel = new JPanel(); + middlePanel.setLayout(new GridLayout(2,2)); + middlePanel.add(new JButton("1")); + middlePanel.add(new JButton("2")); + middlePanel.add(new JButton("3")); + //middlePanel.add(new JButton("4")); + + LeanConsole gc = new LeanConsole(); + middlePanel.add(gc); + + for (int i = 0; i < 100; i++) + { + gc.println("Hello from the console"); + } + + frame.add(middlePanel,BorderLayout.CENTER); + + frame.setSize(400,400); + frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); + frame.setVisible(true); + } +} + diff --git a/SwingCodeExamplesNB/src/main/java/javateaching/basicgui/Demo.java b/SwingCodeExamplesNB/src/main/java/javateaching/basicgui/Demo.java new file mode 100644 index 0000000..894b803 --- /dev/null +++ b/SwingCodeExamplesNB/src/main/java/javateaching/basicgui/Demo.java @@ -0,0 +1,19 @@ +/* + * To change this license header, choose License Headers in Project Properties. + * To change this template file, choose Tools | Templates + * and open the template in the editor. + */ +package javateaching.basicgui; + +import javax.swing.JFrame; + +/** + * + * @author paul + */ +public class Demo { + public static void main(String[] args) + { + JFrame topwindow = new JFrame("My wonderful window"); + } +} diff --git a/SwingCodeExamplesNB/src/main/java/javateaching/basicgui/GridLayoutTest.java b/SwingCodeExamplesNB/src/main/java/javateaching/basicgui/GridLayoutTest.java new file mode 100644 index 0000000..68b95ef --- /dev/null +++ b/SwingCodeExamplesNB/src/main/java/javateaching/basicgui/GridLayoutTest.java @@ -0,0 +1,35 @@ +/* + * To change this license header, choose License Headers in Project Properties. + * To change this template file, choose Tools | Templates + * and open the template in the editor. + */ +package javateaching.basicgui; + +import java.awt.GridLayout; +import javax.swing.JButton; +import javax.swing.JFrame; +import javax.swing.WindowConstants; + +/** + * + * @author paul + */ +public class GridLayoutTest +{ + public static void main(String[] args) + { + JFrame window = new JFrame("Grid Layout Test"); + window.setLayout(new GridLayout(6,7)); + for (int row = 1; row <= 6; row++) + { + for (int col = 1; col <= 7; col++) + { + JButton cell = new JButton(col+","+row+" "); + window.add(cell); + } + } + window.pack(); + window.setVisible(true); + window.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); + } +} diff --git a/SwingCodeExamplesNB/src/main/java/javateaching/basicgui/LeanGui.java b/SwingCodeExamplesNB/src/main/java/javateaching/basicgui/LeanGui.java new file mode 100644 index 0000000..a77d992 --- /dev/null +++ b/SwingCodeExamplesNB/src/main/java/javateaching/basicgui/LeanGui.java @@ -0,0 +1,42 @@ +/* + * To change this license header, choose License Headers in Project Properties. + * To change this template file, choose Tools | Templates + * and open the template in the editor. + */ +package javateaching.basicgui; + +import java.awt.BorderLayout; +import javax.swing.JFrame; +import javax.swing.JScrollPane; +import javax.swing.JTextArea; +import javax.swing.WindowConstants; + +/** + * + * @author paul + */ +public class LeanGui +{ + public static void main(String[] args) throws InterruptedException + { + JFrame ourwindow = new JFrame("Yay! Window"); + ourwindow.setLayout(new BorderLayout()); + JTextArea text = new JTextArea(); + text.setLineWrap(true); // otherwise everything is on one line + text.setEditable(false); // otherwise people can type over the text + ourwindow.add(text,BorderLayout.CENTER); + + ourwindow.setSize(500,400); + ourwindow.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); + ourwindow.setVisible(true); + + // the next code is to illustrate "printing" + while (true) // repeat forever + { + String currentText = text.getText(); + currentText += "Hello! "; + text.setText(currentText); + Thread.sleep(10); // add a delay just so you can see it build up + } + } +} \ No newline at end of file diff --git a/SwingCodeExamplesNB/src/main/java/javateaching/basicgui/LeanGuiWithScroll.java b/SwingCodeExamplesNB/src/main/java/javateaching/basicgui/LeanGuiWithScroll.java new file mode 100644 index 0000000..7ea0c2b --- /dev/null +++ b/SwingCodeExamplesNB/src/main/java/javateaching/basicgui/LeanGuiWithScroll.java @@ -0,0 +1,51 @@ +/* + * To change this license header, choose License Headers in Project Properties. + * To change this template file, choose Tools | Templates + * and open the template in the editor. + */ +package javateaching.basicgui; + +import java.awt.BorderLayout; +import javax.swing.JFrame; +import javax.swing.JScrollPane; +import javax.swing.JTextArea; +import javax.swing.WindowConstants; + +/** + * + * @author paul + */ +public class LeanGuiWithScroll +{ + public static void main(String[] args) throws InterruptedException // needed for Thread.sleep + { + JFrame ourwindow = new JFrame("Yay! Window"); + ourwindow.setLayout(new BorderLayout()); + JTextArea text = new JTextArea(); + text.setLineWrap(true); // otherwise everything is on one line + text.setEditable(false); // otherwise people can type over the text + + // JScrollPane gives magical scrollbar powers + // We specify the component that we want scrollable + // in the JScrollPane constructor + JScrollPane scrolly = new JScrollPane(text); + // we add the JScrollPane to our window + ourwindow.add(scrolly,BorderLayout.CENTER); + + ourwindow.setSize(500,400); + ourwindow.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); + ourwindow.setVisible(true); + + // the next code is to illustrate "printing" + while (true) // repeat forever + { + String currentText = text.getText(); + currentText += "Hello! "; + text.setText(currentText); + // below line will make it scroll to the bottom after each + // "print" + text.setCaretPosition(text.getDocument().getLength()); + Thread.sleep(10); // add a delay just so you can see it build up + } + } +} \ No newline at end of file diff --git a/SwingCodeExamplesNB/src/main/java/javateaching/guimoar/FlowLayoutExample.java b/SwingCodeExamplesNB/src/main/java/javateaching/guimoar/FlowLayoutExample.java new file mode 100644 index 0000000..1a87cc3 --- /dev/null +++ b/SwingCodeExamplesNB/src/main/java/javateaching/guimoar/FlowLayoutExample.java @@ -0,0 +1,49 @@ +/* + * To change this template, choose Tools | Templates + * and open the template in the editor. + */ + +package javateaching.guimoar; + +import java.awt.FlowLayout; +import java.awt.Color; +import javax.swing.JButton; +import javax.swing.JLabel; +import javax.swing.JPanel; + +/** + * + * @author paulneve + */ +public class FlowLayoutExample extends JPanel { + + private JButton component1 = new JButton("click me"); + private JLabel component2 = new JLabel("component 2"); + private JLabel component3 = new JLabel("component 3"); + private JLabel component4 = new JLabel("component 4"); + private JLabel component5 = new JLabel("component 5"); + + public FlowLayoutExample() + { + FlowLayout fl = new FlowLayout(); + this.setLayout(fl); + + component2.setBackground(Color.GREEN); + component2.setOpaque(true); + + component3.setBackground(Color.ORANGE); + component3.setOpaque(true); + + component4.setBackground(Color.CYAN); + component4.setOpaque(true); + + component5.setBackground(Color.WHITE); + component5.setOpaque(true); + + this.add(component1); + this.add(component2); + this.add(component3); + this.add(component4); + this.add(component5); + } +} diff --git a/SwingCodeExamplesNB/src/main/java/javateaching/guimoar/GridLayoutExample.java b/SwingCodeExamplesNB/src/main/java/javateaching/guimoar/GridLayoutExample.java new file mode 100644 index 0000000..ef7b53f --- /dev/null +++ b/SwingCodeExamplesNB/src/main/java/javateaching/guimoar/GridLayoutExample.java @@ -0,0 +1,56 @@ +/* + * To change this template, choose Tools | Templates + * and open the template in the editor. + */ + +package javateaching.guimoar; + +import java.awt.Color; +import java.awt.GridLayout; +import javax.swing.JLabel; +import javax.swing.JPanel; + +/** + * + * @author paulneve + */ +public class GridLayoutExample extends JPanel { + + private JLabel component1 = new JLabel("component 1",JLabel.CENTER); + private JLabel component2 = new JLabel("component 2", JLabel.CENTER); + private JLabel component3 = new JLabel("component 3"); + private JLabel component4 = new JLabel("component 4"); + private JLabel component5 = new JLabel("

component 5 with BIGNESS

"); + private JLabel component6 = new JLabel("component 6"); + + public GridLayoutExample() + { + GridLayout gl = new GridLayout(0,3); + this.setLayout(gl); + + component1.setBackground(Color.YELLOW); + component1.setOpaque(true); + + component2.setBackground(Color.GREEN); + component2.setOpaque(true); + + component3.setBackground(Color.ORANGE); + component3.setOpaque(true); + + component4.setBackground(Color.CYAN); + component4.setOpaque(true); + + component5.setBackground(Color.WHITE); + component5.setOpaque(true); + + component4.setBackground(Color.CYAN); + component4.setOpaque(true); + + this.add(component1); + this.add(component2); + this.add(component3); + this.add(component4); + this.add(component5); + this.add(component6); + } +} diff --git a/SwingCodeExamplesNB/src/main/java/javateaching/guimoar/GridTo500.java b/SwingCodeExamplesNB/src/main/java/javateaching/guimoar/GridTo500.java new file mode 100644 index 0000000..394eb59 --- /dev/null +++ b/SwingCodeExamplesNB/src/main/java/javateaching/guimoar/GridTo500.java @@ -0,0 +1,32 @@ +/* + * To change this template, choose Tools | Templates + * and open the template in the editor. + */ + +package javateaching.guimoar; + +import java.awt.Color; +import java.awt.GridLayout; +import java.util.Random; +import javax.swing.JLabel; +import javax.swing.JPanel; + +/** + * + * @author paulneve + */ +public class GridTo500 extends JPanel { + public GridTo500() + { + GridLayout gl = new GridLayout(0,15); + this.setLayout(gl); + + for (int i = 1; i < 501; i++) + { + JLabel numberJL = new JLabel(String.valueOf(i+" ")); + numberJL.setOpaque(true); + numberJL.setBackground(Color.getHSBColor(new Random().nextFloat(), 1.0F, 1.0F )); + this.add(numberJL); + } + } +} diff --git a/SwingCodeExamplesNB/src/main/java/javateaching/guimoar/MixAndMatchDemo.java b/SwingCodeExamplesNB/src/main/java/javateaching/guimoar/MixAndMatchDemo.java new file mode 100644 index 0000000..c9543cf --- /dev/null +++ b/SwingCodeExamplesNB/src/main/java/javateaching/guimoar/MixAndMatchDemo.java @@ -0,0 +1,18 @@ +/* + * To change this license header, choose License Headers in Project Properties. + * To change this template file, choose Tools | Templates + * and open the template in the editor. + */ +package javateaching.guimoar; + +/** + * + * @author paul + */ +public class MixAndMatchDemo { + public static void main(String[] args) + { + // shortest program evar :-) + MixAndMatchWindow mainwindow = new MixAndMatchWindow(); + } +} diff --git a/SwingCodeExamplesNB/src/main/java/javateaching/guimoar/MixAndMatchWindow.java b/SwingCodeExamplesNB/src/main/java/javateaching/guimoar/MixAndMatchWindow.java new file mode 100644 index 0000000..1370ca8 --- /dev/null +++ b/SwingCodeExamplesNB/src/main/java/javateaching/guimoar/MixAndMatchWindow.java @@ -0,0 +1,46 @@ +/* + * To change this license header, choose License Headers in Project Properties. + * To change this template file, choose Tools | Templates + * and open the template in the editor. + */ +package javateaching.guimoar; + +import java.awt.BorderLayout; +import javax.swing.JFrame; +import javax.swing.JScrollPane; +import javax.swing.WindowConstants; + +/** + * + * @author paul + */ +public class MixAndMatchWindow extends JFrame +{ + private FlowLayoutExample fl = new FlowLayoutExample(); + private GridLayoutExample gl = new GridLayoutExample(); + private GridTo500 g500 = new GridTo500(); + + public MixAndMatchWindow() + { + super("Demo of mixing and matching layouts with JPanes"); + this.setLayout(new BorderLayout()); + this.add(fl,BorderLayout.NORTH); + this.add(gl,BorderLayout.SOUTH); + + // try commenting out this... + this.add(g500,BorderLayout.CENTER); + + // and uncommenting these two... + //JScrollPane scrolly = new JScrollPane(g500); + //this.add(scrolly,BorderLayout.CENTER); + + // and observe the difference! + + this.setSize(600,500); + this.setVisible(true); + this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); + + // also try flipping round the positions of the components and + // see how things work out... + } +} diff --git a/SwingCodeExamplesNB/src/main/java/javateaching/layoutmanagers/BorderLayoutExample.java b/SwingCodeExamplesNB/src/main/java/javateaching/layoutmanagers/BorderLayoutExample.java new file mode 100644 index 0000000..7921917 --- /dev/null +++ b/SwingCodeExamplesNB/src/main/java/javateaching/layoutmanagers/BorderLayoutExample.java @@ -0,0 +1,51 @@ +/* + * To change this template, choose Tools | Templates + * and open the template in the editor. + */ + +package javateaching.layoutmanagers; + +import java.awt.BorderLayout; +import java.awt.Color; +import javax.swing.JLabel; +import javax.swing.JPanel; + +/** + * + * @author + */ +public class BorderLayoutExample extends JPanel { + public BorderLayoutExample() + { + BorderLayout bl = new BorderLayout(); + this.setLayout(bl); + + JLabel component1 = new JLabel("component 1",JLabel.CENTER); + component1.setBackground(Color.YELLOW); + component1.setOpaque(true); + + JLabel component2 = new JLabel("component 2", JLabel.CENTER); + component2.setBackground(Color.GREEN); + component2.setOpaque(true); + + JLabel component3 = new JLabel("component 3"); + component3.setBackground(Color.ORANGE); + component3.setOpaque(true); + + JLabel component4 = new JLabel("component 4"); + component4.setBackground(Color.CYAN); + component4.setOpaque(true); + + /* JLabel component5 = new JLabel("

component 5 with BIGNESS

"); + component5.setBackground(Color.WHITE); + component5.setOpaque(true); */ + + JPanel component5 = new GridTo500(); + + this.add(component1,BorderLayout.NORTH); + this.add(component2,BorderLayout.SOUTH); + this.add(component3,BorderLayout.EAST); + this.add(component4,BorderLayout.WEST); + this.add(component5,BorderLayout.CENTER); + } +} diff --git a/SwingCodeExamplesNB/src/main/java/javateaching/layoutmanagers/Cat.java b/SwingCodeExamplesNB/src/main/java/javateaching/layoutmanagers/Cat.java new file mode 100644 index 0000000..eb803db --- /dev/null +++ b/SwingCodeExamplesNB/src/main/java/javateaching/layoutmanagers/Cat.java @@ -0,0 +1,60 @@ +/* + * To change this template, choose Tools | Templates + * and open the template in the editor. + */ + +package javateaching.layoutmanagers; + +/** + * + * @author paul + */ +public class Cat { + + private String colour; + private int numberOfLegs; + private int loudnessOfPurr; + + public void purr() + { + // cat purrs + } + + public void inexplicablyScratchYou() + { + // etc + } + + public String getColour() + { + return colour; + } + + public void setColour(String colour) + { + this.colour = colour; + } + + public int getLoudnessOfPurr() + { + return loudnessOfPurr; + } + + public void setLoudnessOfPurr(int loudnessOfPurr) + { + this.loudnessOfPurr = loudnessOfPurr; + } + + public int getNumberOfLegs() + { + return numberOfLegs; + } + + public void setNumberOfLegs(int numberOfLegs) + { + this.numberOfLegs = numberOfLegs; + } + + + +} diff --git a/SwingCodeExamplesNB/src/main/java/javateaching/layoutmanagers/CombiningLayouts.java b/SwingCodeExamplesNB/src/main/java/javateaching/layoutmanagers/CombiningLayouts.java new file mode 100644 index 0000000..fc0498e --- /dev/null +++ b/SwingCodeExamplesNB/src/main/java/javateaching/layoutmanagers/CombiningLayouts.java @@ -0,0 +1,40 @@ +/* + * To change this license header, choose License Headers in Project Properties. + * To change this template file, choose Tools | Templates + * and open the template in the editor. + */ +package javateaching.layoutmanagers; + +import java.awt.BorderLayout; +import java.awt.GridLayout; +import javax.swing.JButton; +import javax.swing.JFrame; +import javax.swing.JPanel; + +/** + * + * @author + */ +public class CombiningLayouts { + public static void main(String[] args) { +JFrame frame = new JFrame(); +frame.setLayout(new BorderLayout()); +frame.add(new JButton("North"),BorderLayout.NORTH); +frame.add(new JButton("South"),BorderLayout.SOUTH); +frame.add(new JButton("East"),BorderLayout.EAST); +frame.add(new JButton("West"),BorderLayout.WEST); + +JPanel middlePanel = new JPanel(); +middlePanel.setLayout(new GridLayout(2,2)); +middlePanel.add(new JButton("1")); +middlePanel.add(new JButton("2")); +middlePanel.add(new JButton("3")); +middlePanel.add(new JButton("4")); +frame.add(middlePanel,BorderLayout.CENTER); + +frame.setSize(400,400); +frame.setVisible(true); + + frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); + } +} diff --git a/SwingCodeExamplesNB/src/main/java/javateaching/layoutmanagers/ComponentShowcase.java b/SwingCodeExamplesNB/src/main/java/javateaching/layoutmanagers/ComponentShowcase.java new file mode 100644 index 0000000..4261d9e --- /dev/null +++ b/SwingCodeExamplesNB/src/main/java/javateaching/layoutmanagers/ComponentShowcase.java @@ -0,0 +1,57 @@ +/* + * To change this template, choose Tools | Templates + * and open the template in the editor. + */ + +package javateaching.layoutmanagers; + +import java.awt.FlowLayout; +import javax.swing.JButton; +import javax.swing.JCheckBox; +import javax.swing.JComboBox; +import javax.swing.JFrame; +import javax.swing.JLabel; +import javax.swing.JList; +import javax.swing.JRadioButton; +import javax.swing.JTextField; + +/** + * + * @author + */ +public class ComponentShowcase { + + public static void main(String[] args) + { + // add top level window + JFrame ourWindow = new JFrame("Component showcase"); + // set layout + ourWindow.setLayout(new FlowLayout()); + + // create components + JButton buttonOK = new JButton("OK"); + JLabel label = new JLabel("Enter your name"); + JTextField text = new JTextField("Bill Bloggs"); + JCheckBox chbox = new JCheckBox("Option A"); + JRadioButton rbut = new JRadioButton("Option 1"); + JComboBox cbox = new JComboBox(new String [] {"red", "green", "blue"}); + JList jlist = new JList(new String [] {"red", "green", "blue"}); + + // add components + ourWindow.add(buttonOK); + ourWindow.add(label); + ourWindow.add(text); + ourWindow.add(chbox); + ourWindow.add(rbut); + ourWindow.add(cbox); + ourWindow.add(jlist); + + + // set size + ourWindow.setSize(650,60); //ourWindow.pack(); + // set visibility + ourWindow.setVisible(true); + ourWindow.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); + } +} + diff --git a/SwingCodeExamplesNB/src/main/java/javateaching/layoutmanagers/ConstructorDemo.java b/SwingCodeExamplesNB/src/main/java/javateaching/layoutmanagers/ConstructorDemo.java new file mode 100644 index 0000000..6c3c425 --- /dev/null +++ b/SwingCodeExamplesNB/src/main/java/javateaching/layoutmanagers/ConstructorDemo.java @@ -0,0 +1,68 @@ +/* + * To change this template, choose Tools | Templates + * and open the template in the editor. + */ + +package javateaching.layoutmanagers; + +import java.io.BufferedReader; +import java.io.File; +import java.io.FileReader; +import java.io.IOException; + +/** + * + * @author + */ +public class ConstructorDemo { + + public ConstructorDemo(String msg) + { + System.out.println("I've been constructed with "+msg); + } + + public ConstructorDemo() + { + this("a default value"); + } + + public static void main(String[] args) + { + //ConstructorDemo z = new ConstructorDemo(); + //ConstructorDemo x = new ConstructorDemo(" an elephant in a bun"); + File file = new File("test.txt"); + // original FileReader only code + try + { + BufferedReader bufInput1 = + new BufferedReader(new FileReader(file)); + String str = bufInput1.readLine(); + while (str != null) + { + //students.add(new Student(str)); + ConstructorDemo c = new ConstructorDemo(str); + str = bufInput1.readLine(); + + } + bufInput1.close(); + } + catch (Exception ex) + { + ex.printStackTrace(); + } + + /* + try { + InputStreamReader reader = + new InputStreamReader(new FileInputStream("myfile.txt"), "UTF-8"); + BufferedReader bufInput1 = new BufferedReader(reader); + } catch (FileNotFoundException ex) { + // do nothing + } catch (UnsupportedEncodingException ex) + { + // do nothing + } + */ + } + +} diff --git a/SwingCodeExamplesNB/src/main/java/javateaching/layoutmanagers/Controller.java b/SwingCodeExamplesNB/src/main/java/javateaching/layoutmanagers/Controller.java new file mode 100644 index 0000000..96c4b8e --- /dev/null +++ b/SwingCodeExamplesNB/src/main/java/javateaching/layoutmanagers/Controller.java @@ -0,0 +1,83 @@ +/* + * To change this template, choose Tools | Templates + * and open the template in the editor. + */ + +package javateaching.layoutmanagers; + +import java.awt.event.ActionEvent; +import java.awt.event.ActionListener; +import java.awt.event.MouseEvent; +import java.awt.event.MouseListener; +import javax.swing.JFrame; + +/** + * + * @author + */ +public class Controller implements ActionListener, MouseListener +{ + private static Controller oneInstance ;//= new Controller(); + + public static Controller getInstance() { + if (oneInstance == null){ + oneInstance = new Controller(); + } + return oneInstance; + } + + // singleton pattern + private Controller() + { + + } + + public void actionPerformed(ActionEvent ae) + { + + System.out.println(ae.getActionCommand()); + if (ae.getActionCommand().equals("clickme")) { + GridTo500 g500 = new GridTo500(); + JFrame newWindow = new JFrame("New"); + newWindow.add(g500); + newWindow.setSize(500, 500); + newWindow.setVisible(true); + } + if (ae.getActionCommand().equals("abc")) + { + System.out.println("hello"); + } + } + +// @Override +// public void actionPerformed(ActionEvent ae) { +// throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates. +// } + + @Override + public void mouseClicked(MouseEvent me) { + throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates. + } + + @Override + public void mousePressed(MouseEvent me) { + throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates. + } + + @Override + public void mouseReleased(MouseEvent me) { + throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates. + } + + @Override + public void mouseEntered(MouseEvent me) { + throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates. + } + + @Override + public void mouseExited(MouseEvent me) { + throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates. + } + + +} diff --git a/SwingCodeExamplesNB/src/main/java/javateaching/layoutmanagers/FlowLayoutExample.java b/SwingCodeExamplesNB/src/main/java/javateaching/layoutmanagers/FlowLayoutExample.java new file mode 100644 index 0000000..3f24a63 --- /dev/null +++ b/SwingCodeExamplesNB/src/main/java/javateaching/layoutmanagers/FlowLayoutExample.java @@ -0,0 +1,54 @@ +/* + * To change this template, choose Tools | Templates + * and open the template in the editor. + */ + +package javateaching.layoutmanagers; + +import java.awt.FlowLayout; +import java.awt.Color; +import javax.swing.JButton; +import javax.swing.JLabel; +import javax.swing.JPanel; + +/** + * + * @author + */ +public class FlowLayoutExample extends JPanel { + public FlowLayoutExample() + { + FlowLayout fl = new FlowLayout(); + this.setLayout(fl); + + /*JLabel component1 = new JLabel("component 1"); + component1.setBackground(Color.YELLOW); + component1.setOpaque(true); */ + + JButton component1 = new JButton("click me"); + component1.setActionCommand("clickme"); + component1.addActionListener(Controller.getInstance()); + + JLabel component2 = new JLabel("component 2"); + component2.setBackground(Color.GREEN); + component2.setOpaque(true); + + JLabel component3 = new JLabel("component 3"); + component3.setBackground(Color.ORANGE); + component3.setOpaque(true); + + JLabel component4 = new JLabel("component 4"); + component4.setBackground(Color.CYAN); + component4.setOpaque(true); + + JLabel component5 = new JLabel("component 5"); + component5.setBackground(Color.WHITE); + component5.setOpaque(true); + + this.add(component1); + this.add(component2); + this.add(component3); + this.add(component4); + this.add(component5); + } +} diff --git a/SwingCodeExamplesNB/src/main/java/javateaching/layoutmanagers/GridLayoutExample.java b/SwingCodeExamplesNB/src/main/java/javateaching/layoutmanagers/GridLayoutExample.java new file mode 100644 index 0000000..0bcb852 --- /dev/null +++ b/SwingCodeExamplesNB/src/main/java/javateaching/layoutmanagers/GridLayoutExample.java @@ -0,0 +1,54 @@ +/* + * To change this template, choose Tools | Templates + * and open the template in the editor. + */ + +package javateaching.layoutmanagers; + +import java.awt.Color; +import java.awt.GridLayout; +import javax.swing.JLabel; +import javax.swing.JPanel; + +/** + * + * @author + */ +public class GridLayoutExample extends JPanel { + public GridLayoutExample() + { + GridLayout gl = new GridLayout(2,3); + this.setLayout(gl); + + JLabel component1 = new JLabel("component 1",JLabel.CENTER); + component1.setBackground(Color.YELLOW); + component1.setOpaque(true); + + JLabel component2 = new JLabel("component 2", JLabel.CENTER); + component2.setBackground(Color.GREEN); + component2.setOpaque(true); + + JLabel component3 = new JLabel("component 3"); + component3.setBackground(Color.ORANGE); + component3.setOpaque(true); + + JLabel component4 = new JLabel("component 4"); + component4.setBackground(Color.CYAN); + component4.setOpaque(true); + + JLabel component5 = new JLabel("

component 5 with BIGNESS

"); + component5.setBackground(Color.WHITE); + component5.setOpaque(true); + + JLabel component6 = new JLabel("component 6"); + component4.setBackground(Color.CYAN); + component4.setOpaque(true); + + this.add(component1); + this.add(component2); + this.add(component3); + this.add(component4); + this.add(component5); + this.add(component6); + } +} diff --git a/SwingCodeExamplesNB/src/main/java/javateaching/layoutmanagers/GridTo500.java b/SwingCodeExamplesNB/src/main/java/javateaching/layoutmanagers/GridTo500.java new file mode 100644 index 0000000..cc66c3c --- /dev/null +++ b/SwingCodeExamplesNB/src/main/java/javateaching/layoutmanagers/GridTo500.java @@ -0,0 +1,38 @@ +/* + * To change this template, choose Tools | Templates + * and open the template in the editor. + */ + +package javateaching.layoutmanagers; + +import java.awt.Color; +import java.awt.GridLayout; +import java.util.Random; +import javax.swing.JButton; +import javax.swing.JLabel; +import javax.swing.JPanel; + +/** + * + * @author + */ +public class GridTo500 extends JPanel { + public GridTo500() + { + GridLayout gl = new GridLayout(0,15); + this.setLayout(gl); + + for (int i = 1; i < 501; i++) + { + JLabel numberJL = new JLabel(String.valueOf(i+" ")); + numberJL.setOpaque(true); + numberJL.setBackground(Color.getHSBColor(new Random().nextFloat(), 1.0F, 1.0F )); + this.add(numberJL); + } + JButton button = new JButton("spling"); + // button.setActionCommand("abc"); + button.setActionCommand("clickme"); + button.addActionListener(Controller.getInstance()); + this.add(button); + } +} diff --git a/SwingCodeExamplesNB/src/main/java/javateaching/layoutmanagers/LayoutManagers.java b/SwingCodeExamplesNB/src/main/java/javateaching/layoutmanagers/LayoutManagers.java new file mode 100644 index 0000000..ccc3c8b --- /dev/null +++ b/SwingCodeExamplesNB/src/main/java/javateaching/layoutmanagers/LayoutManagers.java @@ -0,0 +1,37 @@ +/* + * To change this template, choose Tools | Templates + * and open the template in the editor. + */ + +package javateaching.layoutmanagers; + +import javax.swing.JFrame; +import javax.swing.JPanel; + +/** + * + * @author + */ +public class LayoutManagers { + + /** + * @param args the command line arguments + */ + public static void main(String[] args) { + // JPanel jp = new FlowLayoutExample(); + //JPanel jp = new GridLayoutExample(); + JPanel jp = new BorderLayoutExample(); + // JPanel jp = new MigLayoutExample(); + //JPanel jp = new GridTo500(); + JFrame mainWindow = new JFrame("Layout Manager Demo"); + System.out.println(mainWindow); + mainWindow.add(jp); + mainWindow.pack(); +// mainWindow.setBounds(50,50,600,600); + mainWindow.setVisible(true); + Object o = new Object(); + System.out.println(o.toString()); + mainWindow.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); + } + +} diff --git a/SwingCodeExamplesNB/src/main/java/javateaching/layoutmanagers/MigLayoutExample.java b/SwingCodeExamplesNB/src/main/java/javateaching/layoutmanagers/MigLayoutExample.java new file mode 100644 index 0000000..b49242d --- /dev/null +++ b/SwingCodeExamplesNB/src/main/java/javateaching/layoutmanagers/MigLayoutExample.java @@ -0,0 +1,50 @@ +/* + * To change this template, choose Tools | Templates + * and open the template in the editor. + */ + +package javateaching.layoutmanagers; + +import java.awt.Color; +import javax.swing.JLabel; +import javax.swing.JPanel; +import net.miginfocom.swing.MigLayout; + +/** + * + * @author + */ +public class MigLayoutExample extends JPanel { + private JLabel component4; + public MigLayoutExample() + { + MigLayout migL = new MigLayout("wrap 2"); + this.setLayout(migL); + + JLabel component1 = new JLabel("component 1"); + component1.setBackground(Color.YELLOW); + component1.setOpaque(true); + + JLabel component2 = new JLabel("component 2"); + component2.setBackground(Color.GREEN); + component2.setOpaque(true); + + JLabel component3 = new JLabel("component 3"); + component3.setBackground(Color.ORANGE); + component3.setOpaque(true); + + component4 = new JLabel("component 4"); + component4.setBackground(Color.CYAN); + component4.setOpaque(true); + + JLabel component5 = new JLabel("component 5"); + component5.setBackground(Color.WHITE); + component5.setOpaque(true); + + this.add(component1,"width 70%"); + this.add(component2,"grow"); + this.add(component3); + this.add(component4); + this.add(component5,"span 2, width 100%"); + } +} diff --git a/SwingCodeExamplesNB/src/main/java/javateaching/layoutmanagers/ScrollPaneDemo.java b/SwingCodeExamplesNB/src/main/java/javateaching/layoutmanagers/ScrollPaneDemo.java new file mode 100644 index 0000000..05f456e --- /dev/null +++ b/SwingCodeExamplesNB/src/main/java/javateaching/layoutmanagers/ScrollPaneDemo.java @@ -0,0 +1,36 @@ +/* + * To change this template, choose Tools | Templates + * and open the template in the editor. + */ + +package javateaching.layoutmanagers; + +import javax.swing.JFrame; +import javax.swing.JPanel; +import javax.swing.JScrollPane; + +/** + * + * @author + */ +public class ScrollPaneDemo { + + /** + * @param args the command line arguments + */ + public static void main(String[] args) { + //JPanel jp = new FlowLayoutExample(); + //JPanel jp = new GridLayoutExample(); + //JPanel jp = new BorderLayoutExample(); + //JPanel jp = new MigLayoutExample(); + GridTo500 g500 = new GridTo500(); + JFrame mainWindow = new JFrame("ScrollPane Manager Demo"); + JScrollPane js = new JScrollPane(g500); + mainWindow.add(js); + mainWindow.pack(); + //mainWindow.setBounds(50,50,600,600); + mainWindow.setVisible(true); + mainWindow.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); + } + +} diff --git a/SwingCodeExamplesNB/src/main/java/javateaching/leanconsole/GCDemo.java b/SwingCodeExamplesNB/src/main/java/javateaching/leanconsole/GCDemo.java new file mode 100644 index 0000000..b0da538 --- /dev/null +++ b/SwingCodeExamplesNB/src/main/java/javateaching/leanconsole/GCDemo.java @@ -0,0 +1,43 @@ +/* + * To change this license header, choose License Headers in Project Properties. + * To change this template file, choose Tools | Templates + * and open the template in the editor. + */ +package javateaching.leanconsole; + +import java.awt.BorderLayout; +import javax.swing.JFrame; +import javax.swing.WindowConstants; + +/** + * + * @author paul + */ +public class GCDemo +{ + public static void main(String[] args) throws InterruptedException + { + JFrame ourwindow = new JFrame("Yay! Window"); + ourwindow.setLayout(new BorderLayout()); + + LeanConsoleWithInput console = new LeanConsoleWithInput(); + ourwindow.add(console,BorderLayout.CENTER); + + ourwindow.setSize(500,400); + ourwindow.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); + ourwindow.setVisible(true); + + console.println("Hello everyone!"); + + for (int i = 1; i <= 200; i++) + { + console.println("Hello times "+i); + } + + console.println("What is your name?"); + String name = console.readln(); + console.println("Hello, "+name); + console.println("*** end of program ***"); + } +} + diff --git a/SwingCodeExamplesNB/src/main/java/javateaching/leanconsole/LeanConsole.java b/SwingCodeExamplesNB/src/main/java/javateaching/leanconsole/LeanConsole.java new file mode 100644 index 0000000..a887e1e --- /dev/null +++ b/SwingCodeExamplesNB/src/main/java/javateaching/leanconsole/LeanConsole.java @@ -0,0 +1,52 @@ +/* + * To change this license header, choose License Headers in Project Properties. + * To change this template file, choose Tools | Templates + * and open the template in the editor. + */ +package javateaching.leanconsole; + +import java.awt.BorderLayout; +import javax.swing.JPanel; +import javax.swing.JScrollPane; +import javax.swing.JTextArea; + +/** + * + * @author paul + */ +public class LeanConsole extends JPanel +{ + private JTextArea textarea = new JTextArea(); + + public LeanConsole() + { + this.setLayout(new BorderLayout()); + JScrollPane scroll = new JScrollPane(textarea); + this.add(scroll,BorderLayout.CENTER); + textarea.setLineWrap(true); // otherwise everything is on one line + textarea.setEditable(false); // otherwise people can type over the text + } + + public void print(String text) + { + String currentText = this.getTextarea().getText(); + currentText += text; + this.getTextarea().setText(currentText); + this.getTextarea().setCaretPosition(this.getTextarea().getDocument().getLength()); + } + + public void println(String text) + { + this.print(text+System.getProperty("line.separator")); + } + + public void clear() + { + this.getTextarea().setText(""); + } + + public JTextArea getTextarea() { + return textarea; + } +} + diff --git a/SwingCodeExamplesNB/src/main/java/javateaching/leanconsole/LeanConsoleWithInput.java b/SwingCodeExamplesNB/src/main/java/javateaching/leanconsole/LeanConsoleWithInput.java new file mode 100644 index 0000000..f210325 --- /dev/null +++ b/SwingCodeExamplesNB/src/main/java/javateaching/leanconsole/LeanConsoleWithInput.java @@ -0,0 +1,101 @@ +/* + * To change this license header, choose License Headers in Project Properties. + * To change this template file, choose Tools | Templates + * and open the template in the editor. + */ +package javateaching.leanconsole; + +import java.awt.BorderLayout; +import java.awt.event.KeyEvent; +import java.awt.event.KeyListener; +import java.util.logging.Level; +import java.util.logging.Logger; +import javax.swing.JPanel; +import javax.swing.JScrollPane; +import javax.swing.JTextArea; +import javax.swing.JTextField; + +/** + * + * @author paul + */ +public class LeanConsoleWithInput extends JPanel +{ + private JTextArea textarea = new JTextArea(); + private JTextField inputfield = new JTextField(); + + private boolean returnPressed = false; + + public LeanConsoleWithInput() + { + this.setLayout(new BorderLayout()); + JScrollPane scroll = new JScrollPane(textarea); + this.add(scroll,BorderLayout.CENTER); + textarea.setLineWrap(true); // otherwise everything is on one line + textarea.setEditable(false); // otherwise people can type over the text + // add input field at bottom + this.add(inputfield,BorderLayout.SOUTH); + inputfield.addKeyListener(new KeyListener(){ + @Override + public void keyTyped(KeyEvent e) {} + + @Override + public void keyPressed(KeyEvent e) {} + + @Override + public void keyReleased(KeyEvent e) { + // while it might seem more natural to hook keyTyped, + // apparently getKeyCode only returns values for + // keyPressed and keyReleased. + if (e.getKeyCode() == KeyEvent.VK_ENTER) returnPressed = true; + } + }); + } + + public void print(String text) + { + String currentText = this.getTextarea().getText(); + currentText += text; + this.getTextarea().setText(currentText); + this.getTextarea().setCaretPosition(this.getTextarea().getDocument().getLength()); + } + + public void println(String text) + { + this.print(text+System.getProperty("line.separator")); + } + + public String readln() + { + // clear any existing text + inputfield.setText(""); + // put the cursor in the input field + inputfield.requestFocusInWindow(); + this.returnPressed = false; + + // returnPressed is going to get set when the + // user presses return on the KeyListener for + // the input field. We wait until it is... + while (!this.returnPressed) + { + // add in a brief pause to processing. Otherwise this loop would + // peg the CPU at 100% + try { Thread.sleep(200); } catch (InterruptedException ex) {} + } + String result = inputfield.getText(); + // we've pressed return, so clear the field + inputfield.setText(""); + this.returnPressed = false; + return result; + } + + public void clear() + { + this.getTextarea().setText(""); + } + + public JTextArea getTextarea() { + return textarea; + } +} + diff --git a/SwingCodeExamplesNB/src/main/java/singletonexample/MySingleton.java b/SwingCodeExamplesNB/src/main/java/singletonexample/MySingleton.java new file mode 100644 index 0000000..122c3f7 --- /dev/null +++ b/SwingCodeExamplesNB/src/main/java/singletonexample/MySingleton.java @@ -0,0 +1,57 @@ +/* + * To change this license header, choose License Headers in Project Properties. + * To change this template file, choose Tools | Templates + * and open the template in the editor. + */ +package singletonexample; + +import java.util.ArrayList; + +/** + * + * @author localadmin + */ +public class MySingleton { + // this is the static attribute which contains the only object of this + // example class. being static it won't get swept up by the garbage collector + // it is null before the constructor is called + private static MySingleton uniqueInstance; + + // instance variables here + // some data for our unique instance object to show you can use it + // to get model data in this case just Strings empty at first but it could be + // Collections of objects or anything + private String myUniqueInstanceData; + + + // this is the way of getting the unique instance + // printlines have been put in to show how it works + public static MySingleton getInstance() { + if (uniqueInstance == null) { + System.out.println("The uniqueInstance is null."); + System.out.println("Call the constructor"); + uniqueInstance = new MySingleton(); + } + System.out.println("We have a uniqueInstance so return it"); + return uniqueInstance; + } + + // the private constructor + private MySingleton() { + // a print line just to show when the constructor is called + // it is a private constructor and can only be called from the getInstance method + System.out.println("Creating our uniqueInstance"); + } + + // other useful methods here + + // getters and setters for whatever data we have in our uniqueInstance + public String getMyUniqueInstanceData() { + return myUniqueInstanceData; + } + + public void setMyUniqueInstanceData(String myUniqueInstanceData) { + this.myUniqueInstanceData = myUniqueInstanceData; + } + +} diff --git a/SwingCodeExamplesNB/src/main/java/singletonexample/SingletonExample.java b/SwingCodeExamplesNB/src/main/java/singletonexample/SingletonExample.java new file mode 100644 index 0000000..af3691a --- /dev/null +++ b/SwingCodeExamplesNB/src/main/java/singletonexample/SingletonExample.java @@ -0,0 +1,37 @@ +/* + * To change this license header, choose License Headers in Project Properties. + * To change this template file, choose Tools | Templates + * and open the template in the editor. + */ +package singletonexample; + +import java.util.ArrayList; + +/** + * + * @author localadmin + */ +public class SingletonExample { + + /** + * @param args the command line arguments + */ + public static void main(String[] args) { + // We are going to get our uniqueInstance of a Singleton object + System.out.println("First call to get our uniqueInstance"); + MySingleton theOnlyOne = MySingleton.getInstance(); + System.out.println("Now I'm going to put some data in and it will be: theOnlyOne"); + theOnlyOne.setMyUniqueInstanceData("theOnlyOne"); + // now I am going to retrieve the data + System.out.println("Now I want to get my uniqueInstance again (I can get it from anywhere in my application)"); + + MySingleton theOnlyOneAgain = MySingleton.getInstance(); + + System.out.println("Let's see if it is the same object - what's the data?"); + + System.out.println(theOnlyOneAgain.getMyUniqueInstanceData()); + + System.out.println("Hey man it's the same object! That was easy :-) No more passing the object all over the place. Patterns rule!"); + } + +}