SHARE
TWEET

JTextField Font Change

a guest Feb 5th, 2016 154 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. package weekTwo;
  2.  
  3. import javax.swing.*;
  4. import java.awt.*;
  5. import java.awt.GridBagConstraints;
  6. import java.awt.event.ActionEvent;
  7. import java.awt.event.ActionListener;
  8.  
  9. public class TaskTwo {
  10.    
  11.     public static void main(String[] args) {
  12.         JFrame window = new JFrame("Font Chooser");
  13.         window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  14.         window.setSize(500, 100);
  15.  
  16.         Container pane = window.getContentPane();
  17.         pane.setLayout(new GridBagLayout());
  18.  
  19.         GridBagConstraints gbc = new GridBagConstraints();
  20.  
  21.         JTextField text = new JTextField();
  22.         gbc.gridx = 2;
  23.         gbc.gridy = 2;
  24.         gbc.weightx = 1;
  25.         gbc.weighty = 1;
  26.         gbc.insets = new Insets(0, 0, 5, 5); //padding constraints
  27.         gbc.fill = GridBagConstraints.BOTH;   //text field fills cell
  28.         pane.add(text, gbc);
  29.        
  30.         FontSetter fontSetter = new FontSetter(text);
  31.  
  32.         JCheckBox bold = new JCheckBox("Bold");
  33.         gbc.gridx = 0; //sets x position to 0
  34.         gbc.gridy = 1;
  35.         bold.addActionListener(new Bold(fontSetter, text));
  36.         pane.add(bold, gbc);
  37.  
  38.         JCheckBox italic = new JCheckBox("Italic");
  39.         gbc.gridx = 0;
  40.         gbc.gridy = 3;
  41.         italic.addActionListener(new Italic(fontSetter, text));
  42.         pane.add(italic, gbc);
  43.  
  44.         JRadioButton times = new JRadioButton("Times", true);
  45.         gbc.gridx = 1;
  46.         gbc.gridy = 0;
  47.         times.addActionListener(new Times(fontSetter, text));
  48.         pane.add(times, gbc);
  49.  
  50.         JRadioButton helvetica = new JRadioButton("Helvetica", false);
  51.         gbc.gridx = 1;
  52.         gbc.gridy = 2;
  53.         helvetica.addActionListener(new Helvetica(fontSetter, text));
  54.         pane.add(helvetica, gbc);
  55.  
  56.         JRadioButton courier = new JRadioButton("Courier", false);
  57.         gbc.gridx = 1;
  58.         gbc.gridy = 4;
  59.         courier.addActionListener(new Courier(fontSetter, text));
  60.         pane.add(courier, gbc);
  61.  
  62.         ButtonGroup fonts = new ButtonGroup();  //now only one of these buttons can be selected at once
  63.         fonts.add(times);
  64.         fonts.add(helvetica);
  65.         fonts.add(courier);
  66.  
  67.         JButton ok = new JButton("OK");
  68.         gbc.gridx = 3;
  69.         gbc.gridy = 2;
  70.         //ok.addActionListener();
  71.         pane.add(ok, gbc);
  72.  
  73.         window.setVisible(true);
  74.     }
  75. }
  76.  
  77. class Bold implements ActionListener {
  78.  
  79.     private final FontSetter fontSetter;
  80.     private final JTextField textfield;
  81.  
  82.     Bold(FontSetter fontSetter, JTextField textfield) {
  83.         this.fontSetter = fontSetter;
  84.         this.textfield = textfield;
  85.     }
  86.  
  87.  
  88.     @Override
  89.     public void actionPerformed(ActionEvent e) {
  90.         JCheckBox ch = (JCheckBox) e.getSource();
  91.         fontSetter.setBold(ch.isSelected());
  92.     }
  93.  
  94. }
  95.  
  96. class Italic implements ActionListener {
  97.  
  98.     private final FontSetter fontSetter;
  99.     private final JTextField textfield;
  100.  
  101.     Italic(FontSetter fontSetter, JTextField textfield) {
  102.         this.fontSetter = fontSetter;
  103.         this.textfield = textfield;
  104.     }
  105.  
  106.     @Override
  107.     public void actionPerformed(ActionEvent e) {
  108.         JCheckBox ch = (JCheckBox) e.getSource();
  109.         fontSetter.setItalic(ch.isSelected());
  110.     }
  111.  
  112. }
  113.  
  114. class Times implements ActionListener {
  115.  
  116.     private final FontSetter fontSetter;
  117.     private final JTextField textfield;
  118.  
  119.     Times(FontSetter fontSetter, JTextField textfield) {
  120.         this.fontSetter = fontSetter;
  121.         this.textfield = textfield;
  122.     }
  123.  
  124.     @Override
  125.     public void actionPerformed(ActionEvent e) {
  126.         fontSetter.setFontName("Helvetica");
  127.         textfield.setText("Times");
  128.     }
  129.  
  130. }
  131.  
  132. class Helvetica implements ActionListener {
  133.  
  134.     private final FontSetter fontSetter;
  135.     private final JTextField textfield;
  136.  
  137.     Helvetica(FontSetter fontSetter, JTextField textfield) {
  138.         this.fontSetter = fontSetter;
  139.         this.textfield = textfield;
  140.     }
  141.  
  142.     @Override
  143.     public void actionPerformed(ActionEvent e) {
  144.         fontSetter.setFontName("Helvetica");
  145.         textfield.setText("Helvetica");
  146.     }
  147.  
  148. }
  149.  
  150. class Courier implements ActionListener {
  151.  
  152.     private final FontSetter fontSetter;
  153.     private final JTextField textfield;
  154.  
  155.     Courier(FontSetter fontSetter, JTextField textfield) {
  156.         this.fontSetter = fontSetter;
  157.         this.textfield = textfield;
  158.     }
  159.  
  160.     @Override
  161.     public void actionPerformed(ActionEvent e) {
  162.         fontSetter.setFontName("Courier");
  163.         textfield.setText("Courier");
  164.     }
  165.  
  166. }
  167.  
  168. class FontSetter {
  169.  
  170.     private final JTextField text;
  171.     private String fontName = "Times";
  172.     private boolean isBold = false;
  173.     private boolean isItalic = false;
  174.  
  175.     FontSetter(JTextField text) {
  176.         this.text = text;
  177.     }
  178.    
  179.     void setBold(boolean flag) {
  180.         isBold = flag;
  181.         applyFontChanges();
  182.     }
  183.  
  184.     void setItalic(boolean flag) {
  185.         isItalic = flag;
  186.         applyFontChanges();
  187.     }
  188.  
  189.     void setFontName(String fontName) {
  190.         this.fontName = fontName;
  191.         applyFontChanges();
  192.     }
  193.    
  194.     private void applyFontChanges(){
  195.         Font font;
  196.         int style;
  197.        
  198.         if(isBold && isItalic){
  199.             style = Font.BOLD + Font.ITALIC;
  200.         } else if(isBold){
  201.             style = Font.BOLD;
  202.         } else if(isItalic){
  203.             style = Font.ITALIC;
  204.         } else {
  205.             style = Font.PLAIN;
  206.         }
  207.        
  208.         font = new Font(fontName, style, 12);
  209.         text.setFont(font );
  210.     }
  211.    
  212. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Top