├── .gitignore ├── README.md ├── src └── main │ ├── resources │ └── com │ │ └── icon │ │ ├── Add.png │ │ ├── Accept.png │ │ ├── Cancel.png │ │ └── Refresh.png │ └── java │ ├── JFrameTutorial.java │ ├── JLabelTutorial.java │ ├── JScrollPaneTutorial.java │ ├── JTextFieldTutorial.java │ ├── JWindowTutorial.java │ ├── JNumberTextFieldTutorial.java │ ├── JButtonTutorial.java │ ├── JSplitPaneTutorial.java │ ├── FlowLayoutTutorial.java │ ├── JToolBarTutorial.java │ ├── JTextAreaTutorial.java │ ├── BoxLayoutTutorial.java │ ├── com │ └── event │ │ ├── ButtonEvent.java │ │ └── TextFieldEvent.java │ ├── BorderLayoutTutorial.java │ ├── JTabbedPaneTutorial.java │ ├── JPanelTutorial.java │ ├── GridLayoutTutorial.java │ ├── JInternalFrameTutorial.java │ ├── JSliderTutorial.java │ ├── GridBagLayoutTutorial.java │ ├── MigLayoutTutorial.java │ ├── JOptionPaneTutorial.java │ ├── CardLayoutTutorial.java │ ├── LoadingScreenDemo.java │ ├── JRadioButtonTutorial.java │ ├── JPasswordFieldTutorial.java │ ├── JComboBoxTutorial.java │ ├── JListTutorial.java │ └── JNumberTextField.java └── pom.xml /.gitignore: -------------------------------------------------------------------------------- 1 | .idea 2 | target 3 | swing-tutorials.iml -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | Examples for basic usage of Swing components. 2 | 3 | Feel free to contribute! 4 | -------------------------------------------------------------------------------- /src/main/resources/com/icon/Add.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/BranislavLazic/SwingTutorials/HEAD/src/main/resources/com/icon/Add.png -------------------------------------------------------------------------------- /src/main/resources/com/icon/Accept.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/BranislavLazic/SwingTutorials/HEAD/src/main/resources/com/icon/Accept.png -------------------------------------------------------------------------------- /src/main/resources/com/icon/Cancel.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/BranislavLazic/SwingTutorials/HEAD/src/main/resources/com/icon/Cancel.png -------------------------------------------------------------------------------- /src/main/resources/com/icon/Refresh.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/BranislavLazic/SwingTutorials/HEAD/src/main/resources/com/icon/Refresh.png -------------------------------------------------------------------------------- /src/main/java/JFrameTutorial.java: -------------------------------------------------------------------------------- 1 | import javax.swing.JFrame; 2 | 3 | public class JFrameTutorial { 4 | 5 | public static void main(String[] args) { 6 | JFrame frame = new JFrame("JFrame demo"); 7 | JPanelTutorial panel = new JPanelTutorial(); 8 | frame.add(panel); 9 | frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE); 10 | frame.setSize(320,240); 11 | frame.setVisible(true); 12 | } 13 | } 14 | -------------------------------------------------------------------------------- /src/main/java/JLabelTutorial.java: -------------------------------------------------------------------------------- 1 | import javax.swing.*; 2 | 3 | public class JLabelTutorial { 4 | 5 | public static void main(String[] args) { 6 | JFrame frame = new JFrame("JLabel demo"); 7 | JLabel label = new JLabel(); 8 | label.setIcon(new ImageIcon("C:/Documents and Settings/All Users/Documents/My Pictures/Sample Pictures/Water lilies.jpg")); 9 | 10 | frame.add(label); 11 | 12 | frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE); 13 | frame.pack(); 14 | frame.setVisible(true); 15 | } 16 | 17 | } 18 | -------------------------------------------------------------------------------- /src/main/java/JScrollPaneTutorial.java: -------------------------------------------------------------------------------- 1 | import javax.swing.*; 2 | 3 | public class JScrollPaneTutorial extends JFrame{ 4 | JLabel label = new JLabel(); 5 | JScrollPane scrollPane = new JScrollPane(); 6 | 7 | public JScrollPaneTutorial(){ 8 | label.setIcon(new ImageIcon("C:/Documents and Settings/All Users/Documents/My Pictures/Sample Pictures/Image.jpg")); 9 | scrollPane.setViewportView(label); 10 | add(scrollPane); 11 | } 12 | 13 | public static void main(String[] args) { 14 | JScrollPaneTutorial sp = new JScrollPaneTutorial(); 15 | sp.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE); 16 | //sp.setSize(1024,800); 17 | sp.pack(); 18 | sp.setVisible(true); 19 | } 20 | 21 | } 22 | -------------------------------------------------------------------------------- /src/main/java/JTextFieldTutorial.java: -------------------------------------------------------------------------------- 1 | import javax.swing.JFrame; 2 | import javax.swing.JTextField; 3 | import javax.swing.SwingUtilities; 4 | 5 | public class JTextFieldTutorial { 6 | 7 | JFrame frame = new JFrame("JTextField demo"); 8 | JTextField textfield = new JTextField("Hello from JTextField", 10); 9 | 10 | public JTextFieldTutorial() { 11 | frame.add(textfield); 12 | frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE); 13 | frame.pack(); 14 | frame.setVisible(true); 15 | } 16 | 17 | public static void main(String[] args) { 18 | SwingUtilities.invokeLater(new Runnable() { 19 | public void run() { 20 | new JTextFieldTutorial(); 21 | } 22 | }); 23 | } 24 | 25 | } 26 | -------------------------------------------------------------------------------- /src/main/java/JWindowTutorial.java: -------------------------------------------------------------------------------- 1 | import javax.swing.*; 2 | import java.awt.*; 3 | import java.awt.event.MouseAdapter; 4 | import java.awt.event.MouseEvent; 5 | 6 | public class JWindowTutorial extends JWindow{ 7 | JPanel panel = new JPanel(); 8 | 9 | public JWindowTutorial(){ 10 | panel.setBackground(Color.BLUE); 11 | add(panel); 12 | addMouseListener(new MouseAdapter(){ 13 | public void mouseClicked(MouseEvent e){ 14 | if(e.getClickCount() == 2){ 15 | dispose(); 16 | } 17 | } 18 | }); 19 | } 20 | 21 | public static void main(String[] args) { 22 | JWindowTutorial w = new JWindowTutorial(); 23 | w.setSize(640,480); 24 | w.setVisible(true); 25 | } 26 | 27 | } 28 | -------------------------------------------------------------------------------- /src/main/java/JNumberTextFieldTutorial.java: -------------------------------------------------------------------------------- 1 | import javax.swing.JFrame; 2 | import javax.swing.SwingUtilities; 3 | 4 | public class JNumberTextFieldTutorial { 5 | 6 | public static void main(String[] args) { 7 | SwingUtilities.invokeLater(new Runnable() { 8 | public void run() { 9 | JNumberTextField num = new JNumberTextField(); 10 | num.setColumns(15); 11 | num.setFormat(JNumberTextField.DECIMAL); 12 | num.setMaxLength(7); 13 | num.setPrecision(4); 14 | num.setAllowNegative(true); 15 | 16 | JFrame frame = new JFrame(); 17 | frame.add(num); 18 | frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE); 19 | frame.pack(); 20 | frame.setVisible(true); 21 | } 22 | }); 23 | } 24 | 25 | } 26 | -------------------------------------------------------------------------------- /src/main/java/JButtonTutorial.java: -------------------------------------------------------------------------------- 1 | import javax.swing.ImageIcon; 2 | import javax.swing.JButton; 3 | import javax.swing.JFrame; 4 | import javax.swing.SwingUtilities; 5 | 6 | public class JButtonTutorial { 7 | public static void main(String[] args) { 8 | SwingUtilities.invokeLater(new Runnable() { 9 | public void run() { 10 | JFrame frame = new JFrame(); 11 | JButton button = new JButton("Accept"); 12 | frame.add(button); 13 | /* 14 | * By calling setIcon method you are setting com on JButton by 15 | * passing ImageIcon instance 16 | */ 17 | button.setIcon(new ImageIcon(getClass().getResource( 18 | "com/icon/Accept.png"))); 19 | 20 | frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE); 21 | frame.pack(); 22 | frame.setVisible(true); 23 | } 24 | }); 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /src/main/java/JSplitPaneTutorial.java: -------------------------------------------------------------------------------- 1 | import javax.swing.*; 2 | 3 | public class JSplitPaneTutorial extends JFrame{ 4 | JLabel leftLabel = new JLabel(); 5 | JLabel rightLabel = new JLabel(); 6 | 7 | JSplitPane splitPane = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT, new JScrollPane(leftLabel),new JScrollPane(rightLabel)); 8 | 9 | public JSplitPaneTutorial(){ 10 | leftLabel.setIcon(new ImageIcon("C:/Documents and Settings/All Users/Documents/My Pictures/Sample Pictures/Water lilies.jpg")); 11 | rightLabel.setIcon(new ImageIcon("C:/Documents and Settings/All Users/Documents/My Pictures/Sample Pictures/Winter.jpg")); 12 | add(splitPane); 13 | } 14 | 15 | public static void main(String[] args) { 16 | JSplitPaneTutorial sp = new JSplitPaneTutorial(); 17 | sp.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE); 18 | sp.setSize(1024,600); 19 | sp.setVisible(true); 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /src/main/java/FlowLayoutTutorial.java: -------------------------------------------------------------------------------- 1 | import javax.swing.*; 2 | import java.awt.*; 3 | 4 | 5 | public class FlowLayoutTutorial { 6 | JFrame frame = new JFrame("FlowLayout demo"); 7 | JPanel panel = new JPanel(); 8 | JButton btn1 = new JButton("First"); 9 | JButton btn2 = new JButton("Second"); 10 | JButton btn3 = new JButton("Third"); 11 | 12 | public FlowLayoutTutorial() { 13 | panel.setLayout(new FlowLayout(FlowLayout.RIGHT,3,3)); 14 | panel.add(btn1); 15 | panel.add(btn2); 16 | panel.add(btn3); 17 | 18 | frame.add(panel); 19 | frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE); 20 | frame.pack(); 21 | frame.setVisible(true); 22 | 23 | } 24 | 25 | 26 | public static void main(String[] args) { 27 | SwingUtilities.invokeLater(new Runnable() { 28 | @Override 29 | public void run() { 30 | new FlowLayoutTutorial(); 31 | } 32 | }); 33 | } 34 | 35 | } 36 | -------------------------------------------------------------------------------- /src/main/java/JToolBarTutorial.java: -------------------------------------------------------------------------------- 1 | import javax.swing.*; 2 | import java.awt.*; 3 | 4 | public class JToolBarTutorial { 5 | JFrame frame = new JFrame(); 6 | JTextField textField = new JTextField(15); 7 | JButton btnAdd = new JButton("Add"); 8 | JButton btnDelete = new JButton("Delete"); 9 | JToolBar toolBar = new JToolBar(); 10 | 11 | public JToolBarTutorial() { 12 | toolBar.add(textField); 13 | toolBar.add(btnAdd); 14 | toolBar.addSeparator(); 15 | toolBar.add(btnDelete); 16 | toolBar.setFloatable(false); 17 | frame.add(toolBar, BorderLayout.NORTH); 18 | frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE); 19 | frame.pack(); 20 | frame.setVisible(true); 21 | } 22 | 23 | public static void main(String[] args) { 24 | SwingUtilities.invokeLater(new Runnable() { 25 | @Override 26 | public void run() { 27 | new JToolBarTutorial(); 28 | } 29 | }); 30 | } 31 | } 32 | -------------------------------------------------------------------------------- /src/main/java/JTextAreaTutorial.java: -------------------------------------------------------------------------------- 1 | import javax.swing.JFrame; 2 | import javax.swing.JScrollPane; 3 | import javax.swing.JTextArea; 4 | import javax.swing.SwingUtilities; 5 | 6 | public class JTextAreaTutorial { 7 | JFrame frame = new JFrame("JTextArea demo"); 8 | JTextArea textarea = new JTextArea("Hello", 10, 30); 9 | JScrollPane scrollPane = new JScrollPane(textarea); 10 | 11 | public JTextAreaTutorial() { 12 | //setEditable(); 13 | //setEnabled(); 14 | //setText(); getText(); 15 | textarea.append(" from JTextArea\n"); 16 | textarea.append(" frge\n"); 17 | frame.add(scrollPane); 18 | frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE); 19 | frame.pack(); 20 | frame.setVisible(true); 21 | } 22 | 23 | public static void main(String[] args) { 24 | SwingUtilities.invokeLater(new Runnable() { 25 | @Override 26 | public void run() { 27 | new JTextAreaTutorial(); 28 | } 29 | }); 30 | } 31 | 32 | } 33 | -------------------------------------------------------------------------------- /src/main/java/BoxLayoutTutorial.java: -------------------------------------------------------------------------------- 1 | import javax.swing.*; 2 | import java.awt.*; 3 | 4 | 5 | public class BoxLayoutTutorial { 6 | JFrame frame = new JFrame("BoxLayout tutorial"); 7 | JPanel panel = new JPanel(); 8 | JButton buttonFirst = new JButton("First"); 9 | JButton buttonSecond = new JButton("Second"); 10 | 11 | public BoxLayoutTutorial() { 12 | panel.setLayout(new BoxLayout(panel,BoxLayout.LINE_AXIS)); 13 | panel.add(buttonFirst); 14 | panel.add(Box.createHorizontalGlue()); 15 | panel.add(Box.createRigidArea(new Dimension(5,0))); 16 | panel.add(buttonSecond); 17 | frame.add(panel); 18 | frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE); 19 | frame.pack(); 20 | frame.setVisible(true); 21 | } 22 | 23 | public static void main(String[] args) { 24 | SwingUtilities.invokeLater(new Runnable() { 25 | @Override 26 | public void run() { 27 | new BoxLayoutTutorial(); 28 | } 29 | }); 30 | } 31 | 32 | } 33 | -------------------------------------------------------------------------------- /src/main/java/com/event/ButtonEvent.java: -------------------------------------------------------------------------------- 1 | package com.event; 2 | 3 | import java.awt.Color; 4 | import java.awt.event.ActionEvent; 5 | import java.awt.event.ActionListener; 6 | 7 | import javax.swing.JButton; 8 | import javax.swing.JFrame; 9 | import javax.swing.SwingUtilities; 10 | 11 | public class ButtonEvent { 12 | 13 | JFrame frame = new JFrame("Button event"); 14 | JButton button = new JButton("Click me"); 15 | 16 | public ButtonEvent() { 17 | button.addActionListener(new ActionListener() { 18 | public void actionPerformed(ActionEvent e) { 19 | button.setBackground(Color.RED); 20 | } 21 | }); 22 | frame.add(button); 23 | frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE); 24 | frame.pack(); 25 | frame.setVisible(true); 26 | } 27 | 28 | public static void main(String[] args) { 29 | SwingUtilities.invokeLater(new Runnable() { 30 | public void run() { 31 | new ButtonEvent(); 32 | } 33 | }); 34 | } 35 | 36 | } 37 | -------------------------------------------------------------------------------- /src/main/java/BorderLayoutTutorial.java: -------------------------------------------------------------------------------- 1 | import javax.swing.*; 2 | import java.awt.*; 3 | 4 | public class BorderLayoutTutorial { 5 | 6 | JFrame frame = new JFrame("BorderLayout demo"); 7 | JButton btn1 = new JButton("NORTH"); 8 | JButton btn2 = new JButton("SOUTH"); 9 | JButton btn3 = new JButton("CENTER"); 10 | JButton btn4 = new JButton("WEST"); 11 | JButton btn5 = new JButton("EAST"); 12 | 13 | public BorderLayoutTutorial() { 14 | frame.setLayout(new BorderLayout()); 15 | frame.add(btn1, BorderLayout.NORTH); 16 | frame.add(btn2, BorderLayout.SOUTH); 17 | frame.add(btn3, BorderLayout.CENTER); 18 | frame.add(btn4, BorderLayout.WEST); 19 | frame.add(btn5, BorderLayout.EAST); 20 | 21 | frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE); 22 | frame.pack(); 23 | frame.setVisible(true); 24 | } 25 | 26 | public static void main(String[] args) { 27 | SwingUtilities.invokeLater(new Runnable() { 28 | @Override 29 | public void run() { 30 | new BorderLayoutTutorial(); 31 | } 32 | }); 33 | } 34 | 35 | } 36 | -------------------------------------------------------------------------------- /src/main/java/JTabbedPaneTutorial.java: -------------------------------------------------------------------------------- 1 | import javax.swing.*; 2 | 3 | public class JTabbedPaneTutorial extends JFrame{ 4 | 5 | JPanel firstPanel = new JPanel(); 6 | JPanel secondPanel = new JPanel(); 7 | 8 | JLabel firstLabel = new JLabel("First!"); 9 | JLabel secondLabel = new JLabel("Second!"); 10 | 11 | JTabbedPane tabbedPane = new JTabbedPane(); 12 | 13 | public JTabbedPaneTutorial(){ 14 | firstPanel.add(firstLabel); 15 | secondPanel.add(secondLabel); 16 | 17 | tabbedPane.add("First panel",firstPanel); 18 | tabbedPane.add("Second panel",secondPanel); 19 | add(tabbedPane); 20 | } 21 | 22 | public static void main(String[] args) { 23 | try { 24 | UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName()); 25 | } catch (ClassNotFoundException | InstantiationException 26 | | IllegalAccessException | UnsupportedLookAndFeelException e) { 27 | e.printStackTrace(); 28 | } 29 | 30 | JTabbedPaneTutorial tp = new JTabbedPaneTutorial(); 31 | tp.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE); 32 | tp.setSize(600,400); 33 | tp.setVisible(true); 34 | } 35 | 36 | } 37 | -------------------------------------------------------------------------------- /src/main/java/JPanelTutorial.java: -------------------------------------------------------------------------------- 1 | import javax.imageio.ImageIO; 2 | import javax.swing.*; 3 | import java.awt.*; 4 | import java.awt.image.BufferedImage; 5 | import java.io.File; 6 | import java.io.IOException; 7 | 8 | public class JPanelTutorial extends JPanel{ 9 | BufferedImage image; 10 | 11 | public static void main(String[] args) { 12 | SwingUtilities.invokeLater(new Runnable() { 13 | @Override 14 | public void run() { 15 | JFrame frame = new JFrame(); 16 | frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE); 17 | frame.add(new JPanelTutorial()); 18 | frame.setExtendedState(JFrame.MAXIMIZED_BOTH); 19 | frame.setVisible(true); 20 | } 21 | }); 22 | } 23 | 24 | public JPanelTutorial(){ 25 | try { 26 | image = ImageIO.read(new File("C:/Documents and Settings/All Users/Documents/My Pictures/Sample Pictures/Winter.jpg")); 27 | } catch (IOException e) { 28 | e.printStackTrace(); 29 | } 30 | } 31 | 32 | public void paintComponent(Graphics g){ 33 | super.paintComponents(g); 34 | g.drawImage(image, 0,0,null); 35 | g.setColor(Color.RED); 36 | g.fillRect(10, 10, 100, 50); 37 | } 38 | } 39 | -------------------------------------------------------------------------------- /src/main/java/GridLayoutTutorial.java: -------------------------------------------------------------------------------- 1 | import java.awt.GridLayout; 2 | 3 | import javax.swing.JButton; 4 | import javax.swing.JFrame; 5 | import javax.swing.JPanel; 6 | import javax.swing.SwingUtilities; 7 | 8 | public class GridLayoutTutorial { 9 | JFrame frame = new JFrame("GridLayout demo"); 10 | JPanel panel = new JPanel(); 11 | JButton btn1 = new JButton("First"); 12 | JButton btn2 = new JButton("Second"); 13 | JButton btn3 = new JButton("Third"); 14 | JButton btn4 = new JButton("Fourth"); 15 | 16 | public GridLayoutTutorial() { 17 | panel.setLayout(new GridLayout(2,2,3,3)); 18 | panel.add(btn1); 19 | panel.add(btn2); 20 | panel.add(btn3); 21 | panel.add(btn4); 22 | //btn1.setPreferredSize(new Dimension(100,100)); 23 | 24 | frame.add(panel); 25 | 26 | frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE); 27 | frame.pack(); 28 | //frame.setSize(200,200); 29 | frame.setVisible(true); 30 | } 31 | 32 | public static void main(String[] args) { 33 | SwingUtilities.invokeLater(new Runnable() { 34 | @Override 35 | public void run() { 36 | new GridLayoutTutorial(); 37 | } 38 | }); 39 | } 40 | 41 | } 42 | -------------------------------------------------------------------------------- /src/main/java/JInternalFrameTutorial.java: -------------------------------------------------------------------------------- 1 | import javax.swing.JDesktopPane; 2 | import javax.swing.JFrame; 3 | import javax.swing.JInternalFrame; 4 | import javax.swing.SwingUtilities; 5 | 6 | public class JInternalFrameTutorial { 7 | 8 | public static void main(String[] args) { 9 | SwingUtilities.invokeLater(new Runnable() { 10 | public void run() { 11 | JDesktopPane desktopPane = new JDesktopPane(); 12 | JInternalFrame intFrame = new JInternalFrame( 13 | "JInternalFrame demo"); 14 | 15 | intFrame.setMaximizable(true); 16 | intFrame.setIconifiable(true); 17 | intFrame.setResizable(true); 18 | intFrame.setClosable(true); 19 | intFrame.setDefaultCloseOperation(JInternalFrame.DISPOSE_ON_CLOSE); 20 | 21 | intFrame.setSize(320, 240); 22 | // intFrame.pack(); 23 | intFrame.setVisible(true); 24 | 25 | desktopPane.add(intFrame); 26 | 27 | JFrame frame = new JFrame(); 28 | frame.add(desktopPane); 29 | frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE); 30 | frame.setSize(640, 480); 31 | // frame.pack(); 32 | 33 | frame.setVisible(true); 34 | 35 | } 36 | }); 37 | } 38 | } 39 | -------------------------------------------------------------------------------- /pom.xml: -------------------------------------------------------------------------------- 1 | 3 | 4.0.0 4 | 5 | com.swingtutorials 6 | swing-tutorials 7 | 1.0-SNAPSHOT 8 | jar 9 | 10 | 11 | 12 | com.miglayout 13 | miglayout-swing 14 | 4.1 15 | 16 | 17 | 18 | 19 | 20 | 21 | org.apache.maven.plugins 22 | maven-compiler-plugin 23 | 3.3 24 | 25 | 1.8 26 | 1.8 27 | 28 | 29 | 30 | 31 | -------------------------------------------------------------------------------- /src/main/java/JSliderTutorial.java: -------------------------------------------------------------------------------- 1 | import javax.swing.*; 2 | import javax.swing.event.ChangeEvent; 3 | import javax.swing.event.ChangeListener; 4 | import java.awt.*; 5 | 6 | public class JSliderTutorial { 7 | JFrame frame = new JFrame(); 8 | JLabel label = new JLabel(); 9 | JSlider slider = new JSlider(JSlider.HORIZONTAL,0,255,0); 10 | 11 | public JSliderTutorial() { 12 | label.setOpaque(true); 13 | slider.setMajorTickSpacing(10); 14 | slider.setMinorTickSpacing(1); 15 | slider.setPaintLabels(true); 16 | slider.setPaintTicks(true); 17 | slider.addChangeListener(new ChangeListener() { 18 | @Override 19 | public void stateChanged(ChangeEvent e) { 20 | JSlider source = (JSlider) e.getSource(); 21 | label.setBackground(new Color(0,0,source.getValue())); 22 | } 23 | }); 24 | frame.add(label, BorderLayout.CENTER); 25 | frame.add(slider,BorderLayout.SOUTH); 26 | frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE); 27 | frame.pack(); 28 | frame.setVisible(true); 29 | } 30 | 31 | public static void main(String[] args) { 32 | SwingUtilities.invokeLater(new Runnable() { 33 | @Override 34 | public void run() { 35 | new JSliderTutorial(); 36 | } 37 | }); 38 | } 39 | } 40 | -------------------------------------------------------------------------------- /src/main/java/GridBagLayoutTutorial.java: -------------------------------------------------------------------------------- 1 | import java.awt.GridBagConstraints; 2 | import java.awt.GridBagLayout; 3 | import java.awt.Insets; 4 | 5 | import javax.swing.JButton; 6 | import javax.swing.JFrame; 7 | import javax.swing.JPanel; 8 | import javax.swing.SwingUtilities; 9 | 10 | public class GridBagLayoutTutorial { 11 | JFrame frame = new JFrame(); 12 | JPanel panel = new JPanel(); 13 | JButton btn1 = new JButton("One"); 14 | JButton btn2 = new JButton("Two"); 15 | JButton btn3 = new JButton("Three"); 16 | 17 | public GridBagLayoutTutorial() { 18 | panel.setLayout(new GridBagLayout()); 19 | GridBagConstraints gbc = new GridBagConstraints(); 20 | gbc.fill = GridBagConstraints.HORIZONTAL; 21 | gbc.insets = new Insets(5, 5, 5, 5); 22 | 23 | gbc.gridx = 0; 24 | gbc.gridy = 0; 25 | panel.add(btn1, gbc); 26 | 27 | gbc.gridx = 1; 28 | gbc.gridy = 0; 29 | panel.add(btn2, gbc); 30 | 31 | gbc.gridwidth = 2; 32 | gbc.gridx = 0; 33 | gbc.gridy = 1; 34 | panel.add(btn3, gbc); 35 | 36 | frame.add(panel); 37 | frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE); 38 | frame.pack(); 39 | frame.setVisible(true); 40 | } 41 | 42 | public static void main(String[] args) { 43 | SwingUtilities.invokeLater(new Runnable() { 44 | @Override 45 | public void run() { 46 | new GridBagLayoutTutorial(); 47 | } 48 | }); 49 | } 50 | 51 | } 52 | -------------------------------------------------------------------------------- /src/main/java/MigLayoutTutorial.java: -------------------------------------------------------------------------------- 1 | import net.miginfocom.swing.MigLayout; 2 | 3 | import javax.swing.*; 4 | 5 | public class MigLayoutTutorial { 6 | JFrame frame = new JFrame("Employees"); 7 | JPanel panel = new JPanel(); 8 | 9 | JLabel lblFirstName = new JLabel("First name:"); 10 | JLabel lblLastName = new JLabel("Last name:"); 11 | JTextField txtFirstName = new JTextField(20); 12 | JTextField txtLastName = new JTextField(20); 13 | JButton btnCreate = new JButton("Create"); 14 | JButton btnDelete = new JButton("Delete"); 15 | JLabel lblDsc = new JLabel("Description:"); 16 | JTextArea txaDsc = new JTextArea(10, 10); 17 | 18 | public MigLayoutTutorial() { 19 | panel.setLayout(new MigLayout()); 20 | panel.add(lblFirstName); 21 | panel.add(txtFirstName, "wrap, pushx, growx"); 22 | panel.add(lblLastName); 23 | panel.add(txtLastName, "wrap, pushx, growx"); 24 | panel.add(btnCreate, "skip, split2"); 25 | panel.add(btnDelete, "wrap"); 26 | panel.add(lblDsc, "top"); 27 | panel.add(new JScrollPane(txaDsc), "push, grow"); 28 | 29 | frame.add(panel); 30 | frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE); 31 | frame.pack(); 32 | frame.setVisible(true); 33 | } 34 | 35 | public static void main(String[] args) { 36 | SwingUtilities.invokeLater(new Runnable() { 37 | @Override 38 | public void run() { 39 | new MigLayoutTutorial(); 40 | } 41 | }); 42 | } 43 | 44 | } 45 | -------------------------------------------------------------------------------- /src/main/java/com/event/TextFieldEvent.java: -------------------------------------------------------------------------------- 1 | package com.event; 2 | 3 | import javax.swing.JButton; 4 | import javax.swing.JFrame; 5 | import javax.swing.JPanel; 6 | import javax.swing.JTextField; 7 | import javax.swing.SwingUtilities; 8 | import javax.swing.event.DocumentEvent; 9 | import javax.swing.event.DocumentListener; 10 | 11 | public class TextFieldEvent { 12 | 13 | JFrame frame = new JFrame("JTextField demo"); 14 | JPanel panel = new JPanel(); 15 | JTextField textfield = new JTextField("", 20); 16 | JButton button = new JButton("Ok"); 17 | 18 | public TextFieldEvent() { 19 | textfield.getDocument().addDocumentListener(new DocumentListener() { 20 | 21 | @Override 22 | public void changedUpdate(DocumentEvent arg0) { 23 | warn(); 24 | } 25 | 26 | @Override 27 | public void insertUpdate(DocumentEvent arg0) { 28 | warn(); 29 | } 30 | 31 | @Override 32 | public void removeUpdate(DocumentEvent arg0) { 33 | warn(); 34 | } 35 | 36 | public void warn(){ 37 | button.setText(textfield.getText()); 38 | } 39 | }); 40 | panel.add(textfield); 41 | panel.add(button); 42 | frame.add(panel); 43 | frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE); 44 | frame.pack(); 45 | frame.setVisible(true); 46 | } 47 | 48 | public static void main(String[] args) { 49 | SwingUtilities.invokeLater(new Runnable() { 50 | public void run() { 51 | new TextFieldEvent(); 52 | } 53 | }); 54 | } 55 | 56 | } 57 | -------------------------------------------------------------------------------- /src/main/java/JOptionPaneTutorial.java: -------------------------------------------------------------------------------- 1 | import javax.swing.*; 2 | import java.awt.event.ActionEvent; 3 | import java.awt.event.ActionListener; 4 | 5 | public class JOptionPaneTutorial { 6 | JFrame frame = new JFrame("JOptionPane demo"); 7 | JPanel panel = new JPanel(); 8 | JButton button = new JButton("Call dialog"); 9 | JTextArea textarea = new JTextArea(10, 10); 10 | 11 | public JOptionPaneTutorial() { 12 | button.addActionListener(new ActionListener() { 13 | @Override 14 | public void actionPerformed(ActionEvent arg0) { 15 | // JOptionPane.showMessageDialog(null, new 16 | // JScrollPane(textarea), 17 | // "Message dialog", JOptionPane.PLAIN_MESSAGE); 18 | 19 | String title = JOptionPane.showInputDialog(null, 20 | "Enter title for JFrame:"); 21 | frame.setTitle(title); 22 | 23 | // int result = JOptionPane.showConfirmDialog(null, 24 | // "Are you sure you want to close a JFrame", 25 | // "Select an option", JOptionPane.OK_CANCEL_OPTION); 26 | // 27 | // if (result == JOptionPane.OK_OPTION) { 28 | // frame.dispose(); 29 | // } 30 | } 31 | }); 32 | panel.add(button); 33 | frame.add(panel); 34 | frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE); 35 | // frame.setBounds(100, 100, 790, 400); 36 | frame.pack(); 37 | frame.setResizable(false); 38 | frame.setVisible(true); 39 | } 40 | 41 | public static void main(String[] args) { 42 | SwingUtilities.invokeLater(new Runnable() { 43 | @Override 44 | public void run() { 45 | new JOptionPaneTutorial(); 46 | } 47 | }); 48 | } 49 | 50 | } 51 | -------------------------------------------------------------------------------- /src/main/java/CardLayoutTutorial.java: -------------------------------------------------------------------------------- 1 | import javax.swing.*; 2 | import java.awt.*; 3 | import java.awt.event.ActionEvent; 4 | import java.awt.event.ActionListener; 5 | 6 | public class CardLayoutTutorial { 7 | JFrame frame = new JFrame("CardLayout demo"); 8 | JPanel panelCont = new JPanel(); 9 | JPanel panelFirst = new JPanel(); 10 | JPanel panelSecond = new JPanel(); 11 | JButton buttonOne = new JButton("Switch to second panel/workspace"); 12 | JButton buttonSecond = new JButton("Switch to first panel/workspace"); 13 | CardLayout cl = new CardLayout(); 14 | 15 | public CardLayoutTutorial() { 16 | panelCont.setLayout(cl); 17 | 18 | panelFirst.add(buttonOne); 19 | panelSecond.add(buttonSecond); 20 | panelFirst.setBackground(Color.BLUE); 21 | panelSecond.setBackground(Color.GREEN); 22 | 23 | panelCont.add(panelFirst, "1"); 24 | panelCont.add(panelSecond, "2"); 25 | cl.show(panelCont, "1"); 26 | 27 | buttonOne.addActionListener(new ActionListener() { 28 | @Override 29 | public void actionPerformed(ActionEvent arg0) { 30 | cl.show(panelCont, "2"); 31 | } 32 | }); 33 | 34 | buttonSecond.addActionListener(new ActionListener() { 35 | @Override 36 | public void actionPerformed(ActionEvent arg0) { 37 | cl.show(panelCont, "1"); 38 | } 39 | }); 40 | 41 | frame.add(panelCont); 42 | frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE); 43 | frame.pack(); 44 | frame.setVisible(true); 45 | 46 | } 47 | 48 | public static void main(String[] args) { 49 | SwingUtilities.invokeLater(new Runnable() { 50 | @Override 51 | public void run() { 52 | new CardLayoutTutorial(); 53 | } 54 | }); 55 | } 56 | 57 | } 58 | -------------------------------------------------------------------------------- /src/main/java/LoadingScreenDemo.java: -------------------------------------------------------------------------------- 1 | import javax.swing.*; 2 | import java.awt.*; 3 | import java.awt.event.ActionEvent; 4 | import java.awt.event.ActionListener; 5 | 6 | public class LoadingScreenDemo extends JWindow { 7 | private static final long serialVersionUID = 1L; 8 | 9 | JPanel panel = new JPanel(); 10 | JProgressBar progressBar = new JProgressBar(); 11 | 12 | public LoadingScreenDemo() { 13 | panel.setBackground(Color.YELLOW); 14 | panel.setLayout(new BorderLayout()); 15 | panel.add(progressBar, BorderLayout.SOUTH); 16 | panel.setFocusable(true); 17 | progressBar.setStringPainted(true); 18 | add(panel); 19 | 20 | Timer timer = new Timer(100, new ActionListener() { 21 | int i = 0; 22 | 23 | public void actionPerformed(ActionEvent evt) { 24 | progressBar.setValue(i); 25 | i++; 26 | panel.repaint(); 27 | if (i == 100) { 28 | ((Timer) evt.getSource()).stop(); 29 | dispose(); 30 | } 31 | } 32 | }); 33 | timer.start(); 34 | } 35 | 36 | public static void main(String[] args) { 37 | try { 38 | UIManager.setLookAndFeel("javax.swing.plaf.nimbus.NimbusLookAndFeel"); 39 | } catch (ClassNotFoundException e) { 40 | e.printStackTrace(); 41 | } catch (InstantiationException e) { 42 | e.printStackTrace(); 43 | } catch (IllegalAccessException e) { 44 | e.printStackTrace(); 45 | } catch (UnsupportedLookAndFeelException e) { 46 | e.printStackTrace(); 47 | } 48 | 49 | SwingUtilities.invokeLater(new Runnable() { 50 | public void run() { 51 | LoadingScreenDemo w = new LoadingScreenDemo(); 52 | w.setSize(640, 480); 53 | w.setLocationRelativeTo(null); 54 | w.setVisible(true); 55 | } 56 | }); 57 | 58 | } 59 | } 60 | -------------------------------------------------------------------------------- /src/main/java/JRadioButtonTutorial.java: -------------------------------------------------------------------------------- 1 | import java.awt.*; 2 | import javax.swing.*; 3 | import java.awt.event.ActionEvent; 4 | import java.awt.event.ActionListener; 5 | 6 | 7 | public class JRadioButtonTutorial { 8 | JFrame frame = new JFrame(); 9 | JPanel panel = new JPanel(); 10 | JRadioButton rdbGreen = new JRadioButton("Green"); 11 | JRadioButton rdbRed = new JRadioButton("Red"); 12 | ButtonGroup buttonGroup = new ButtonGroup(); 13 | 14 | public JRadioButtonTutorial() { 15 | rdbGreen.addActionListener(new ActionListener() { 16 | @Override 17 | public void actionPerformed(ActionEvent e) { 18 | JRadioButton source = (JRadioButton)e.getSource(); 19 | if (source.isSelected()) { 20 | panel.setBackground(Color.GREEN); 21 | } 22 | if (!source.isSelected()) { 23 | panel.setBackground(Color.WHITE); 24 | } 25 | panel.repaint(); 26 | } 27 | }); 28 | rdbRed.addActionListener(new ActionListener() { 29 | @Override 30 | public void actionPerformed(ActionEvent e) { 31 | JRadioButton source = (JRadioButton)e.getSource(); 32 | if (source.isSelected()) { 33 | panel.setBackground(Color.RED); 34 | } 35 | if (!source.isSelected()) { 36 | panel.setBackground(Color.WHITE); 37 | } 38 | panel.repaint(); 39 | } 40 | }); 41 | buttonGroup.add(rdbGreen); 42 | buttonGroup.add(rdbRed); 43 | panel.add(rdbGreen); 44 | panel.add(rdbRed); 45 | frame.add(panel); 46 | frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE); 47 | frame.pack(); 48 | frame.setVisible(true); 49 | } 50 | 51 | public static void main(String [] args){ 52 | SwingUtilities.invokeLater(new Runnable() { 53 | @Override 54 | public void run() { 55 | new JRadioButtonTutorial(); 56 | } 57 | }); 58 | } 59 | } 60 | -------------------------------------------------------------------------------- /src/main/java/JPasswordFieldTutorial.java: -------------------------------------------------------------------------------- 1 | import javax.swing.*; 2 | import java.awt.event.ActionEvent; 3 | import java.awt.event.ActionListener; 4 | import java.util.Arrays; 5 | 6 | /** 7 | * Created with IntelliJ IDEA. 8 | * User: Branislav 9 | * Date: 5/19/13 10 | * Time: 3:56 PM 11 | */ 12 | public class JPasswordFieldTutorial { 13 | JFrame frame = new JFrame(); 14 | JPanel panel = new JPanel(); 15 | JPasswordField passwordField = new JPasswordField(20); 16 | JButton button = new JButton("Perform check"); 17 | 18 | public JPasswordFieldTutorial() { 19 | button.addActionListener(new ActionListener() { 20 | @Override 21 | public void actionPerformed(ActionEvent e) { 22 | char [] input = passwordField.getPassword(); 23 | if(checkIfCorrect(input)) { 24 | JOptionPane.showMessageDialog(null,"Password is correct!"); 25 | } else { 26 | JOptionPane.showMessageDialog(null,"Password is incorrect!"); 27 | } 28 | } 29 | }); 30 | passwordField.setEchoChar('*'); 31 | panel.add(passwordField); 32 | panel.add(button); 33 | 34 | frame.add(panel); 35 | frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE); 36 | frame.pack(); 37 | frame.setVisible(true); 38 | } 39 | 40 | private boolean checkIfCorrect(char [] input) { 41 | boolean isCorrect = false; 42 | char [] correctPass = {'1','2','3'}; 43 | 44 | if(input.length != correctPass.length) { 45 | isCorrect = false; 46 | } else { 47 | isCorrect = Arrays.equals(input, correctPass); 48 | } 49 | Arrays.fill(correctPass,'0'); 50 | return isCorrect; 51 | } 52 | 53 | public static void main(String [] args) { 54 | SwingUtilities.invokeLater(new Runnable() { 55 | @Override 56 | public void run() { 57 | new JPasswordFieldTutorial(); 58 | } 59 | }); 60 | } 61 | } 62 | -------------------------------------------------------------------------------- /src/main/java/JComboBoxTutorial.java: -------------------------------------------------------------------------------- 1 | import javax.swing.*; 2 | import java.awt.event.ActionEvent; 3 | import java.awt.event.ActionListener; 4 | import java.awt.event.ItemEvent; 5 | import java.awt.event.ItemListener; 6 | 7 | 8 | public class JComboBoxTutorial { 9 | JFrame frame = new JFrame(); 10 | JPanel panel = new JPanel(); 11 | JComboBox comboBox = new JComboBox(); 12 | DefaultComboBoxModel model = new DefaultComboBoxModel(); 13 | JTextField txtAdd = new JTextField(15); 14 | JButton btnAdd = new JButton("Add"); 15 | JButton btnRemove = new JButton("Remove"); 16 | String selectedValue; 17 | 18 | public JComboBoxTutorial() { 19 | comboBox.setModel(model); 20 | comboBox.addItemListener(new ItemListener() { 21 | @Override 22 | public void itemStateChanged(ItemEvent e) { 23 | if(e.getStateChange() == ItemEvent.SELECTED) { 24 | selectedValue = model.getSelectedItem().toString(); 25 | } 26 | } 27 | }); 28 | 29 | btnRemove.addActionListener(new ActionListener() { 30 | @Override 31 | public void actionPerformed(ActionEvent e) { 32 | model.removeElement(selectedValue); 33 | } 34 | }); 35 | 36 | btnAdd.addActionListener(new ActionListener() { 37 | @Override 38 | public void actionPerformed(ActionEvent e) { 39 | model.addElement(txtAdd.getText()); 40 | } 41 | }); 42 | 43 | panel.add(txtAdd); 44 | panel.add(btnAdd); 45 | panel.add(comboBox); 46 | panel.add(btnRemove); 47 | frame.add(panel); 48 | frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE); 49 | frame.pack(); 50 | frame.setVisible(true); 51 | 52 | } 53 | 54 | public static void main(String[] args) { 55 | SwingUtilities.invokeLater(new Runnable() { 56 | @Override 57 | public void run() { 58 | new JComboBoxTutorial(); 59 | } 60 | }); 61 | } 62 | } 63 | -------------------------------------------------------------------------------- /src/main/java/JListTutorial.java: -------------------------------------------------------------------------------- 1 | import javax.swing.*; 2 | import java.math.BigDecimal; 3 | 4 | public class JListTutorial { 5 | 6 | JFrame frame = new JFrame("Storage"); 7 | JList list = new JList<>(); 8 | DefaultListModel model = new DefaultListModel<>(); 9 | 10 | JLabel label = new JLabel(); 11 | JPanel panel = new JPanel(); 12 | JSplitPane splitPane = new JSplitPane(); 13 | 14 | public JListTutorial() { 15 | 16 | list.setModel(model); 17 | 18 | model.addElement(new Product("Oranges", new BigDecimal("2.00"), "These are fresh oranges")); 19 | model.addElement(new Product("Apples", new BigDecimal("1.50"), "These are fresh apples")); 20 | 21 | list.getSelectionModel().addListSelectionListener(e -> { 22 | Product p = list.getSelectedValue(); 23 | label.setText("Name: " + p.getName() + " ::: " + p.getPrice().toPlainString() + " ::: " + p.getDesc()); 24 | }); 25 | 26 | splitPane.setLeftComponent(new JScrollPane(list)); 27 | panel.add(label); 28 | splitPane.setRightComponent(panel); 29 | 30 | frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE); 31 | frame.add(splitPane); 32 | frame.pack(); 33 | frame.setLocationRelativeTo(null); 34 | frame.setVisible(true); 35 | } 36 | 37 | public static void main(String[] args) { 38 | SwingUtilities.invokeLater(JListTutorial::new); 39 | } 40 | 41 | private class Product { 42 | String name; 43 | BigDecimal price; 44 | String desc; 45 | 46 | public Product(String name, BigDecimal price, String desc) { 47 | this.name = name; 48 | this.price = price; 49 | this.desc = desc; 50 | } 51 | 52 | public String getName() { 53 | return name; 54 | } 55 | 56 | public void setName(String name) { 57 | this.name = name; 58 | } 59 | 60 | public BigDecimal getPrice() { 61 | return price; 62 | } 63 | 64 | public void setPrice(BigDecimal price) { 65 | this.price = price; 66 | } 67 | 68 | public String getDesc() { 69 | return desc; 70 | } 71 | 72 | public void setDesc(String desc) { 73 | this.desc = desc; 74 | } 75 | 76 | @Override 77 | public String toString() { 78 | return name; 79 | } 80 | } 81 | 82 | } 83 | -------------------------------------------------------------------------------- /src/main/java/JNumberTextField.java: -------------------------------------------------------------------------------- 1 | import javax.swing.JTextField; 2 | import javax.swing.text.AttributeSet; 3 | import javax.swing.text.BadLocationException; 4 | import javax.swing.text.Document; 5 | import javax.swing.text.PlainDocument; 6 | 7 | public class JNumberTextField extends JTextField { 8 | private static final long serialVersionUID = 1L; 9 | private static final char DOT = '.'; 10 | private static final char NEGATIVE = '-'; 11 | private static final String BLANK = ""; 12 | private static final int DEF_PRECISION = 2; 13 | 14 | public static final int NUMERIC = 2; 15 | public static final int DECIMAL = 3; 16 | 17 | public static final String FM_NUMERIC = "0123456789"; 18 | public static final String FM_DECIMAL = FM_NUMERIC + DOT; 19 | 20 | private int maxLength = 0; 21 | private int format = NUMERIC; 22 | private String negativeChars = BLANK; 23 | private String allowedChars = null; 24 | private boolean allowNegative = false; 25 | private int precision = 0; 26 | 27 | protected PlainDocument numberFieldFilter; 28 | 29 | public JNumberTextField() { 30 | this(10, NUMERIC); 31 | } 32 | 33 | public JNumberTextField(int maxLen) { 34 | this(maxLen, NUMERIC); 35 | } 36 | 37 | public JNumberTextField(int maxLen, int format) { 38 | setAllowNegative(true); 39 | setMaxLength(maxLen); 40 | setFormat(format); 41 | 42 | numberFieldFilter = new JNumberFieldFilter(); 43 | super.setDocument(numberFieldFilter); 44 | } 45 | 46 | public void setMaxLength(int maxLen) { 47 | if (maxLen > 0) 48 | maxLength = maxLen; 49 | else 50 | maxLength = 0; 51 | } 52 | 53 | public int getMaxLength() { 54 | return maxLength; 55 | } 56 | 57 | public void setPrecision(int precision) { 58 | if (format == NUMERIC) 59 | return; 60 | 61 | if (precision >= 0) 62 | this.precision = precision; 63 | else 64 | this.precision = DEF_PRECISION; 65 | } 66 | 67 | public int getPrecision() { 68 | return precision; 69 | } 70 | 71 | public Number getNumber() { 72 | Number number = null; 73 | 74 | if (format == NUMERIC) 75 | number = new Integer(getText()); 76 | else 77 | number = new Double(getText()); 78 | 79 | return number; 80 | } 81 | 82 | public void setNumber(Number value) { 83 | setText(String.valueOf(value)); 84 | } 85 | 86 | public int getInt() { 87 | return Integer.parseInt(getText()); 88 | } 89 | 90 | public void setInt(int value) { 91 | setText(String.valueOf(value)); 92 | } 93 | 94 | public float getFloat() { 95 | return (new Float(getText())).floatValue(); 96 | } 97 | 98 | public void setFloat(float value) { 99 | setText(String.valueOf(value)); 100 | } 101 | 102 | public double getDouble() { 103 | return (new Double(getText())).doubleValue(); 104 | } 105 | 106 | public void setDouble(double value) { 107 | setText(String.valueOf(value)); 108 | } 109 | 110 | public int getFormat() { 111 | return format; 112 | } 113 | 114 | public void setFormat(int format) { 115 | switch (format) { 116 | case NUMERIC: 117 | default: 118 | this.format = NUMERIC; 119 | this.precision = 0; 120 | this.allowedChars = FM_NUMERIC; 121 | break; 122 | 123 | case DECIMAL: 124 | this.format = DECIMAL; 125 | this.precision = DEF_PRECISION; 126 | this.allowedChars = FM_DECIMAL; 127 | break; 128 | } 129 | } 130 | 131 | public void setAllowNegative(boolean value) { 132 | allowNegative = value; 133 | 134 | if (value) 135 | negativeChars = "" + NEGATIVE; 136 | else 137 | negativeChars = BLANK; 138 | } 139 | 140 | public boolean isAllowNegative() { 141 | return allowNegative; 142 | } 143 | 144 | public void setDocument(Document document) { 145 | } 146 | 147 | class JNumberFieldFilter extends PlainDocument { 148 | private static final long serialVersionUID = 1L; 149 | 150 | public JNumberFieldFilter() { 151 | super(); 152 | } 153 | 154 | public void insertString(int offset, String str, AttributeSet attr) 155 | throws BadLocationException { 156 | String text = getText(0, offset) + str 157 | + getText(offset, (getLength() - offset)); 158 | 159 | if (str == null || text == null) 160 | return; 161 | 162 | for (int i = 0; i < str.length(); i++) { 163 | if ((allowedChars + negativeChars).indexOf(str.charAt(i)) == -1) 164 | return; 165 | } 166 | 167 | int precisionLength = 0, dotLength = 0, minusLength = 0; 168 | int textLength = text.length(); 169 | 170 | try { 171 | if (format == NUMERIC) { 172 | if (!((text.equals(negativeChars)) && (text.length() == 1))) 173 | new Long(text); 174 | } else if (format == DECIMAL) { 175 | if (!((text.equals(negativeChars)) && (text.length() == 1))) 176 | new Double(text); 177 | 178 | int dotIndex = text.indexOf(DOT); 179 | if (dotIndex != -1) { 180 | dotLength = 1; 181 | precisionLength = textLength - dotIndex - dotLength; 182 | 183 | if (precisionLength > precision) 184 | return; 185 | } 186 | } 187 | } catch (Exception ex) { 188 | return; 189 | } 190 | 191 | if (text.startsWith("" + NEGATIVE)) { 192 | if (!allowNegative) 193 | return; 194 | else 195 | minusLength = 1; 196 | } 197 | 198 | if (maxLength < (textLength - dotLength - precisionLength - minusLength)) 199 | return; 200 | 201 | super.insertString(offset, str, attr); 202 | } 203 | } 204 | } --------------------------------------------------------------------------------