import java.io.*; /** A class that eases the pain of running external processes * from applications. Lets you run a program three ways: **
* Note that the PATH is not taken into account, so you must * specify the full pathname to the command, and shell * built-in commands will not work. For instance, on Unix the * above three examples might look like: *- exec: Execute the command, returning * immediately even if the command is still running. * This would be appropriate for printing a file. *
- execWait: Execute the command, but don?t * return until the command finishes. This would be * appropriate for sequential commands where the first * depends on the second having finished (e.g., *
javacfollowed byjava). *- execPrint: Execute the command and print the * output. This would be appropriate for the Unix * command
ls. **
Exec.exec("/usr/ucb/lpr Some-File");*
Exec.execWait("/usr/local/bin/javac Foo.java"); * Exec.execWait("/usr/local/bin/java Foo");*
Exec.execPrint("/usr/bin/ls -al");*
*
* Taken from Core Web Programming from
* Prentice Hall and Sun Microsystems Press,
* .
* © 2001 Marty Hall and Larry Brown;
* may be freely used or adapted.
*/public class Exec {
private static boolean verbose = true;
/** Determines if the Exec class should print which commands
* are being executed, and prints error messages if a problem
* is found. Default is true.
*
* @param verboseFlag true: print messages, false: don?t.
*/public static void setVerbose(boolean verboseFlag) {
verbose = verboseFlag;
}/** Will Exec print status messages? */
public static boolean getVerbose() {
return(verbose);
}/** Starts a process to execute the command. Returns
* immediately, even if the new process is still running.
*
* @param command The full pathname of the command to
* be executed. No shell built-ins (e.g., "cd") or shell
* meta-chars (e.g. ">") are allowed.
* @return false if a problem is known to occur, but since
* this returns immediately, problems aren?t usually found
* in time. Returns true otherwise.
*/public static boolean exec(String command) {
return(exec(command, false, false));
}/** Starts a process to execute the command. Waits for the
* process to finish before returning.
*
* @param command The full pathname of the command to
* be executed. No shell built-ins or shell metachars are
* allowed.
* @return false if a problem is known to occur, either due
* to an exception or from the subprocess returning a
* nonzero value. Returns true otherwise.
*/public static boolean execWait(String command) {
return(exec(command, false, true));
}/** Starts a process to execute the command. Prints any output
* the command produces.
*
* @param command The full pathname of the command to
* be executed. No shell built-ins or shell meta-chars are
* allowed.
* @return false if a problem is known to occur, either due
* to an exception or from the subprocess returning a
* nonzero value. Returns true otherwise.
*/public static boolean execPrint(String command) {
return(exec(command, true, false));
}/** This creates a Process object via Runtime.getRuntime.exec()
* Depending on the flags, it may call waitFor on the process
* to avoid continuing until the process terminates, and open
* an input stream from the process to read the results.
*/private static boolean exec(String command,
boolean printResults,
boolean wait) {
if (verbose) {
printSeparator();
System.out.println("Executing '" + command + "'.");
}
try {
// Start running command, returning immediately.
Process p = Runtime.getRuntime().exec(command);// Print the output. Since we read until there is no more
// input, this causes us to wait until the process is
// completed.
if(printResults) {
BufferedReader buffer = new BufferedReader(
new InputStreamReader(p.getInputStream()));
String s = null;
try {
while ((s = buffer.readLine()) != null) {
System.out.println("Output: " + s);
}
buffer.close();
if (p.exitValue() != 0) {
if (verbose) {
printError(command + " -- p.exitValue() != 0");
}
return(false);
}
} catch (Exception e) {
// Ignore read errors; they mean the process is done.
}// If not printing the results, then we should call waitFor
// to stop until the process is completed.
} else if (wait) {
try {
System.out.println(" ");
int returnVal = p.waitFor();
if (returnVal != 0) {
if (verbose) {
printError(command);
}
return(false);
}
} catch (Exception e) {
if (verbose) {
printError(command, e);
}
return(false);
}
}
} catch (Exception e) {
if (verbose) {
printError(command, e);
}
return(false);
}
return(true);
}private static void printError(String command,
Exception e) {
System.out.println("Error doing exec(" + command + "): " +
e.getMessage());
System.out.println("Did you specify the full " +
"pathname?");
}private static void printError(String command) {
System.out.println("Error executing ?" + command + "?.");
}private static void printSeparator() {
System.out.println
("==============================================");
}
}Note: Brought from our old site: http://www.salearningschool.com/example_codes/ on Jan 2nd, 2017 From: http://sitestree.com/?p=10215
Categories:Programming Code Examples, Java/J2EE/J2ME, Basic Java
Tags:Java/J2EE/J2MEBasic Java
Post Data:2017-01-02 16:04:23Shop Online: https://www.ShopForSoul.com/
(Big Data, Cloud, Security, Machine Learning): Courses: http://Training.SitesTree.com
In Bengali: http://Bangla.SaLearningSchool.com
http://SitesTree.com
8112223 Canada Inc./JustEtc: http://JustEtc.net (Software/Web/Mobile/Big-Data/Machine Learning)
Shop Online: https://www.ShopForSoul.com/
Medium: https://medium.com/@SayedAhmedCanada
May 06
Exec.java Provides static methods for running external processes from applications. #Programming Code Examples #Java/J2EE/J2ME #Basic Java
May 06
DropBall.java Uses a while loop to determine how long it takes a ball to fall from the top of the Washington Monument to the ground. #Programming Code Examples #Java/J2EE/J2ME #Basic Java
/** Simulating dropping a ball from the top of the Washington
* Monument. The program outputs the height of the ball each
* second until the ball hits the ground.
*
* Taken from Core Web Programming from
* Prentice Hall and Sun Microsystems Press,
* .
* © 2001 Marty Hall and Larry Brown;
* may be freely used or adapted.
*/
public class DropBall {
public static void main(String[] args) {
int time = 0;
double start = 550.0, drop = 0.0;
double height = start;
while (height > 0) {
System.out.println("After " + time +
(time==1 ? " second, " : " seconds,") +
"the ball is at " + height + " feet.");
time++;
drop = freeFall(time);
height = start - drop;
}
System.out.println("Before " + time + " seconds could " +
"expire, the ball hit the ground!");
}
/** Calculate the distance in feet for an object in
* free fall.
*/
public static double freeFall (float time) {
// Gravitational constant is 32 feet per second squared
return(16.0 * time * time); // 1/2 gt^2
}
}
Note: Brought from our old site: http://www.salearningschool.com/example_codes/ on Jan 2nd, 2017 From: http://sitestree.com/?p=10214
Categories:Programming Code Examples, Java/J2EE/J2ME, Basic Java
Tags:Java/J2EE/J2MEBasic Java
Post Data:2017-01-02 16:04:23
Shop Online: https://www.ShopForSoul.com/
(Big Data, Cloud, Security, Machine Learning): Courses: http://Training.SitesTree.com
In Bengali: http://Bangla.SaLearningSchool.com
http://SitesTree.com
8112223 Canada Inc./JustEtc: http://JustEtc.net (Software/Web/Mobile/Big-Data/Machine Learning)
Shop Online: https://www.ShopForSoul.com/
Medium: https://medium.com/@SayedAhmedCanada
May 06
Creates three radio buttons and illustrates handling #Programming Code Examples #Java/J2EE/J2ME #Basic Swing
JRadioButtonTest.java Creates three radio buttons and illustrates handling ItemEvents in response to selecting a radio button.
import javax.swing.JRadioButton;
import javax.swing.ButtonGroup;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
/**
*/
public class JRadioButtonTest extends JPanel
implements ItemListener {
public JRadioButtonTest() {
String[] labels = {"Java Swing","Java Servlets",
"JavaServer Pages"};
JRadioButton[] buttons = new JRadioButton[3];
ButtonGroup group = new ButtonGroup();
for(int i=0; i<buttons .length; i++) {
buttons[i] = new JRadioButton(labels[i]);
buttons[i].setContentAreaFilled(false);
buttons[i].addItemListener(this);
group.add(buttons[i]);
add(buttons[i]);
}
}
public void itemStateChanged(ItemEvent event) {
JRadioButton radiobutton = (JRadioButton)event.getItem();
if (event.getStateChange() == ItemEvent.SELECTED) {
System.out.println(radiobutton.getText() + " selected.");
} else {
System.out.println(radiobutton.getText() + " deselected.");
}
}
public static void main(String[] args) {
JPanel panel = new JRadioButtonTest();
WindowUtilities.setNativeLookAndFeel();
WindowUtilities.openInJFrame(panel, 400, 75);
}
}
Note: Brought from our old site: http://www.salearningschool.com/example_codes/ on Jan 2nd, 2017 From: http://sitestree.com/?p=10318
Categories:Programming Code Examples, Java/J2EE/J2ME, Basic Swing
Tags:Java/J2EE/J2MEBasic Swing
Post Data:2017-01-02 16:04:31
Shop Online: https://www.ShopForSoul.com/
(Big Data, Cloud, Security, Machine Learning): Courses: http://Training.SitesTree.com
In Bengali: http://Bangla.SaLearningSchool.com
http://SitesTree.com
8112223 Canada Inc./JustEtc: http://JustEtc.net (Software/Web/Mobile/Big-Data/Machine Learning)
Shop Online: https://www.ShopForSoul.com/
Medium: https://medium.com/@SayedAhmedCanada
May 06
Technical and Vocational Institute Summary Report
| Technical and Vocational Institute Summary Report | |||||||
| Education level | Management | Institute | Teacher | Student | |||
| Total | Female | Total | Femal | Total | Female | ||
| 01 – POLYTECHNIC INSTITUTE | PRIVATE | 357 | 12 | 4759 | 868 | 5946 | 1887 |
| PUBLIC | 57 | 3 | 1389 | 165 | 27173 | 15960 | |
| 02 -TECHNICAL SCHOOL AND COLLEGE | PRIVATE | 84 | 5 | 1016 | 222 | 162 | 57 |
| PUBLIC | 37 | 0 | 440 | 65 | 1165 | 183 | |
| 03 – TEXTILE (Vocational Institute) | PUBLIC | 27 | 0 | 156 | 34 | 14 | 1 |
| 04 – TEXTILE TRAINING CENTER | PRIVATE | 74 | 0 | 183 | 20 | 5294 | 2263 |
| PUBLIC | 22 | 4 | 592 | 92 | 4929 | 2339 | |
| 05 – AGRICULTURE TRAINING INSTITUTE | PRIVATE | 57 | 0 | 502 | 77 | 159 | 60 |
| PUBLIC | 8 | 0 | 94 | 21 | |||
| 06 – TEXTILE INSTITUTE | PRIVATE | 12 | 0 | 135 | 31 | 50 | 23 |
| PUBLIC | 1 | 0 | 24 | 1 | |||
| 07 – NATIONAL SKILL | PRIVATE | 129 | 1 | 11985 | 4572 | ||
| PUBLIC | 1 | 0 | 39 | 13 | |||
| 08 – H.S.C(Vocational Independent) | PRIVATE | 1 | 0 | 3 | 0 | ||
| 09 – H.S.C (B.M Independent) | PRIVATE | 600 | 61 | 5809 | 1228 | 1607 | 640 |
| 10 – SURVEY INSTITUTE | PRIVATE | 1 | 0 | 9 | 1 | ||
| PUBLIC | 1 | 0 | 5 | 0 | |||
| 11 – GRAPHIC ARTS | PUBLIC | 1 | 0 | 13 | 3 | ||
| 12 – S.S.C(Vocational Independent) | PRIVATE | 143 | 11 | 778 | 172 | 134 | 56 |
| 13 – GLASS AND CERAMICS | PUBLIC | 1 | 0 | 15 | 2 | ||
| 15 – HEALTH TECHNOLOGY | PRIVATE | 53 | 0 | 169 | 38 | ||
| PUBLIC | 3 | 0 | |||||
| 17 – OTHERS | PRIVATE | 115 | 7 | 1129 | 244 | 3705 | 1217 |
| PUBLIC | 25 | 1 | 498 | 58 | 2647 | 1107 | |
| 18 – DIPLOMA IN FISHERIES | PRIVATE | 4 | 0 | ||||
| Total | 1814 | 105 | 17549 | 3304 | 65178 | 30416 |
May 05
Simple example illustrating the use of check boxes #Programming Code Examples #Java/J2EE/J2ME #Basic Swing
JCheckBoxTest.java Simple example illustrating the use of check boxes.
import javax.swing.*;
import java.awt.event.*;
*/
public class JCheckBoxTest extends JPanel
implements ItemListener,
ActionListener{
JCheckBox checkBox1, checkBox2;
public JCheckBoxTest() {
checkBox1 = new JCheckBox("Java Servlets");
checkBox2 = new JCheckBox("JavaServer Pages");
checkBox1.setContentAreaFilled(false);
checkBox2.setContentAreaFilled(false);
checkBox1.addItemListener(this);
checkBox2.addActionListener(this);
add(checkBox1);
add(checkBox2);
}
public void actionPerformed(ActionEvent event) {
System.out.println("JavaServer Pages selected: " +
checkBox2.isSelected());
}
public void itemStateChanged(ItemEvent event) {
JCheckBox checkbox = (JCheckBox)event.getItem();
if (event.getStateChange() == ItemEvent.SELECTED) {
System.out.println(checkbox.getText() + " selected.");
} else {
System.out.println(checkbox.getText() + " deselected.");
}
}
public static void main(String[] args) {
JPanel panel = new JCheckBoxTest();
WindowUtilities.setNativeLookAndFeel();
WindowUtilities.openInJFrame(panel, 300, 75);
}
}
Note: Brought from our old site: http://www.salearningschool.com/example_codes/ on Jan 2nd, 2017 From: http://sitestree.com/?p=10317
Categories:Programming Code Examples, Java/J2EE/J2ME, Basic Swing
Tags:Java/J2EE/J2MEBasic Swing
Post Data:2017-01-02 16:04:31
Shop Online: https://www.ShopForSoul.com/
(Big Data, Cloud, Security, Machine Learning): Courses: http://Training.SitesTree.com
In Bengali: http://Bangla.SaLearningSchool.com
http://SitesTree.com
8112223 Canada Inc./JustEtc: http://JustEtc.net (Software/Web/Mobile/Big-Data/Machine Learning)
Shop Online: https://www.ShopForSoul.com/
Medium: https://medium.com/@SayedAhmedCanada
May 05
Simple button that the user can select to load the entered URL. #Programming Code Examples #Java/J2EE/J2ME #Basic Swing
JIconButton.java A simple button that the user can select to load the entered URL.
import javax.swing.*;
/** A regular JButton created with an ImageIcon and with borders
* and content areas turned off.
*
*/
public class JIconButton extends JButton {
public JIconButton(String file) {
super(new ImageIcon(file));
setContentAreaFilled(false);
setBorderPainted(false);
setFocusPainted(false);
}
}
Note: Brought from our old site: http://www.salearningschool.com/example_codes/ on Jan 2nd, 2017 From: http://sitestree.com/?p=10316
Categories:Programming Code Examples, Java/J2EE/J2ME, Basic Swing
Tags:Java/J2EE/J2MEBasic Swing
Post Data:2017-01-02 16:04:31
Shop Online: https://www.ShopForSoul.com/
(Big Data, Cloud, Security, Machine Learning): Courses: http://Training.SitesTree.com
In Bengali: http://Bangla.SaLearningSchool.com
http://SitesTree.com
8112223 Canada Inc./JustEtc: http://JustEtc.net (Software/Web/Mobile/Big-Data/Machine Learning)
Shop Online: https://www.ShopForSoul.com/
Medium: https://medium.com/@SayedAhmedCanada
May 05
Basic tool bar for holding multiple buttons. #Programming Code Examples #Java/J2EE/J2ME #Basic Swing
BrowserToolBar.java A basic tool bar for holding multiple buttons.
import java.awt.*;
import javax.swing.*;
/** Part of a small example showing basic use of JToolBar.
* Creates a small dockable toolbar that is supposed to look
* vaguely like one that might come with a Web browser.
* Makes use of ToolBarButton, a small extension of JButton
* that shrinks the margins around the icon and puts text
* label, if any, below the icon.
*
*/
public class BrowserToolBar extends JToolBar {
public BrowserToolBar() {
String[] imageFiles =
{ "Left.gif", "Right.gif", "RotCCUp.gif",
"TrafficRed.gif", "Home.gif", "Print.gif", "Help.gif" };
String[] toolbarLabels =
{ "Back", "Forward", "Reload", "Stop",
"Home", "Print", "Help" };
Insets margins = new Insets(0, 0, 0, 0);
for(int i=0; i<toolbarlabels .length; i++) {
ToolBarButton button =
new ToolBarButton("images/" + imageFiles[i]);
button.setToolTipText(toolbarLabels[i]);
button.setMargin(margins);
add(button);
}
}
public void setTextLabels(boolean labelsAreEnabled) {
Component c;
int i = 0;
while((c = getComponentAtIndex(i++)) != null) {
ToolBarButton button = (ToolBarButton)c;
if (labelsAreEnabled) {
button.setText(button.getToolTipText());
} else {
button.setText(null);
}
}
}
}
Note: Brought from our old site: http://www.salearningschool.com/example_codes/ on Jan 2nd, 2017 From: http://sitestree.com/?p=10314
Categories:Programming Code Examples, Java/J2EE/J2ME, Basic Swing
Tags:Java/J2EE/J2MEBasic Swing
Post Data:2017-01-02 16:04:31
Shop Online: https://www.ShopForSoul.com/
(Big Data, Cloud, Security, Machine Learning): Courses: http://Training.SitesTree.com
In Bengali: http://Bangla.SaLearningSchool.com
http://SitesTree.com
8112223 Canada Inc./JustEtc: http://JustEtc.net (Software/Web/Mobile/Big-Data/Machine Learning)
Shop Online: https://www.ShopForSoul.com/
Medium: https://medium.com/@SayedAhmedCanada
May 05
A simple button that contains an image and a label for use in a toolbar #Programming Code Examples #Java/J2EE/J2ME #Basic Swing
ToolBarButton.java A simple button that contains an image and a label for use in a toolbar.
import java.awt.*;
import javax.swing.*;
/** Part of a small example showing basic use of JToolBar.
* The point here is that dropping a regular JButton in a
* JToolBar (or adding an Action) in JDK 1.2 doesn't give
* you what you want -- namely, a small button just enclosing
* the icon, and with text labels (if any) below the icon,
* not to the right of it. In JDK 1.3, if you add an Action
* to the toolbar, the Action label is no longer displayed.
*
*/
public class ToolBarButton extends JButton {
private static final Insets margins =
new Insets(0, 0, 0, 0);
public ToolBarButton(Icon icon) {
super(icon);
setMargin(margins);
setVerticalTextPosition(BOTTOM);
setHorizontalTextPosition(CENTER);
}
public ToolBarButton(String imageFile) {
this(new ImageIcon(imageFile));
}
public ToolBarButton(String imageFile, String text) {
this(new ImageIcon(imageFile));
setText(text);
}
}
Note: Brought from our old site: http://www.salearningschool.com/example_codes/ on Jan 2nd, 2017 From: http://sitestree.com/?p=10313
Categories:Programming Code Examples, Java/J2EE/J2ME, Basic Swing
Tags:Java/J2EE/J2MEBasic Swing
Post Data:2017-01-02 16:04:31
Shop Online: https://www.ShopForSoul.com/
(Big Data, Cloud, Security, Machine Learning): Courses: http://Training.SitesTree.com
In Bengali: http://Bangla.SaLearningSchool.com
http://SitesTree.com
8112223 Canada Inc./JustEtc: http://JustEtc.net (Software/Web/Mobile/Big-Data/Machine Learning)
Shop Online: https://www.ShopForSoul.com/
Medium: https://medium.com/@SayedAhmedCanada
May 05
mall example showing the basic use of a JToolBar #Programming Code Examples #Java/J2EE/J2ME #Basic Swing
mall example showing the basic use of a JToolBar
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
/** Small example showing basic use of JToolBar.
*
*
*/
public class JToolBarExample extends JFrame
implements ItemListener {
private BrowserToolBar toolbar;
private JCheckBox labelBox;
public static void main(String[] args) {
new JToolBarExample();
}
public JToolBarExample() {
super("JToolBar Example");
WindowUtilities.setNativeLookAndFeel();
addWindowListener(new ExitListener());
Container content = getContentPane();
content.setBackground(Color.white);
JPanel panel = new JPanel(new BorderLayout());
labelBox = new JCheckBox("Show Text Labels?");
labelBox.setHorizontalAlignment(SwingConstants.CENTER);
labelBox.addItemListener(this);
panel.add(new JTextArea(10,30), BorderLayout.CENTER);
panel.add(labelBox, BorderLayout.SOUTH);
toolbar = new BrowserToolBar();
content.add(toolbar, BorderLayout.NORTH);
content.add(panel, BorderLayout.CENTER);
pack();
setVisible(true);
}
public void itemStateChanged(ItemEvent event) {
toolbar.setTextLabels(labelBox.isSelected());
pack();
}
}
Note: Brought from our old site: http://www.salearningschool.com/example_codes/ on Jan 2nd, 2017 From: http://sitestree.com/?p=10312
Categories:Programming Code Examples, Java/J2EE/J2ME, Basic Swing
Tags:Java/J2EE/J2MEBasic Swing
Post Data:2017-01-02 16:04:31
Shop Online: https://www.ShopForSoul.com/
(Big Data, Cloud, Security, Machine Learning): Courses: http://Training.SitesTree.com
In Bengali: http://Bangla.SaLearningSchool.com
http://SitesTree.com
8112223 Canada Inc./JustEtc: http://JustEtc.net (Software/Web/Mobile/Big-Data/Machine Learning)
Shop Online: https://www.ShopForSoul.com/
Medium: https://medium.com/@SayedAhmedCanada
May 05
Demonstrates the use of a JColorChooser which presents a dialog with three different tabbed panes to allow the user to select a color preference #Programming Code Examples #Java/J2EE/J2ME #Basic Swing
Demonstrates the use of a JColorChooser which presents a dialog with three different tabbed panes to allow the user to select a color preference. The dialog returns a Color object based on the user's selection or null if the user entered Cancel.
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
/** Simple example illustrating the use of internal frames.
*
* */
public class JInternalFrames extends JFrame {
public static void main(String[] args) {
new JInternalFrames();
}
public JInternalFrames() {
super("Multiple Document Interface");
WindowUtilities.setNativeLookAndFeel();
addWindowListener(new ExitListener());
Container content = getContentPane();
content.setBackground(Color.white);
JDesktopPane desktop = new JDesktopPane();
desktop.setBackground(Color.white);
content.add(desktop, BorderLayout.CENTER);
setSize(450, 400);
for(int i=0; i<5; i++) {
JInternalFrame frame
= new JInternalFrame(("Internal Frame " + i),
true, true, true, true);
frame.setLocation(i*50+10, i*50+10);
frame.setSize(200, 150);
frame.setBackground(Color.white);
frame.setVisible(true);
desktop.add(frame);
frame.moveToFront();
}
setVisible(true);
}
}
Note: Brought from our old site: http://www.salearningschool.com/example_codes/ on Jan 2nd, 2017 From: http://sitestree.com/?p=10311
Categories:Programming Code Examples, Java/J2EE/J2ME, Basic Swing
Tags:Java/J2EE/J2MEBasic Swing
Post Data:2017-01-02 16:04:31
Shop Online: https://www.ShopForSoul.com/
(Big Data, Cloud, Security, Machine Learning): Courses: http://Training.SitesTree.com
In Bengali: http://Bangla.SaLearningSchool.com
http://SitesTree.com
8112223 Canada Inc./JustEtc: http://JustEtc.net (Software/Web/Mobile/Big-Data/Machine Learning)
Shop Online: https://www.ShopForSoul.com/
Medium: https://medium.com/@SayedAhmedCanada
