JFrame myFrame = new JFrame();
myFrame.setVisible(true);
myFrame.setExtendedState(myFrame.getExtendedState() | JFrame.MAXIMIZED_BOTH);
maximize a JFrame window in java
How to Maximize a JFrame :
The usage of Java packages.
Explain the usage of Java packages.
A. This is a way to organize files when a project consists of multiple modules. It also helps resolve naming conflicts when different packages have classes with the same names.
Packages access level also allows you to protect data from being used by the non-authorized classes.
Java: difference between private, protected, and public?
These keywords are for allowing privileges to components such as java methods and variables.
Public: accessible to all classes
Private: accessible only to the class to which they belong
Protected: accessible to the class to which they belong and any subclasses.
Access specifiers are keywords that determines the type of access to the member of a class. These are:
* Public
* Protected
* Private
* Defaults
Public: accessible to all classes
Private: accessible only to the class to which they belong
Protected: accessible to the class to which they belong and any subclasses.
Access specifiers are keywords that determines the type of access to the member of a class. These are:
* Public
* Protected
* Private
* Defaults
java show window JFrame JDialog always on top
We have to use this feature of java.awt.Window : Window.alwaysOnTop(boolean);
Example code :
import javax.swing.JFrame;
import javax.swing.JLabel;
public class Always_on_Top_JFrame_JAVA{
public static void main(String[] args) {
JFrame frame = new JFrame("Title - always on top :D ");
// Set's the window to be "always on top"
frame.setAlwaysOnTop( true );
frame.setLocationByPlatform( true );
frame.add( new JLabel(" Always on TOP ") );
frame.pack();
frame.setVisible( true );
}
}
Order of catching exception in java
Does it matter in what order catch statements for FileNotFoundException and IOExceptipon are written?
A. Yes, it does. The FileNotFoundException is inherited from the IOException.
A. Yes, it does. The FileNotFoundException is inherited from the IOException.
Exception's subclasses have to be caught first.
Exception
^
|
IOException
^
|
FileNotFoundException
So while catching exceptions, we must catch the low level exception first - here : FileNotFoundException .
#The hierarchy in Java Exception framework :
wrapper classes in Java
Describe the wrapper classes in Java.Wrapper class is wrapper around a primitive data type. An instance of a wrapper class contains, or wraps, a primitive value of the corresponding type.
Following table lists the primitive types and the corresponding wrapper classes:
Following table lists the primitive types and the corresponding wrapper classes:
Primitive - Wrapper
boolean - java.lang.Boolean
byte - java.lang.Byte
char - java.lang.Character
double - java.lang.Double
float - java.lang.Float
int - java.lang.Integer
long - java.lang.Long
short - java.lang.Short
void - java.lang.Void
Java Class as Applet as well as Application
Can you write a Java class that could be used both as an applet as well as an application?
A. Yes. Add a main() method to the applet.
A. Yes. Add a main() method to the applet.
Bring JFrame JDialog Window to front java swing
To bring JFrame or JDialog ... or Window (JFrame and JDialog inherits Window) to front in JAVA, fun the code below :
java.awt.EventQueue.invokeLater(new Runnable() {
@Override
//myFrame is object of Window or JFrame
public void run() {
myFrame.toFront();
myFrame.repaint();
}
});
Redirect Standard System Output and Error Message to PrintStream in Java
In a Java program, how can you divert standard system output or error messages, say to a file?
->We can achieve this by using
public static void setErr(PrintStream err)
and public static void setOut(PrintStream out) methods.
By default, they both point at the system console. The following example redirects Out and Err messages to 'error.txt' file
Stream stream = new Stream(new FileOutputStream("error.txt"));
System.setErr(stream);
System.setOut(stream);
You can redirect the Err and Out stream to any PrintStream object.
->We can achieve this by using
public static void setErr(PrintStream err)
and public static void setOut(PrintStream out) methods.
By default, they both point at the system console. The following example redirects Out and Err messages to 'error.txt' file
Stream stream = new Stream(new FileOutputStream("error.txt"));
System.setErr(stream);
System.setOut(stream);
You can redirect the Err and Out stream to any PrintStream object.
concatenate arrays of any type - java
Combining two arrays of any type
public static T[] concat(T[] first, T[] second) {
T[] result = Arrays.copyOf(first, first.length + second.length);
System.arraycopy(second, 0, result, first.length, second.length);
return result;
}
For combining arbitrary number of arrays
Subscribe to:
Posts
(
Atom
)