🎨 Customizing GUI in Java: Colors, Fonts & Borders in AWT and Swing (Enhanced Guide)


🌟 Introduction

In GUI development, functionality alone is not enough β€” appearance matters too! 🎯

πŸ‘‰ A well-designed interface improves:

  • User experience 😊
  • Readability πŸ“–
  • Professional look πŸ’»

In this blog, we will learn how to customize:

  • 🎨 Colors (background & text)
  • πŸ”€ Fonts
  • 🧱 Borders (including border color)

πŸ‘‰ For both AWT and Swing


🧱 PART 1: Customization in AWT


🎨 1. Colors in AWT


βœ… Complete Runnable Program

import java.awt.*;

public class AWTColorDemo {
    public static void main(String[] args) {

        Frame frame = new Frame("AWT Colors");

        Button btn = new Button("Click Me");

        btn.setBackground(Color.BLUE);   // background
        btn.setForeground(Color.WHITE); // text color

        frame.setLayout(new FlowLayout());
        frame.add(btn);

        frame.setSize(300, 200);
        frame.setVisible(true);
    }
}

πŸ”€ 2. Font in AWT


βœ… Program

import java.awt.*;

public class AWTFontDemo {
    public static void main(String[] args) {

        Frame frame = new Frame("AWT Font");

        Label label = new Label("Styled Text");

        label.setFont(new Font("Arial", Font.BOLD, 18));

        frame.setLayout(new FlowLayout());
        frame.add(label);

        frame.setSize(300, 200);
        frame.setVisible(true);
    }
}

🧱 3. Border in AWT (Including Border Color)

πŸ‘‰ AWT does NOT have built-in border support like Swing ❌
πŸ‘‰ But we can simulate borders using Panel + background color trick βœ…


βœ… Complete Runnable Program

import java.awt.*;

public class AWTBorderDemo {
    public static void main(String[] args) {

        Frame frame = new Frame("AWT Border");

        frame.setLayout(new FlowLayout());

        // Outer panel (acts as border)
        Panel outer = new Panel();
        outer.setBackground(Color.RED); // border color πŸ”₯
        outer.setLayout(new FlowLayout());

        // Inner panel (actual content)
        Panel inner = new Panel();
        inner.setBackground(Color.WHITE);

        Label label = new Label("AWT Border Example");

        inner.add(label);
        outer.add(inner);

        frame.add(outer);

        frame.setSize(300, 200);
        frame.setVisible(true);
    }
}

πŸ” Explanation

  • Outer panel β†’ acts as border
  • Background color β†’ border color 🎨
  • Inner panel β†’ content

πŸ‘‰ This is a workaround, not a true border


⚠️ AWT Limitation

πŸ‘‰ No direct border API ❌
πŸ‘‰ Styling is limited


🧱 PART 2: Customization in Swing (Powerful πŸ”₯)


🎨 1. Colors in Swing


βœ… Complete Runnable Program

import javax.swing.*;
import java.awt.*;

public class SwingColorDemo {
    public static void main(String[] args) {

        JFrame frame = new JFrame("Swing Colors");

        JButton btn = new JButton("Click Me");

        btn.setBackground(Color.BLUE);
        btn.setForeground(Color.WHITE);

        frame.setLayout(new FlowLayout());
        frame.add(btn);

        frame.setSize(300, 200);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setVisible(true);
    }
}

🧱 2. Borders in Swing (Including Border Color)


βœ… Complete Runnable Program

import javax.swing.*;
import javax.swing.border.*;
import java.awt.*;

public class SwingBorderDemo {
    public static void main(String[] args) {

        JFrame frame = new JFrame("Swing Border");

        JButton btn = new JButton("Styled Button");

        // Border with color
        Border border = BorderFactory.createLineBorder(Color.RED, 3);

        btn.setBorder(border);

        frame.setLayout(new FlowLayout());
        frame.add(btn);

        frame.setSize(300, 200);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setVisible(true);
    }
}

πŸ” Explanation

  • createLineBorder(Color, thickness)
  • Color defines border color 🎨

✨ 3. Advanced Border Types


βœ… Program

import javax.swing.*;
import javax.swing.border.*;
import java.awt.*;

public class AdvancedBorderDemo {
    public static void main(String[] args) {

        JFrame frame = new JFrame("Advanced Borders");

        JButton btn = new JButton("Fancy Button");

        Border border = BorderFactory.createTitledBorder(
            BorderFactory.createLineBorder(Color.BLUE, 2),
            "My Border"
        );

        btn.setBorder(border);

        frame.setLayout(new FlowLayout());
        frame.add(btn);

        frame.setSize(300, 200);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setVisible(true);
    }
}

πŸ”€ 4. Fonts in Swing


βœ… Program

import javax.swing.*;
import java.awt.*;

public class SwingFontDemo {
    public static void main(String[] args) {

        JFrame frame = new JFrame("Swing Font");

        JLabel label = new JLabel("Styled Text");

        label.setFont(new Font("Verdana", Font.BOLD, 20));

        frame.setLayout(new FlowLayout());
        frame.add(label);

        frame.setSize(300, 200);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setVisible(true);
    }
}

βš–οΈ AWT vs Swing (Styling Perspective)


FeatureAWTSwing
ColorsBasicAdvanced
FontsLimitedFlexible
BordersManual workaround ❌Direct support βœ…
Border ColorTrick-basedDirect property 🎨

⚠️ Common Mistakes


❌ Expecting AWT border like Swing

πŸ‘‰ AWT does not support it directly


❌ Forgetting BorderFactory import

import javax.swing.border.*;

❌ Button color not visible

πŸ‘‰ Depends on system Look & Feel


❌ Mixing AWT & Swing

πŸ‘‰ Causes UI inconsistency


🀯 Common Confusions


πŸ€” Why AWT borders are difficult?

πŸ‘‰ Because AWT relies on OS components


πŸ€” Why Swing borders are easy?

πŸ‘‰ Fully Java-controlled rendering


πŸ’Ό Interview Questions


❓ How to set border color in Swing?

πŸ‘‰ BorderFactory.createLineBorder(Color.RED)


❓ Does AWT support borders?

πŸ‘‰ Not directly β€” requires workaround


❓ Which is better for styling?

πŸ‘‰ Swing


🎯 Best Practices


βœ”οΈ Use Swing for modern UI
βœ”οΈ Use consistent color scheme
βœ”οΈ Keep UI clean
βœ”οΈ Avoid over-styling


🏁 Conclusion

Now you can make your GUI:

πŸ‘‰ Functional βœ”οΈ
πŸ‘‰ Interactive βœ”οΈ
πŸ‘‰ Beautiful 🎨✨


πŸ’‘ Final Thought

πŸ‘‰ Good UI = Good impression πŸ’―


πŸ”₯ One-Line Summary

πŸ‘‰ β€œSwing provides direct and powerful styling features, while AWT has limited customization options.”


πŸ’» Happy Coding! πŸ˜„

Next step: Event Delegation Model (deep dive) πŸ”₯

Comments

No comments yet. Why don’t you start the discussion?

Leave a Reply

Your email address will not be published. Required fields are marked *