James Gosling: idealism, the Internet and Java, Pt I

Monday, 11 January 2021

Java API support for Exception Handling

First of all, please read below blog post to understand basics of Exception Handling in java.

Exception handling and java keywords


To continue from there, Basically Java default API has hierarchy of  java classes to support different uses cases of exceptions. 

The parent class of all Exceptions and Errors is Throwable. Please check below pictorial representation of  API Hierarchy 

Exception:

The class Exception and any subclasses that are also not subclasses of RuntimeException are checked exceptions.

And all subclasses of RuntimeException and RuntimeException class itself are Unchecked exception. Rest all are Checked exceptions

Examples of Unchecked Exceptions: 

         ArrayIndexOutOfBoundException

         NullPointerException

Error:

An Error is a subclass of Throwable that indicates serious problems that a reasonable application should not try to catch. Most such errors are abnormal conditions.

Examples: The ThreadDeath error


Wednesday, 6 January 2021

Java - Swing custom components - Rounded JButton

Unlike AWT, Java Swing tool kit provides feasibilty to create custom components and containers.

To understand Java GUI toolkits more clearly, please read my other articles on Swing and AWT. 

In this article I focus more on building custom components.

Let us see how to create simple Rounded JButton. The below program demonstrates How rounded JButton can be created and added to a JFrame.

 

Program:


import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.GridLayout;
import java.awt.Shape;
import java.awt.geom.Ellipse2D;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JTextField;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

public class RoundedJButton extends JButton {

/**
*/
private static final long serialVersionUID = 1L;

public RoundedJButton(String title) {
super(title);
Dimension size = getPreferredSize();
size.width = size.height = Math.max(size.width, size.height);
setPreferredSize(size);

setContentAreaFilled(false);
}

@Override
protected void paintComponent(Graphics g) {
if (getModel().isArmed()) {
g.setColor(Color.lightGray);
} else {
g.setColor(getBackground());
}
g.fillOval(0, 0, getSize().width - 1, getSize().height - 1);

super.paintComponent(g);
}

@Override
protected void paintBorder(Graphics g) {
g.setColor(getForeground());
g.drawOval(0, 0, getSize().width - 1, getSize().height - 1);
}

Shape shape;

@Override
public boolean contains(int x, int y) {
if (shape == null || !shape.getBounds().equals(getBounds())) {
shape = new Ellipse2D.Float(0, 0, getWidth(), getHeight());
}
return shape.contains(x, y);
}

public static void main(String[] args) throws ClassNotFoundException, InstantiationException, IllegalAccessException, UnsupportedLookAndFeelException {

JFrame frame = new JFrame();
frame.setTitle("It's all about Java...");
frame.setSize(150, 150);
GridLayout layout = new GridLayout();
layout.setRows(1);
layout.setHgap(10);
layout.setVgap(10);
frame.setLayout(layout);

JTextField field1 = new JTextField();
field1.setText("This is a textfield!");
frame.getContentPane().add(field1);

RoundedJButton button = new RoundedJButton("Browse");
button.setBackground(Color.gray);
button.setForeground(Color.GREEN);
frame.getContentPane().add(button);

UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());

SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
frame.pack();
frame.setVisible(true);
}
});
}
}

There are two parts in the program. First part creates RoundedJButton component by extending JButton. and second part creates JFrame and appends two components. 1. RoundedJButton and 2. JTextField

First Part:


RoundedJButton overrides paintComponent(Graphics g) and paintBorder(Graphics g) to get the circled shape to the Button. 

Second Part:

Second part contains main method which creates JFrame object and adds 2 components to JFrame dialog.

Also applies System look and feel by using following

UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());

I will explain clearly about pluggable look and feels also known as PLAF in future articles.

That's all for now!

Happy coding... :) 

Popular posts

Demonstration of Java NullPointerException

NullPointerException causes and reasons Since Java is object oriented programming language, every thing is considered to be an object. and t...