Advanced Java Question Bank Answers
Advanced Java Question Bank Answers
Advanced Java Question Bank Answers
// Append method
stringBuffer.append(", World!");
// Append a string
// Insert method
// Delete method
stringBuffer.delete(5, 10);
// Reverse method
stringBuffer.reverse();
// Replace method
stringBuffer.replace(0, 5, "Hola");
stringBuffer.setLength(5);
Output
Capacity: 21
Length: 12
// Append method
stringBuffer.append(", World!");
// Append a string
// Insert method
// Delete method
stringBuffer.delete(5, 10);
// Reverse method
stringBuffer.reverse();
// Replace method
stringBuffer.replace(0, 5, "Hola");
stringBuffer.setLength(5);
Output
Capacity: 21
Length: 12
// Character extraction
// Substring extraction
// String comparison
// String search
// String modification
// String trimming
Output
First character: H
Last character: !
Substring: World!
Index of comma: 5
paintComponent( )
paintBorder( )
paintChildren( ).
paintComponent The main painting method, which paints the background if the component is
opaque, and then performs any custom painting.
paintBorder Tells the component's border to paint, if it has one. It's not recommended to invoke or
override this method.
paintChildren Tells any components contained by this component to paint themselves. It's also not
recommended to invoke or override this method .
Swing automatically clips any output that will exceed the boundaries of a component, it is still
possible to paint into the border, which will then get overwritten when the border is drawn
A Paint Example
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.util.*;
Insets ins;
Random rand;
// Construct a panel.
PaintPanel() {
super.paintComponent(g);
ins = getInsets();
x = rand.nextInt(width-ins.left);
y = rand.nextInt(height-ins.bottom);
x2 = rand.nextInt(width-ins.left);
y2 = rand.nextInt(height-ins.bottom);
8. Discuss in detail
1.JLabel
2.ImageIcon
Ans: JLabel and ImageIcon :
JLabel is Swing’s easiest-to-use component. It creates a label and was introduced in the JLabel can
be used to display text and/or an icon.
JLabel(Icon icon)
JLabel(String str)
Here, str and icon are the text and icon used for the label. The align argument specifies the
horizontal alignment of the text and/or icon within the dimensions of the label. It must be one of the
following values: LEFT, RIGHT, CENTER, LEADING, or TRAILING. These constants are defined in the
SwingConstants interface, along with several others used by the Swing classes.
The icon and text associated with the label can be obtained by the following methods: Icon
getIcon( ) String getText( )
The icon and text associated with a label can be set by these methods: void setIcon(Icon icon) void
setText(String str)
//Example program
import java.awt.*;
import javax.swing.*;
public JLabelDemo() {
jfrm.setLayout(new FlowLayout());
jfrm.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
jfrm.setSize(260, 210);
// Create an icon.
// Create a label.
jfrm.add(jl);
SwingUtilities.invokeLater
new Runnable()
new JLabelDemo();
);
JButton :
The JButton class provides the functionality of a push button. JButton allows an icon, a string, or
both to be associated with the push button. Three of its constructors are shown here: JButton(Icon
icon) JButton(String str) JButton(String str, Icon icon) Here, str and icon are the string and icon used
for the button.
JToggleButton:
A useful variation on the push button is called a toggle button. A toggle button looks just like a push
button, but it acts differently because it has two states: pushed and released. That is, when you press
a toggle button, it stays pressed rather than popping back up as a regular push button does.
Check Boxes :
The JCheckBox class provides the functionality of a check box. Its immediate superclass is
JToggleButton, which provides support for two-state buttons, as just described. JCheckBox defines
several constructors. The one used here is o JCheckBox(String str).
Radio Buttons :
Radio buttons are a group of mutually exclusive buttons, in which only one button can be selected at
any one time. They are supported by the JRadioButton class, which extends JToggleButton.
JRadioButton provides several constructors. The one used in the example is shown here:
JRadioButton(String str) Here, str is the label for the button. Other constructors let you specify the
initial selection state of the button and specify an icon.
// Demonstrate button
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public JToggleButtonDemo() {
jfrm.setLayout(new FlowLayout());
jfrm.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
jfrm.setSize(200, 100);
// Create a label.
jtbn.addItemListener(new ItemListener() {
);
// Add the toggle button and label to the content pane.
jfrm.add(jtbn); jfrm.add(jlab);
jfrm.setVisible(true);
new JToggleButtonDemo();
);
Containers:
Swing defines two types of containers. The first are top-level containers: JFrame, JApplet, JWindow,
and JDialog. These containers do not inherit JComponent. They do, however, inherit the AWT classes
Component and Container. Unlike Swing’s other components, which are lightweight, the top-level
containers are heavyweight. This makes the top-level containers a special case in the Swing
component library. A top-level container is not contained within any other container. Furthermore,
every containment hierarchy must begin with a top-level container. The one most commonly used for
applications is JFrame. In the past, the one used for applets was JApplet. As a result, JApplet is also
deprecated for removal. Furthermore, beginning with JDK 11, applet support has been removed.
The second type of containers supported by Swing are lightweight containers. Lightweight containers
do inherit JComponent. An example of a lightweight container is JPanel, which is a general-purpose
container. Lightweight containers are often used to organize and manage groups of related
components because a lightweight container can be contained within another container. Thus, you
can use lightweight containers such as JPanel to create subgroups of related controls that are
contained within an outer container.
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
class EventDemo {
jfrm.setLayout(new FlowLayout());
jfrm.setSize(220, 90);
jbtnAlpha.addActionListener(new ActionListener() {
);
jbtnBeta.addActionListener(new ActionListener() {
);
jfrm.add(jbtnBeta);
jfrm.add(jlab);
jfrm.setVisible(true);
SwingUtilities.invokeLater(new Runnable() {
new EventDemo();
);