Question stringlengths 1 113 | Answer stringlengths 22 6.98k |
|---|---|
What is Java JRootPane? | RootPane is a lightweight container used behind the scenes by JFrame, JDialog, JWindow, JApplet, and JInternalFrame. |
Can you provide an example of JRootPane? | import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JRootPane;
public class JRootPaneExample {
public static void main(String[] args) {
JFrame f = new JFrame();
f.setDefaultCloseOperatio... |
How to use ToolTip in Java Swing? | You can create a tool tip for any JComponent with setToolTipText() method. This method is used to set up a tool tip for the component.
For example, to add tool tip to PasswordField, you need to add only one line of code:
field.setToolTipText("Enter your Password"); |
Can you provide an example of Simple ToolTip? | import javax.swing.*;
public class ToolTipExample {
public static void main(String[] args) {
JFrame f=new JFrame("Password Field Example");
//Creating PasswordField and label
JPasswordField value = new JPasswordField();
value.setBounds(100,100,100,30);
value.se... |
How to change TitleBar icon in Java AWT and Swing? | The setIconImage() method of Frame class is used to change the icon of Frame or Window. It changes the icon which is displayed at the left side of Frame or Window.
The Toolkit class is used to get instance of Image class in AWT and Swing.
Toolkit class is the abstract super class of every implementation in the Ab... |
Can you provide an Example to change TitleBar icon in Java AWT? | import java.awt.*;
class IconExample {
IconExample(){
Frame f=new Frame();
Image icon = Toolkit.getDefaultToolkit().getImage("D:\\icon.png");
f.setIconImage(icon);
f.setLayout(null);
f.setSize(400,400);
f.setVisible(true);
}
public static void main(String args[]... |
Can you provide an Example to change TitleBar icon in Java Swing? | import javax.swing.*;
import java.awt.*;
class IconExample {
IconExample(){
JFrame f=new JFrame();
Image icon = Toolkit.getDefaultToolkit().getImage("D:\\icon.png");
f.setIconImage(icon);
f.setLayout(null);
f.setSize(200,200);
f.setVisible(true);
}
public static void main(String ... |
How to make an executable jar file in Java? | The jar (Java Archive) tool of JDK provides the facility to create the executable jar file. An executable jar file calls the main method of the class if you double click it.
To create the executable jar file, you need to create .mf file, also known as manifest file. |
What is jar (Java Archive)? | The jar (Java Archive) tool of JDK provides the facility to create the executable jar file. An executable jar file calls the main method of the class if you double click it.
To create the executable jar file, you need to create .mf file, also known as manifest file. |
What is Creating manifest file? | To create manifest file, you need to write Main-Class, then colon, then space, then classname then enter. For example:
myfile.mf
Main-Class: First
As you can see, the mf file starts with Main-Class colon space class name. Here, class name is First.
In mf file, new line is must after the class name. |
What is Creating executable jar file using jar tool? | The jar tool provides many switches, some of them are as follows:
1-c creates new archive file
2-v generates verbose output. It displays the included or extracted resource on the standard output.
3-m includes manifest information from the given mf file.
4-f specifies the archive file name
5.-x extracts files from the ... |
Can you provide an Example of digital clock in swing:? | import javax.swing.*;
import java.awt.*;
import java.text.*;
import java.util.*;
public class DigitalWatch implements Runnable{
JFrame f;
Thread t=null;
int hours=0, minutes=0, seconds=0;
String timeString = "";
JButton b;
DigitalWatch(){
f=new JFrame();
t = new T... |
What is Displaying graphics in swing:? | java.awt.Graphics class provides many methods for graphics programming. |
What is public abstract void drawString(String str, int x, int y)? | public abstract void drawString(String str, int x, int y): is used to draw the specified string. |
What is public void drawRect(int x, int y, int width, int height)? | public void drawRect(int x, int y, int width, int height): draws a rectangle with the specified width and height. |
What is public abstract void fillRect(int x, int y, int width, int height)? | public abstract void fillRect(int x, int y, int width, int height): is used to fill rectangle with the default color and specified width and height. |
What is public abstract void drawOval(int x, int y, int width, int height)? | public abstract void drawOval(int x, int y, int width, int height): is used to draw oval with the specified width and height. |
What is public abstract void fillOval(int x, int y, int width, int height)? | public abstract void fillOval(int x, int y, int width, int height): is used to fill oval with the default color and specified width and height. |
What is public abstract void drawLine(int x1, int y1, int x2, int y2)? | public abstract void drawLine(int x1, int y1, int x2, int y2): is used to draw line between the points(x1, y1) and (x2, y2). |
What is public abstract boolean drawImage(Image img, int x, int y, ImageObserver observer)? | public abstract boolean drawImage(Image img, int x, int y, ImageObserver observer): is used draw the specified image. |
What is public abstract void drawArc(int x, int y, int width, int height, int startAngle, int arcAngle)? | public abstract void drawArc(int x, int y, int width, int height, int startAngle, int arcAngle): is used draw a circular or elliptical arc. |
What is public abstract void fillArc(int x, int y, int width, int height, int startAngle, int arcAngle)? | public abstract void fillArc(int x, int y, int width, int height, int startAngle, int arcAngle): is used to fill a circular or elliptical arc. |
What is public abstract void setColor(Color c)? | public abstract void setColor(Color c): is used to set the graphics current color to the specified color. |
What is public abstract void setFont(Font font)? | public abstract void setFont(Font font): is used to set the graphics current font to the specified font. |
Can you provide an Example of displaying graphics in swing? | import java.awt.*;
import javax.swing.JFrame;
public class DisplayGraphics extends Canvas{
public void paint(Graphics g) {
g.drawString("Hello",40,40);
setBackground(Color.WHITE);
g.fillRect(130, 30,100, 80);
g.drawOval(30,130,50, 60);
setF... |
What is Displaying image in swing? | For displaying image, we can use the method drawImage() of Graphics class. |
What is Syntax of drawImage() method? | public abstract boolean drawImage(Image img, int x, int y, ImageObserver observer): is used draw the specified image. |
What is public abstract boolean drawImage(Image img, int x, int y, ImageObserver observer)? | public abstract boolean drawImage(Image img, int x, int y, ImageObserver observer): is used draw the specified image. |
Can you provide an Example of displaying image in swing? | import java.awt.*;
import javax.swing.JFrame;
public class MyCanvas extends Canvas{
public void paint(Graphics g) {
Toolkit t=Toolkit.getDefaultToolkit();
Image i=t.getImage("p3.gif");
g.drawImage(i, 120,100,this);
}
public stat... |
What are the Java LayoutManagers? | The LayoutManagers are used to arrange components in a particular manner. The Java LayoutManagers facilitates us to control the positioning and size of the components in GUI forms. LayoutManager is an interface that is implemented by all the classes of layout managers. There are the following classes that represent the... |
What are the Java BorderLayout? | The BorderLayout is used to arrange the components in five regions: north, south, east, west, and center. Each region (area) may contain one component only. It is the default layout of a frame or window. The BorderLayout provides five constants for each region:
1.public static final int NORTH
2.public static final ... |
What are the Constructors of BorderLayout class:? | -BorderLayout(): creates a border layout but with no gaps between the components.
-BorderLayout(int hgap, int vgap): creates a border layout with the given horizontal and vertical gaps between the components. |
Can you provide an Example of BorderLayout class: Using BorderLayout() constructor? | FileName: Border.java
import java.awt.*;
import javax.swing.*;
public class Border
{
JFrame f;
Border()
{
f = new JFrame();
// creating buttons
JButton b1 = new JButton("NORTH");; // the button will be labeled as NORTH
JButton b2 = new JBut... |
What is Java GridLayout? | The Java GridLayout class is used to arrange the components in a rectangular grid. One component is displayed in each rectangle. |
What are the Constructors of GridLayout class? | 1.GridLayout(): creates a grid layout with one column per component in a row.
2.GridLayout(int rows, int columns): creates a grid layout with the given rows and columns but no gaps between the components.
3.GridLayout(int rows, int columns, int hgap, int vgap): creates a grid layout with the given rows and columns al... |
Can you provide an Example of GridLayout class: Using GridLayout() Constructor? | The GridLayout() constructor creates only one row. The following example shows the usage of the parameterless constructor.
FileName: GridLayoutExample.java
// import statements
import java.awt.*;
import javax.swing.*;
public class GridLayoutExample
{
JFrame frameObj;
// constru... |
What is Java FlowLayout? | The Java FlowLayout class is used to arrange the components in a line, one after another (in a flow). It is the default layout of the applet or panel. |
What are the Fields of FlowLayout class? | 1.public static final int LEFT
2.public static final int RIGHT
3.public static final int CENTER
4.public static final int LEADING
5.public static final int TRAILING |
What are the Constructors of FlowLayout class? | 1.FlowLayout(): creates a flow layout with centered alignment and a default 5 unit horizontal and vertical gap.
2.FlowLayout(int align): creates a flow layout with the given alignment and a default 5 unit horizontal and vertical gap.
3.FlowLayout(int align, int hgap, int vgap): creates a flow layout with the given alig... |
Can you provide an Example of FlowLayout class: Using FlowLayout() constructor? | FileName: FlowLayoutExample.java
// import statements
import java.awt.*;
import javax.swing.*;
public class FlowLayoutExample
{
JFrame frameObj;
// constructor
FlowLayoutExample()
{
// creating a frame object
frameObj = new JFrame();
/... |
What is Java BoxLayout? | The Java BoxLayout class is used to arrange the components either vertically or horizontally. For this purpose, the BoxLayout class provides four constants. They are as follows:
Note: The BoxLayout class is found in javax.swing package. |
What are the Fields of BoxLayout Class? | 1.public static final int X_AXIS: Alignment of the components are horizontal from left to right.
2.public static final int Y_AXIS: Alignment of the components are vertical from top to bottom.
3.public static final int LINE_AXIS: Alignment of the components is similar to the way words are aligned in a line, which is bas... |
What is Constructor of BoxLayout class? | BoxLayout(Container c, int axis): creates a box layout that arranges the components with the given axis. |
Can you provide an Example of BoxLayout class with Y-AXIS:? | FileName: BoxLayoutExample1.java
import java.awt.*;
import javax.swing.*;
public class BoxLayoutExample1 extends Frame {
Button buttons[];
public BoxLayoutExample1 () {
buttons = new Button [5];
for (int i = 0;i<5;i++) {
buttons[i] = new Button ("B... |
Can you provide an Example of BoxLayout class with X-AXIS? | FileName: BoxLayoutExample2.java
import java.awt.*;
import javax.swing.*;
public class BoxLayoutExample2 extends Frame {
Button buttons[];
public BoxLayoutExample2() {
buttons = new Button [5];
for (int i = 0;i<5;i++) {
buttons[i] = new Button ("Bu... |
Can you provide an Example of BoxLayout Class with LINE_AXIS? | The following example shows the effect of setting the value of ComponentOrientation property of the container to RIGHT_TO_LEFT. If we do not set the value of ComponentOrientation property, then the components would be laid out from left to right. Comment line 11, and see it yourself.
FileName: BoxLayoutExample3.java... |
Can you provide an Example of BoxLayout Class with PAGE_AXIS? | The following example shows how to use PAGE_AXIS.
FileName: BoxLayoutExample4.java
// import statements
import java.awt.*;
import javax.swing.*;
public class BoxLayoutExample4 extends Frame
{
Button buttons[];
// constructor of the class
public BoxLayoutExample4()
{
JF... |
What is Java CardLayout? | The Java CardLayout class manages the components in such a manner that only one component is visible at a time. It treats each component as a card that is why it is known as CardLayout. |
What are the Constructors of CardLayout Class? | 1.CardLayout(): creates a card layout with zero horizontal and vertical gap.
2.CardLayout(int hgap, int vgap): creates a card layout with the given horizontal and vertical gap. |
What are the Commonly Used Methods of CardLayout Class? | -public void next(Container parent): is used to flip to the next card of the given container.
-public void previous(Container parent): is used to flip to the previous card of the given container.
-public void first(Container parent): is used to flip to the first card of the given container.
-public void last(Container ... |
Can you provide an Example of CardLayout Class: Using Default Constructor | The following program uses the next() method to move to the next card of the container.
FileName: CardLayoutExample1.java
// import statements
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
public class CardLayoutExample1 extends JFrame implements ActionListener
{
... |
What is Java GridBagLayout? | The Java GridBagLayout class is used to align components vertically, horizontally or along their baseline.
The components may not be of the same size. Each GridBagLayout object maintains a dynamic, rectangular grid of cells. Each component occupies one or more cells known as its display area. Each component associat... |
What is GridBagLayout Fields? | Modifier and Type: double[]
Field: columnWeights
Description: It is used to hold the overrides to the column weights.
Modifier and Type: int[]
Field: columnWidths
Description: It is used to hold the overrides to the column minimum width.
Modifier and Type: protected Hashtable<Component,GridBagConstraints>
Fi... |
Can you give me GridBagLayout Methods? | Modifier and Type: void
Method: addLayoutComponent(Component comp, Object constraints)
Description: It adds specified component to the layout, using the specified constraints object.
Modifier and Type: void
Method: addLayoutComponent(String name, Component comp)
Description: It has no effect, since this layout m... |
What is GroupLayout? | GroupLayout groups its components and places them in a Container hierarchically. The grouping is done by instances of the Group class.
Group is an abstract class, and two concrete classes which implement this Group class are SequentialGroup and ParallelGroup.
SequentialGroup positions its child sequentially one a... |
What is Java SpringLayout? | A SpringLayout arranges the children of its associated container according to a set of constraints. Constraints are nothing but horizontal and vertical distance between two-component edges. Every constraint is represented by a SpringLayout.Constraint object.
Each child of a SpringLayout container, as well as the con... |
What is pringLayout Fields? | Modifier and Type: static String
Field: BASELINE
Description: It specifies the baseline of a component.
Modifier and Type: static String
Field: EAST
Description: It specifies the right edge of a component's bounding rectangle.
Modifier and Type: static String
Field: HEIGHT
Description: It specifies the heig... |
Can you give SpringLayout Methods? | SpringLayout Methods
Modifier and Type: void
Method: addLayoutComponent(Component component, Object constraints)
Description: If constraints is an instance of SpringLayout. Constraints, associates the constraints with the specified component.
Modifier and Type: void
Method: addLayoutComponent(String name, Component c)... |
What is ScrollPaneLayout? | The layout manager is used by JScrollPane. JScrollPaneLayout is responsible for nine components: a viewport, two scrollbars, a row header, a column header, and four "corner" components. |
FieldScrollPanelLayout Field | Modifier and Type: protected JViewport
Field: colHead
Description: It is column header child.
Modifier and Type: protected JScrollBar
Field: hsb
Description: It is scrollpane's horizontal scrollbar child.
Modifier and Type: protected int
Field: hsbPolicy
Description: It displays policy for the horizontal sc... |
Can you give me ScrollPanelLayout Methods? | ScrollPanelLayout Methods
Modifier and Type: void
Method: addLayoutComponent(String s, Component c)
Description: It adds the specified component to the layout.
Modifier and Type: protected Component
Method: addSingletonComponent(Component oldC, Component newC)
Description: It removes an existing component.
... |
What is Java Applet? | Applet is a special type of program that is embedded in the webpage to generate the dynamic content. It runs inside the browser and works at client side. |
What is the Advantage of Applet? | There are many advantages of applet. They are as follows:
-It works at client side so less response time.
-Secured
-It can be executed by browsers running under many plateforms, including Linux, Windows, Mac Os etc. |
What is Drawback of Applet? | Plugin is required at client browser to execute applet.
|
What is Lifecycle of Java Applet? | 1.Applet is initialized.
2.Applet is started.
3.Applet is painted.
4.Applet is stopped.
5.Applet is destroyed. |
What is Lifecycle methods for Applet? | The java.applet.Applet class 4 life cycle methods and java.awt.Component class provides 1 life cycle methods for an applet. |
What is java.applet.Applet class? | For creating any applet java.applet.Applet class must be inherited. It provides 4 life cycle methods of applet.
1.public void init(): is used to initialized the Applet. It is invoked only once.
2.public void start(): is invoked after the init() method or browser is maximized. It is used to start the Applet.
3.public v... |
What is java.awt.Component class? | The Component class provides 1 life cycle method of applet.
1.public void paint(Graphics g): is used to paint the Applet. It provides Graphics class object that can be used for drawing oval, rectangle, arc etc. |
Who is responsible to manage the life cycle of an applet? | Java Plug-in software. |
How to run an Applet? | There are two ways to run an applet
1.By html file.
2.By appletViewer tool (for testing purpose). |
Can you provide an Simple example of Applet by html file | To execute the applet by html file, create an applet and compile it. After that create an html file and place the applet code in html file. Now click the html file.
//First.java
import java.applet.Applet;
import java.awt.Graphics;
public class First extends Applet{
public void paint(Graphics g){
g... |
What is Displaying Graphics in Applet? | java.awt.Graphics class provides many methods for graphics programming.
Commonly used methods of Graphics class: |
What is Commonly used methods of Graphics class? | 1.public abstract void drawString(String str, int x, int y): is used to draw the specified string.
2.public void drawRect(int x, int y, int width, int height): draws a rectangle with the specified width and height.
3.public abstract void fillRect(int x, int y, int width, int height): is used to fill rectangle with th... |
Can you provide an Example of Graphics in applet? | import java.applet.Applet;
import java.awt.*;
public class GraphicsDemo extends Applet{
public void paint(Graphics g){
g.setColor(Color.red);
g.drawString("Welcome",50, 50);
g.drawLine(20,30,20,300);
g.drawRect(70,100,30,30);
g.fillRect(170,100,30,30);
g.drawOval(70,200,30,30);
... |
What is Displaying Image in Applet? | Applet is mostly used in games and animation. For this purpose image is required to be displayed. The java.awt.Graphics class provide a method drawImage() to display the image. |
Can you give me Syntax of drawImage() method? | public abstract boolean drawImage(Image img, int x, int y, ImageObserver observer): is used draw the specified image. |
How to get the object of Image? | The java.applet.Applet class provides getImage() method that returns the object of Image. Syntax:
public Image getImage(URL u, String image){} |
Can you give me Other required methods of Applet class to display image? | 1.public URL getDocumentBase(): is used to return the URL of the document in which applet is embedded.
2.public URL getCodeBase(): is used to return the base URL. |
Can you provide an Example of displaying image in applet? | import java.awt.*;
import java.applet.*;
public class DisplayImage extends Applet {
Image picture;
public void init() {
picture = getImage(getDocumentBase(),"sonoo.jpg");
}
public void paint(Graphics g) {
g.drawImage(picture, 30,30, this);
}
... |
What is Animation in Applet? | Applet is mostly used in games and animation. For this purpose image is required to be moved. |
Can you provide an Example of animation in applet? | import java.awt.*;
import java.applet.*;
public class AnimationExample extends Applet {
Image picture;
public void init() {
picture =getImage(getDocumentBase(),"bike_1.gif");
}
public void paint(Graphics g) {
for(int i=0;i<500;i++){
g.drawImage(picture, i,3... |
What is EventHandling in Applet? | As we perform event handling in AWT or Swing, we can perform it in applet also. Let's see the simple example of event handling in applet that prints a message by click on the button. |
Can you provide an Example of EventHandling in applet? | import java.applet.*;
import java.awt.*;
import java.awt.event.*;
public class EventApplet extends Applet implements ActionListener{
Button b;
TextField tf;
public void init(){
tf=new TextField();
tf.setBounds(30,40,150,20);
b=new Button("Click");
b.setBounds(80,150,60,50);
add(b);add(tf)... |
What is JApplet class in Applet? | As we prefer Swing to AWT. Now we can use JApplet that can have all the controls of swing. The JApplet class extends the Applet class. |
Example of EventHandling in JApplet | import java.applet.*;
import javax.swing.*;
import java.awt.event.*;
public class EventJApplet extends JApplet implements ActionListener{
JButton b;
JTextField tf;
public void init(){
tf=new JTextField();
tf.setBounds(30,40,150,20);
b=new JButton("Click");
b.setBounds(80,150,70,40)... |
Painting in Applet | We can perform painting operation in applet by the mouseDragged() method of MouseMotionListener. |
Can you provide an Example of Painting in Applet? | import java.awt.*;
import java.awt.event.*;
import java.applet.*;
public class MouseDrag extends Applet implements MouseMotionListener{
public void init(){
addMouseMotionListener(this);
setBackground(Color.red);
}
public void mouseDragged(MouseEvent me){
Graphics g=getGraphics();
g... |
What is Digital clock in Applet? | Digital clock can be created by using the Calendar and SimpleDateFormat class. Let's see the simple example: |
Can you provide an Example of Digital clock in Applet? | import java.applet.*;
import java.awt.*;
import java.util.*;
import java.text.*;
public class DigitalClock extends Applet implements Runnable {
Thread t = null;
int hours=0, minutes=0, seconds=0;
String timeString = "";
public void init() {
setBackground( Color.g... |
What is Analog clock in Applet? | Analog clock can be created by using the Math class. Let's see the simple example: |
Can you provide an Example of Analog clock in Applet? | import java.applet.*;
import java.awt.*;
import java.util.*;
import java.text.*;
public class MyClock extends Applet implements Runnable {
int width, height;
Thread t = null;
boolean threadSuspended;
int hours=0, minutes=0, seconds=0;
String timeString = "";
p... |
What is Parameter in Applet? | We can get any information from the HTML file as a parameter. For this purpose, Applet class provides a method named getParameter(). Syntax:
public String getParameter(String parameterName) |
Can you provide an Example of using parameter in Applet? | import java.applet.Applet;
import java.awt.Graphics;
public class UseParam extends Applet{
public void paint(Graphics g){
String str=getParameter("msg");
g.drawString(str,50, 50);
}
}
myapplet.html
<html>
<body>
<applet code="UseParam.class" width="300" height="300">
<param... |
What is Applet Communication? | java.applet.AppletContext class provides the facility of communication between applets. We provide the name of applet through the HTML file. It provides getApplet() method that returns the object of Applet. Syntax:
public Applet getApplet(String name){} |
Can you provide an Example of Applet Communication? | import java.applet.*;
import java.awt.*;
import java.awt.event.*;
public class ContextApplet extends Applet implements ActionListener{
Button b;
public void init(){
b=new Button("Click");
b.setBounds(50,50,60,50);
add(b);
b.addActionListener(this);
}
public void actionPerfor... |
What is Java Reflection API? | Java Reflection is a process of examining or modifying the run time behavior of a class at run time.
The java.lang.Class class provides many methods that can be used to get metadata, examine and change the run time behavior of a class.
The java.lang and java.lang.reflect packages provide classes for java reflecti... |
What is java.lang.Class class? | The java.lang.Class class performs mainly two tasks:
provides methods to get the metadata of a class at run time.
provides methods to examine and change the run time behavior of a class. |
Can you give me Commonly used methods of Class class? | Commonly used methods of Class class:
Method: public String getName()
Description: returns the class name
Method: public static Class forName(String className)throws ClassNotFoundException
Description: loads the class and returns the reference of Class class.
Method: public Object newInstance()throws Instant... |
How to get the object of Class class? | There are 3 ways to get the instance of Class class. They are as follows:
-forName() method of Class class
-getClass() method of Object class
-the .class syntax |
What is Determining the class object? | The following methods of Class class are used to determine the class object:
1) public boolean isInterface(): determines if the specified Class object represents an interface type.
2) public boolean isArray(): determines if this Class object represents an array class.
3) public boolean isPrimitive(): determine... |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.