Search code examples
javaswingjtextfield

why are bold + italic not working?


I am trying to display the entered text in both italic and bold form at the same time, but for some reason that condition is just not working. The text goes back to the first condition every time I check both the boxes. The code in the book is same as mine and also the codes on various websites seem to be the same too. I can't find the problem.
I tried ^ and + instead of | between the 2 fonts, still not working. This is within the ItemListener subclass:

Font f=null; // bold, italic - name of the checkboxes
  public void itemStateChanged(ItemEvent e) {
    if (italic.isSelected() )//1st condition
    f = new Font("Serif", Font.ITALIC, 30);
  else if (bold.isSelected() )
    f = new Font("Serif", Font.BOLD, 30);
  else if ( bold.isSelected() && italic.isSelected() )
    f = new Font("Serif", Font.BOLD | Font.ITALIC, 30);
  else 
    f = new Font("Serif", Font.PLAIN, 30);

  tf.setFont(f);// tf = object of JTextField
  }

Solution

  • Take a look at your logic

    if (italic.isSelected() )//1st condition
        f = new Font("Serif", Font.ITALIC, 30);
    else if (bold.isSelected() )
        f = new Font("Serif", Font.BOLD, 30);
    else if ( bold.isSelected() && italic.isSelected() )
        f = new Font("Serif", Font.BOLD | Font.ITALIC, 30);
    else 
        f = new Font("Serif", Font.PLAIN, 30);
    

    if italic.isSelected() then make the font italic, else if bold.isSelected(), make it bold, it's impossible for else if ( bold.isSelected() && italic.isSelected() ) to ever be evaluated

    Font Change

    import java.awt.EventQueue;
    import java.awt.Font;
    import java.awt.GridBagConstraints;
    import java.awt.GridBagLayout;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import javax.swing.JFrame;
    import javax.swing.JPanel;
    import javax.swing.JTextField;
    import javax.swing.JToggleButton;
    import javax.swing.UIManager;
    import javax.swing.UnsupportedLookAndFeelException;
    
    public class FontTest {
    
        public static void main(String[] args) {
            new FontTest();
        }
    
        public FontTest() {
            EventQueue.invokeLater(new Runnable() {
                @Override
                public void run() {
                    try {
                        UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                    } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
                        ex.printStackTrace();
                    }
    
                    JFrame frame = new JFrame("Testing");
                    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                    frame.add(new TestPane());
                    frame.pack();
                    frame.setLocationRelativeTo(null);
                    frame.setVisible(true);
                }
            });
        }
    
        public class TestPane extends JPanel {
    
            public TestPane() {
                setLayout(new GridBagLayout());
                GridBagConstraints gbc = new GridBagConstraints();
                gbc.gridwidth = GridBagConstraints.REMAINDER;
    
                JTextField field = new JTextField("Bunch a munchy carrots");
                add(field, gbc);
    
                JToggleButton bold = new JToggleButton("Bold");
                JToggleButton italic = new JToggleButton("Italic");
    
                ActionListener listener = new ActionListener() {
    
                    @Override
                    public void actionPerformed(ActionEvent e) {
                        Font font = field.getFont();
                        if (bold.isSelected() && italic.isSelected()) {
                            font = font.deriveFont(Font.BOLD | Font.ITALIC);
                        } else if (bold.isSelected()) {
                            font = font.deriveFont(Font.BOLD);
                        } else if (italic.isSelected()) {
                            font = font.deriveFont(Font.ITALIC);
                        } else {
                            font = font.deriveFont(Font.PLAIN);
                        }
                        field.setFont(font);
                    }
                };
    
                bold.addActionListener(listener);
                italic.addActionListener(listener);
    
                add(bold);
                add(italic);
            }
    
        }
    
    }