Tags » Java Programming

A Few Java Factoids Part 1

I’m taking a Java class right now and sometimes just looking at code is really great to get the learning started.

Meant for beginners, I’ve come up with a few basic code samples to showcase as a convenient way of getting to know Java. 371 more words

Java

Streamadda demonstrates events in swing

Program to view event methods of swing


import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class StreamSwingEvent {

private JFrame mainFrame;
private JLabel headerLabel;
private JLabel statusLabel;
private JPanel controlPanel;

public StreamSwingEvent(){
prepareGUI();
}

public static void main(String[] args){
StreamSwingEvent StreamSwingEvent = new StreamSwingEvent();
StreamSwingEvent.showEventDemo();
}

private void prepareGUI(){
mainFrame = new JFrame("Stream is demosntrating swing events");
mainFrame.setSize(400,400);
mainFrame.setLayout(new GridLayout(3, 1));

headerLabel = new JLabel("",JLabel.LEFT );
statusLabel = new JLabel("",JLabel.LEFT);

statusLabel.setSize(350,100);
mainFrame.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent windowEvent){
System.exit(0);
}
});
controlPanel = new JPanel();
controlPanel.setLayout(new FlowLayout());

mainFrame.add(headerLabel);
mainFrame.add(controlPanel);
mainFrame.add(statusLabel);
mainFrame.setVisible(true);
}

private void showEventDemo(){
headerLabel.setText("You are view Streamadda's course");

JButton JavaButton = new JButton("Java");
JButton BIButton = new JButton("BI");
JButton HadoopButton = new JButton("Hadoop");

JavaButton.setActionCommand("Java");
BIButton.setActionCommand("BI");
HadoopButton.setActionCommand("Hadoop");

JavaButton.addActionListener(new ButtonClickListener());
BIButton.addActionListener(new ButtonClickListener());
HadoopButton.addActionListener(new ButtonClickListener());

controlPanel.add(JavaButton);
controlPanel.add(BIButton);
controlPanel.add(HadoopButton);

mainFrame.setVisible(true);
}

private class ButtonClickListener implements ActionListener{
public void actionPerformed(ActionEvent e) {
String command = e.getActionCommand();
if( command.equals( "Java" ))  {
statusLabel.setText("You are selecting Java.");
}
else if( command.equals( "BI" ) )  {
statusLabel.setText("You are selecting BI.");
}
else  {
statusLabel.setText("You are selecting Hadoop.");
}
}
}
}

Output of the program

Core Java

Streamadda demostrates examples of JDBC and its methods

Program JDBC Programming


import java.sql.DriverManager;

import java.sql.Connection;

import java.sql.SQLException;

public class StreamJDBCDriver {

   public static void main(String[] argv) {

       System.out.println("-------- Streamadda connecting for JDBC testing ------");

       try {

           Class.forName("oracle.jdbc.driver.OracleDriver");

       } catch (ClassNotFoundException e) {

            System.out.println("Unable to find JDBC Driver");

           e.printStackTrace();

           return;

       }

       System.out.println("Successfully installed JDBC driver");
       Connection connection = null;
       try {

          connection = DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:xe", "hr", "hr");
       } catch (SQLException e) {
           System.out.println("Connection Failed! 93 more words
Core Java

Ready For Study & Best Logics ???

Get Ready 4 Learning About Java Language ..?

Streamadda explores on email API

Below program contains sample email methods


import java.awt.*;
import javax.swing.*;

public class StreamMail extends JFrame {
public StreamMail() {
super("New Message");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(580, 410);
// create menu
JMenu fileMenu = new JMenu("File");
JMenu editMenu = new JMenu("Edit");
JMenu viewMenu = new JMenu("View");
JMenu insertMenu = new JMenu("Insert");
JMenu messageMenu = new JMenu("Message");
JMenu formatMenu = new JMenu("Format");
JMenu helpMenu = new JMenu("Help");
JMenuBar menuBar = new JMenuBar();
menuBar.add(fileMenu);
menuBar.add(editMenu);
menuBar.add(viewMenu);
menuBar.add(insertMenu);
menuBar.add(messageMenu);
menuBar.add(formatMenu);
menuBar.add(helpMenu);
setJMenuBar(menuBar);
// create toolbar
ImageIcon sendIcon = new ImageIcon("send.gif");
ImageIcon queueIcon = new ImageIcon("queue.gif");
ImageIcon draftIcon = new ImageIcon("draft.gif");
ImageIcon attachIcon = new ImageIcon("attach.gif");
ImageIcon styledIcon = new ImageIcon("styled.gif");
ImageIcon spellingIcon = new ImageIcon("spelling.gif");
ImageIcon backIcon = new ImageIcon("back.gif");
JButton send = new JButton("Send", sendIcon);
JButton queue = new JButton("Queue", queueIcon);
JButton draft = new JButton("Draft", draftIcon);
JButton attach = new JButton("Attach", attachIcon);
JButton styled = new JButton("Styled", styledIcon);
JButton spelling = new JButton("Spelling", spellingIcon);
JButton back = new JButton("Back", backIcon);
JToolBar toolBar = new JToolBar();
toolBar.add(send);
toolBar.add(queue);
toolBar.add(draft);
toolBar.add(attach);
toolBar.add(styled);
toolBar.add(spelling);
toolBar.add(back);
setLayout(new BorderLayout());
MainPanel mainPanel = new MainPanel();
add("North", toolBar);
add("Center", mainPanel);
SwingUtilities.updateComponentTreeUI(this);
setVisible(true);
}

public static void main(String[] arguments) {
String[] lookAndFeel = {
"javax.swing.plaf.metal.MetalLookAndFeel",
"com.sun.java.swing.plaf.mac.MacLookAndFeel",
"com.sun.java.swing.plaf.motif.MotifLookAndFeel",
"com.sun.java.swing.plaf.windows.WindowsLookAndFeel",
"com.sun.java.swing.plaf.windows.WindowsClassicLookAndFeel"
};
if (arguments.length < 1) {
System.out.println("Usage: java NewEmail lookAndFeelNumber\n" +
"0: Metal\n1: Mac\n2: Motif\n3: Windows older \n4: Windows 8");
System.exit(-1);
}
try {
int choice = Integer.parseInt(arguments[0]);
UIManager.setLookAndFeel(lookAndFeel);
} catch (Exception e) {
System.out.println("Can't set look and feel: " + e.getMessage());
e.printStackTrace();
}
new Email();
System.out.println("Available look and feels:\n");
UIManager.LookAndFeelInfo[] laf = UIManager.getInstalledLookAndFeels();
for (int i = 0; i < laf.length; i++) {
System.out.println("Class name: " + laf[i].getClassName());
System.out.println("Name: " + laf[i].getName() + "\n");
}
}
}

class MainPanel extends JPanel {
MainPanel() {
// create button bar
JPanel topPanel = new JPanel();
topPanel.setLayout(new FlowLayout(FlowLayout.LEFT));
JLabel fromLabel = new JLabel("From: ");
JComboBox addressBox = new JComboBox();
addressBox.addItem("rcade@example.com");
ImageIcon modifyIcon = new ImageIcon("modify.gif");
ImageIcon templatesIcon = new ImageIcon("templates.gif");
JButton modify = new JButton("Modify Accounts", modifyIcon);
JButton templates = new JButton("Templates", templatesIcon);
topPanel.add(fromLabel);
topPanel.add(addressBox);
topPanel.add(modify);
topPanel.add(templates);
MailPanel mailPanel = new MailPanel();
setLayout(new BorderLayout());
add("North", topPanel);
add("Center", mailPanel);
}
}

class MailPanel extends JTabbedPane {
MailPanel() {
super();
// set up new message entry form
JPanel mailTab = new JPanel();
mailTab.setLayout(new BorderLayout());
JTextArea comments = new JTextArea();
JScrollPane textPanel = new JScrollPane(comments,
ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS,
ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
mailTab.add("North", new MailFormPanel());
mailTab.add("Center", textPanel);
// add entry form to tabbed pane
addTab("Main", mailTab);
addTab("Optional", new JPanel());
}
}

class MailFormPanel extends JPanel {
GridBagLayout gridbag = new GridBagLayout();
GridBagConstraints constraints;

MailFormPanel() {
super();
JLabel toLabel = new JLabel("To: ");
JTextField to = new JTextField();
JLabel subjectLabel = new JLabel("Subject: ");
JTextField subject = new JTextField();
JLabel ccLabel = new JLabel("CC: ");
JTextField cc = new JTextField();
JLabel bccLabel = new JLabel("BCC: ");
JTextField bcc = new JTextField();
setLayout(gridbag);
addComponent(toLabel, 0, 0, 1, 1, 5, 100,
GridBagConstraints.NONE, GridBagConstraints.EAST);
addComponent(to, 1, 0, 9, 1, 195, 100,
GridBagConstraints.HORIZONTAL, GridBagConstraints.WEST);
addComponent(subjectLabel, 0, 1, 1, 1, 5, 100,
GridBagConstraints.NONE, GridBagConstraints.EAST);
addComponent(subject, 1, 1, 9, 1, 195, 100,
GridBagConstraints.HORIZONTAL, GridBagConstraints.WEST);
addComponent(ccLabel, 0, 2, 1, 1, 5, 100,
GridBagConstraints.NONE, GridBagConstraints.EAST);
addComponent(cc, 1, 2, 4, 1, 95, 100,
GridBagConstraints.HORIZONTAL, GridBagConstraints.WEST);
addComponent(bccLabel, 5, 2, 1, 1, 5, 100,
GridBagConstraints.NONE, GridBagConstraints.EAST);
addComponent(bcc, 6, 2, 4, 1, 95, 100,
GridBagConstraints.HORIZONTAL, GridBagConstraints.WEST);
}

private void addComponent(Component component, int gridx, int gridy,
int gridwidth, int gridheight, int weightx, int weighty, int fill,
int anchor) {

GridBagConstraints constraints = new GridBagConstraints();
constraints.gridx = gridx;
constraints.gridy = gridy;
constraints.gridwidth = gridwidth;
constraints.gridheight = gridheight;
constraints.weightx = weightx;
constraints.weighty = weighty;
constraints.fill = fill;
constraints.anchor = anchor;
gridbag.setConstraints(component, constraints);
add(component);
}

public Insets getInsets() {
return new Insets(2, 2, 2, 2);
}
}

… 412 more words
Core Java

Streadadda explores on Networking programs

Below program is split into server side programming and client side programming.

Server Program

// Server side program
import java.net.*;
import java.io.*;
//Server side main class
public class ServerSideProg {
//Main method of the program
public static void main(String args[]) throws IOException {
// Opening a server socket on port 12345
ServerSocket s = new ServerSocket(12345);
//Waiting for request from client side
Socket s1=s.accept(); 
// creating input streams for communication between server and client side
OutputStream s1out = s1.getOutputStream();
DataOutputStream dos = new DataOutputStream (s1out);
// Server message
dos.writeUTF("Hi, Welcome to Streamadda");
// Closing communication socket
dos.close();
s1out.close();
s1.close();
}
}
… 92 more words
Java

Streamadda explores on serialization

Program to create main method of the sample student program used to demonstrate serialization


import java.io.Serializable;

public class Student implements Serializable {

private static final long serialVersionUID = 1L;

int StudentId;

String StudentName;

String course;

public int getStudentId() {

return StudentId;

}

public void setStudentId(int StudentId) {

this.StudentId = StudentId;

}

public String getStudentName() {

return StudentName;

}

public void setStudentName(String StudentName) {

this.StudentName = StudentName;

}

public String getcourse() {

return course;

}

public void setcourse(String course) {

this.course = course;

}

}

… 189 more words
Core Java