Monday, 31 December 2012

Swing Slider:- Slider Program With Tick Marks


import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import javax.swing.event.*;
/*
<APPLET
CODE=slider_tick.class
WIDTH=300
HEIGHT=200 >
</APPLET>
*/
public class slider_tick extends JApplet implements ChangeListener
{
JSlider jslider = new JSlider(SwingConstants.HORIZONTAL, 0, 100, 0);
public void init()
{
Container contentPane = getContentPane();
contentPane.setLayout(new FlowLayout());
jslider.addChangeListener(this);
jslider.setPaintTicks(true);
jslider.setMajorTickSpacing(20);
jslider.setMinorTickSpacing(10);
contentPane.add(jslider);
}
public void stateChanged(ChangeEvent e)
{
JSlider jslider1 = (JSlider) e.getSource();
showStatus("Slider minimum: " + jslider1.getMinimum() +
", maximum: " + jslider1.getMaximum() +
", value: " + jslider1.getValue());
}
}

Swing Slider:- Slider Program 2


import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import javax.swing.event.*;
/*
<APPLET
CODE=fill_slider.class
WIDTH=300
HEIGHT=200 >
</APPLET>
*/
public class fill_slider extends JApplet implements ChangeListener
{
JSlider jslider = new JSlider(SwingConstants.HORIZONTAL, 0, 100, 0);
public void init()
{
Container contentPane = getContentPane();
contentPane.setLayout(new FlowLayout());
jslider.addChangeListener(this);
jslider.putClientProperty("JSlider.isFilled", Boolean.TRUE);
contentPane.add(jslider);
}
public void stateChanged(ChangeEvent e)
{
JSlider jslider1 = (JSlider) e.getSource();
showStatus("Slider minimum: " + jslider1.getMinimum() +
", maximum: " + jslider1.getMaximum() +
", value: " + jslider1.getValue());
}
}

Swing :- Slider Program


import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import javax.swing.event.*;
/*
<APPLET
CODE=slider.class
WIDTH=300
HEIGHT=200 >
</APPLET>
*/
public class slider extends JApplet implements ChangeListener
{
JSlider jslider = new JSlider(SwingConstants.HORIZONTAL, 0, 100, 0);
public void init()
{
Container contentPane = getContentPane();
contentPane.setLayout(new FlowLayout());
jslider.addChangeListener(this);
contentPane.add(jslider);
}
public void stateChanged(ChangeEvent e)
{
JSlider jslider1 = (JSlider) e.getSource();
showStatus("Slider minimum: " + jslider1.getMinimum() +
", maximum: " + jslider1.getMaximum() +
", value: " + jslider1.getValue());
}
}

Swing ScrollPane:-Scroll Image


import java.awt.*;
import javax.swing.*;
/*
<APPLET
CODE=scrollpaneimage.class
WIDTH=300
HEIGHT=200 >
</APPLET>
*/
public class scrollpaneimage extends JApplet
{
public scrollpaneimage()
{
Container contentPane = getContentPane();
JLabel jlabel = new JLabel(new ImageIcon("Source packages\bulg.jpg"));
JScrollPane jscrollpane = new JScrollPane(jlabel);
contentPane.add(jscrollpane);
}
}

Swing ScrollPane:- Scroll Pane


import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.GridLayout;
import javax.swing.BorderFactory;
import javax.swing.JApplet;
import javax.swing.JLabel;
import javax.swing.JScrollPane;
import javax.swing.JTextField;
import javax.swing.ScrollPaneConstants;

/**
 *
 * @author Amol
 */
public class MyScrollPane extends JApplet {
public  void  init(){
        Container contentPane = getContentPane();
        contentPane.setLayout(new BorderLayout(11, 16));
        jpanel jpanel=new jpanel();
        for(int outer =0; outer<=10;outer++ ){
            for(int inner =0 ; inner<=15; inner++){
                jpanel.add(new JTextField("Text Field " + outer +", " + inner));


            }
            JScrollPane jScrollPane =new JScrollPane(jpanel,ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS,ScrollPaneConstants.HORIZONTAL_SCROLLBAR_ALWAYS);
            JLabel jLabel1 = new JLabel("Horizantal label");
            JLabel jLabel2 = new JLabel("Vertical Label");
            jScrollPane.setColumnHeaderView(jLabel1);
            jScrollPane.setRowHeaderView(jLabel2);
            jScrollPane.setViewportBorder(BorderFactory.createEtchedBorder());
            contentPane.add(jScrollPane);


        }
}
}

Swing Scroll Panes


import java.awt.*;
import javax.swing.*;
/*
<APPLET
CODE=scrollpane.class
WIDTH=300
HEIGHT=200 >
</APPLET>
*/
public class scrollpane extends JApplet
{
public void init()
{
Container contentPane = getContentPane();
JPanel jpanel = new JPanel();
jpanel.setLayout(new GridLayout(11, 16));
for(int outer = 0; outer <= 10; outer++) {
for(int inner = 0; inner <= 15; inner++) {
jpanel.add(new JTextField("Text Field " + outer +
", " + inner));
}
}
JScrollPane jscrollpane = new JScrollPane(jpanel,
ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS,
ScrollPaneConstants.HORIZONTAL_SCROLLBAR_ALWAYS);
contentPane.add(jscrollpane);
}
}

Swing Viewport Class


import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
/*
<APPLET
CODE=viewport.class
WIDTH=500
HEIGHT=200 >
</APPLET>
*/
public class viewport extends JApplet
{
public viewport()
{
Container contentPane = getContentPane();
JViewport jviewport = new JViewport();
JPanel jpanel = new JPanel();
jpanel.add(new JLabel(new ImageIcon("illus.jpg")));
jviewport.setView(jpanel);
contentPane.add(jviewport, BorderLayout.CENTER);
contentPane.add(new buttonpanel(jviewport), BorderLayout.SOUTH);
}
}

class buttonpanel extends JPanel implements ActionListener
{
JViewport jviewport;
JButton button1 = new JButton("Scroll left");
JButton button2 = new JButton("Scroll up");
JButton button3 = new JButton("Scroll down");
JButton button4 = new JButton("Scroll right");
public buttonpanel(JViewport vport)
{
jviewport = vport;
add(button1);
add(button2);
add(button3);
add(button4);
button1.addActionListener(this);
button2.addActionListener(this);
button3.addActionListener(this);
button4.addActionListener(this);
}
public void actionPerformed(ActionEvent e)
{
Point position = jviewport.getViewPosition();
if(e.getSource() == button1) position.x += 10;
else if(e.getSource() == button2) position.y += 10;
else if(e.getSource() == button3) position.y -= 10;
else if(e.getSource() == button4) position.x -= 10;
jviewport.setViewPosition(position);
}

}

Swing Button:- Click Magic Program


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

public class ClickMe extends JFrame
{
public static void main(String [] args)
{
new ClickMe();
}

private JButton button1, exitButton;

public ClickMe()
{
   this.setSize(275,100);
   this.setTitle("I am counting!!");
   this.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);

ClickListener cl = new ClickListener();

   JPanel panel1 = new JPanel();

   addWindowListener(new WindowAdapter()
    {
public void windowClosing(WindowEvent e)
{
exitButton.doClick();
}
} );

button1 = new JButton("Press Enter or Click Mouse!");
button1.setMnemonic('P');
getRootPane().setDefaultButton(button1);
button1.addActionListener(cl);
   panel1.add(button1);

   exitButton = new JButton("EXIT");
   exitButton.addActionListener(cl);
   panel1.add(exitButton);

   this.add(panel1);

   this.setVisible(true);
}

private class ClickListener implements ActionListener
{
private int clickCount = 0;

public void actionPerformed(ActionEvent e)
{
if (e.getSource() == button1)
{
clickCount++;
if (clickCount == 1)
button1.setText("You clicked Me!");
   else
button1.setText("You clicked "
+ clickCount + " times!");
}
else if (e.getSource() == exitButton)
{
if (clickCount > 0)
System.exit(0);
else
{
JOptionPane.showMessageDialog(ClickMe.this,
   "...try clicking once!!",
   "In a hurry...!!",
   JOptionPane.ERROR_MESSAGE);
}
}
}
}
}

Swing Button :- Button Click Program


import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
/* <APPLET
CODE=defaultbutton.class
WIDTH=300
HEIGHT=200 >
</APPLET>*/
public class defaultbutton extends JApplet {
JButton button1 = new JButton("Click Me");
JButton button2 = new JButton("Click Me");
JTextField text = new JTextField(20);
public void init()
{
Container contentPane = getContentPane();
contentPane.setLayout(new FlowLayout());
button2.setMnemonic('C');
getRootPane().setDefaultButton(button2);
contentPane.add(button1);
contentPane.add(button2);
contentPane.add(text);
getRootPane().requestFocus();
button1.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent event)
{
text.setText("Hello from Swing!");
}
});
button2.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent event)

{
text.setText("Hello from Swing!");
}
});
}
}

Swing Button :- Button Icon Program


import java.awt.*;
import javax.swing.*;
/* <APPLET
CODE=buttonicons.class
WIDTH=300
HEIGHT=200 >
</APPLET>*/
public class buttonicons extends JApplet
{
public void init()
{
Container contentPane = getContentPane();
Icon normal = new ImageIcon("rollover 1.jpg");
Icon rollover = new ImageIcon("rollover 2.jpg");
Icon pressed = new ImageIcon("rollover 3.jpg");

JButton jbutton = new JButton();
jbutton.setRolloverEnabled(true);
jbutton.setIcon(normal);
jbutton.setRolloverIcon(rollover);

jbutton.setPressedIcon(pressed);
contentPane.setLayout(new FlowLayout());
contentPane.add(jbutton);
}
}

Swing :- Warning Option Program


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

public class Salutation extends JFrame
{
public static void main(String [] args)
{
new Salutation();
}

private JButton buttonOK;
private JTextField textName;

public Salutation()
{
   this.setSize(325,100);
   this.setTitle("Identify yourself");
   this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

ButtonListener bl = new ButtonListener();

   JPanel panel1 = new JPanel();

panel1.add(new JLabel("Enter your name: "));

textName = new JTextField(15);
panel1.add(textName);

buttonOK = new JButton("OK");
buttonOK.addActionListener(bl);
   panel1.add(buttonOK);

   this.add(panel1);

   this.setVisible(true);
}

private class ButtonListener implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
if (e.getSource() == buttonOK)
{
String name = textName.getText();
if (name.length() == 0)
{
JOptionPane.showMessageDialog(Salutation.this,
   "Please don't leave the box empty!",
   "Empty!!",
   JOptionPane.INFORMATION_MESSAGE);
}
else
{
JOptionPane.showMessageDialog(Salutation.this,
   "Good morning " + textName.getText(),
   "Greetings to you",
   JOptionPane.INFORMATION_MESSAGE);
}
textName.requestFocus();
}
}
}
}

Wednesday, 26 December 2012

Swing Check and Radio Buttons


import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
/* <APPLET
CODE=checkselected.class
WIDTH=500
HEIGHT=90 >
</APPLET> */
public class checkselected extends JApplet implements ItemListener {
JCheckBox checks[];
JTextField text;
public void init() {
Container contentPane = getContentPane();
contentPane.setLayout(new FlowLayout());
checks = new JCheckBox[5];
for(int loop_index = 1; loop_index <= checks.length - 1; loop_index++){
checks[loop_index] = new JCheckBox("Check " + loop_index);
checks[loop_index].addItemListener(this);
contentPane.add(checks[loop_index]);
}
text = new JTextField(40);
contentPane.add(text);
}
public void itemStateChanged(ItemEvent e)
{
String outString = new String("Currently selected:\n");

for(int loop_index = 1; loop_index <= checks.length - 1; loop_index++){
if(checks[loop_index].isSelected()) {
outString += " check box " + loop_index;
}
}
text.setText(outString);
}

}

Swing Radio Buttons


import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
/* <APPLET
CODE=radiogroup.class
WIDTH=340
HEIGHT=200 >
</APPLET> */
public class radiogroup extends JApplet implements ItemListener {
JRadioButton radio1, radio2, radio3, radio4;
ButtonGroup group;
JTextField text;
public void init() {
Container contentPane = getContentPane();
contentPane.setLayout(new FlowLayout());
group = new ButtonGroup();
radio1 = new JRadioButton("Radio 1");
radio2 = new JRadioButton("Radio 2");
radio3 = new JRadioButton("Radio 3");
radio4 = new JRadioButton("Radio 4");
group.add(radio1);
group.add(radio2);
group.add(radio3);
group.add(radio4);
radio1.addItemListener(this);
radio2.addItemListener(this);
radio3.addItemListener(this);
radio4.addItemListener(this);
contentPane.add(radio1);
contentPane.add(radio2);
contentPane.add(radio3);
contentPane.add(radio4);
text = new JTextField(20);
contentPane.add(text);
}
public void itemStateChanged(ItemEvent e) {

if (e.getItemSelectable() == radio1) {
text.setText("You clicked radio button 1.");
} else if (e.getItemSelectable() == radio2) {
text.setText("You clicked radio button 2.");
} else if (e.getItemSelectable() == radio3) {
text.setText("You clicked radio button 3.");
} else if (e.getItemSelectable() == radio4) {
text.setText("You clicked radio button 4.");
}
}
}

Swing CheckBox


import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
/* <APPLET
CODE=checkbox.class
WIDTH=340
HEIGHT=200 >
</APPLET>*/
public class checkbox extends JApplet implements ItemListener
{
JCheckBox check1, check2, check3, check4;
JTextField text;
public void init()
{
Container contentPane = getContentPane();
contentPane.setLayout(new FlowLayout());
check1 = new JCheckBox("Check 1");
check2 = new JCheckBox("Check 2");
check3 = new JCheckBox("Check 3");
check4 = new JCheckBox("Check 4");
check1.addItemListener(this);
check2.addItemListener(this);
check3.addItemListener(this);
check4.addItemListener(this);
contentPane.add(check1);
contentPane.add(check2);
contentPane.add(check3);
contentPane.add(check4);
text = new JTextField(20);
contentPane.add(text);
}
public void itemStateChanged(ItemEvent e)
{
if (e.getItemSelectable() == check1) {
text.setText("You clicked check box 1.");

} else if (e.getItemSelectable() == check2) {
text.setText("You clicked check box 2.");

} else if (e.getItemSelectable() == check3) {
text.setText("You clicked check box 3.");

} else if (e.getItemSelectable() == check4) {
text.setText("You clicked check box 4.");

}
}
}

Swing Button Toggle Group Apps


import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
/* <APPLET
CODE=togglegroup.class
WIDTH=300
HEIGHT=520 >
</APPLET>*/
public class togglegroup extends JApplet {
public togglegroup() {
Container contentPane = getContentPane();
ButtonGroup group = new ButtonGroup();
JToggleButton[] buttons = new JToggleButton[] {
new JToggleButton(new ImageIcon("toggle.jpg")),
new JToggleButton(new ImageIcon("toggle.jpg")),
new JToggleButton(new ImageIcon("toggle.jpg")),
new JToggleButton(new ImageIcon("toggle.jpg")),
new JToggleButton(new ImageIcon("toggle.jpg"))
};
contentPane.setLayout(new FlowLayout());
for(int i=0; i < buttons.length; ++i) {
group.add(buttons[i]);
contentPane.add(buttons[i]);
}
}
}

Swing Button Toggle


import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
/* <APPLET
CODE=toggle.class
WIDTH=450
HEIGHT=450 >
</APPLET>*/
public class toggle extends JApplet {
public toggle() {
Container contentPane = getContentPane();
Icon icon = new ImageIcon("toggle.jpg");
JToggleButton toggle1 = new JToggleButton(icon);
JToggleButton toggle2 = new JToggleButton(icon, true);
JToggleButton toggle3 = new JToggleButton("Toggle Me!");
JToggleButton toggle4 = new JToggleButton("Toggle Me!", icon);
JToggleButton toggle5 = new JToggleButton("Toggle Me!", icon,
true);
contentPane.setLayout(new FlowLayout());
contentPane.add(toggle1);
contentPane.add(toggle2);
contentPane.add(toggle3);
contentPane.add(toggle4);
contentPane.add(toggle5);
}
}

Swing Button Click Magic Apps


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

public class ClickMe extends JFrame
{
public static void main(String [] args)
{
new ClickMe();
}

private JButton button1, exitButton;

public ClickMe()
{
   this.setSize(275,100);
   this.setTitle("I am counting!!");
   this.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);

ClickListener cl = new ClickListener();

   JPanel panel1 = new JPanel();

   addWindowListener(new WindowAdapter()
    {
public void windowClosing(WindowEvent e)
{
exitButton.doClick();
}
} );

button1 = new JButton("Press Enter or Click Mouse!");
button1.setMnemonic('P');
getRootPane().setDefaultButton(button1);
button1.addActionListener(cl);
   panel1.add(button1);

   exitButton = new JButton("EXIT");
   exitButton.addActionListener(cl);
   panel1.add(exitButton);

   this.add(panel1);

   this.setVisible(true);
}

private class ClickListener implements ActionListener
{
private int clickCount = 0;

public void actionPerformed(ActionEvent e)
{
if (e.getSource() == button1)
{
clickCount++;
if (clickCount == 1)
button1.setText("You clicked Me!");
   else
button1.setText("You clicked "
+ clickCount + " times!");
}
else if (e.getSource() == exitButton)
{
if (clickCount > 0)
System.exit(0);
else
{
JOptionPane.showMessageDialog(ClickMe.this,
   "...try clicking once!!",
   "In a hurry...!!",
   JOptionPane.ERROR_MESSAGE);
}
}
}
}
}





Swing Default Button


import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
/* <APPLET
CODE=defaultbutton.class
WIDTH=300
HEIGHT=200 >
</APPLET>*/
public class defaultbutton extends JApplet {
JButton button1 = new JButton("Click Me");
JButton button2 = new JButton("Click Me");
JTextField text = new JTextField(20);
public void init()
{
Container contentPane = getContentPane();
contentPane.setLayout(new FlowLayout());
button2.setMnemonic('C');
getRootPane().setDefaultButton(button2);
contentPane.add(button1);
contentPane.add(button2);
contentPane.add(text);
getRootPane().requestFocus();
button1.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent event)
{
text.setText("Hello from Swing!");
}
});
button2.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent event)

{
text.setText("Hello from Swing!");
}
});
}
}

Swing :- Button Icon


import java.awt.*;
import javax.swing.*;
/* <APPLET
CODE=buttonicons.class
WIDTH=300
HEIGHT=200 >
</APPLET>*/
public class buttonicons extends JApplet
{
public void init()
{
Container contentPane = getContentPane();
Icon normal = new ImageIcon("rollover 1.jpg");
Icon rollover = new ImageIcon("rollover 2.jpg");
Icon pressed = new ImageIcon("rollover 3.jpg");

JButton jbutton = new JButton();
jbutton.setRolloverEnabled(true);
jbutton.setIcon(normal);
jbutton.setRolloverIcon(rollover);

jbutton.setPressedIcon(pressed);
contentPane.setLayout(new FlowLayout());
contentPane.add(jbutton);
}
}

Java Swing :- Image on Button Program


import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
/*<APPLET
CODE=imagebutton.class
WIDTH=500
HEIGHT=200 >
</APPLET>*/
public class imagebutton extends JApplet {
JButton button = new JButton("Button", new ImageIcon("buttons.jpg"));
JTextField text = new JTextField(20);
public void init()
{
Container contentPane = getContentPane();
contentPane.setLayout(new FlowLayout());
contentPane.add(button);
contentPane.add(text);
button.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent event) {
text.setText("Image on Buttons from Swing!");
}
});
}
}

Swing Button Apps


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

public class Salutation extends JFrame
{
public static void main(String [] args)
{
new Salutation();
}

private JButton buttonOK;
private JTextField textName;

public Salutation()
{
   this.setSize(325,100);
   this.setTitle("Identify yourself");
   this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

ButtonListener bl = new ButtonListener();

   JPanel panel1 = new JPanel();

panel1.add(new JLabel("Enter your name: "));

textName = new JTextField(15);
panel1.add(textName);

buttonOK = new JButton("OK");
buttonOK.addActionListener(bl);
   panel1.add(buttonOK);

   this.add(panel1);

   this.setVisible(true);
}

private class ButtonListener implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
if (e.getSource() == buttonOK)
{
String name = textName.getText();
if (name.length() == 0)
{
JOptionPane.showMessageDialog(Salutation.this,
   "Please don't leave the box empty!",
   "Empty!!",
   JOptionPane.INFORMATION_MESSAGE);
}
else
{
JOptionPane.showMessageDialog(Salutation.this,
   "Good morning " + textName.getText(),
   "Greetings to you",
   JOptionPane.INFORMATION_MESSAGE);
}
textName.requestFocus();
}
}
}
}

Swing Button Program


import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
/*<APPLET
CODE=button.class
WIDTH=300
HEIGHT=200 >
</APPLET>*/
public class button extends JApplet {
JButton button = new JButton("Click Me");
JTextField text = new JTextField(20);
public void init()
{
Container contentPane = getContentPane();
contentPane.setLayout(new FlowLayout());
contentPane.add(button);
contentPane.add(text);
button.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent event) {
text.setText("Buttons in Swing!");
}
});
}
}

Swing Window Apps



import javax.swing.JTextPane;
import javax.swing.JFrame;
import javax.swing.text.StyledDocument;
import javax.swing.text.StyleConstants;
import javax.swing.text.MutableAttributeSet;

import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;
import java.awt.event.WindowAdapter;

import java.awt.Color;
import java.awt.Font;
import java.awt.BorderLayout;
/*
<APPLET
CODE=windowlistenapp1.class
WIDTH=500
HEIGHT=200 >
</APPLET>
*/

public class windowlistenapp1 extends WindowAdapter {

    public windowlistenapp1() {
        JFrame frm = new JFrame("Second JTextPane Example");
        frm.getContentPane().setLayout(new BorderLayout());

        JTextPane jtextpane = new JTextPane();
        Font fnt = new Font("Serif", Font.ITALIC, 20);
        setJTextPaneFont(jtextpane, fnt, Color.blue);
        frm.getContentPane().add(jtextpane, BorderLayout.CENTER);

        frm.addWindowListener(this);
        frm.setSize(400, 400);
        frm.show();
    }


    public static void setJTextPaneFont(JTextPane jtp, Font font, Color c) {
        MutableAttributeSet attrs = jtp.getInputAttributes();


        StyleConstants.setFontFamily(attrs, font.getFamily());
        StyleConstants.setFontSize(attrs, font.getSize());
        StyleConstants.setItalic(attrs, (font.getStyle() & Font.ITALIC) != 0);
        StyleConstants.setBold(attrs, (font.getStyle() & Font.BOLD) != 0);

     
        StyleConstants.setForeground(attrs, c);

        StyledDocument doc = jtp.getStyledDocument();

        doc.setCharacterAttributes(0, doc.getLength() + 1, attrs, false);
    }

    public void windowClosing(WindowEvent evt) {
        System.exit(0);
    }

    public static void main(String[] args) {
        windowlistenapp1 instance = new windowlistenapp1();
    }

}

Swing Window Application


import javax.swing.JTextPane;
import javax.swing.JFrame;

import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;
import java.awt.event.WindowAdapter;

import java.awt.Color;
import java.awt.Font;
import java.awt.BorderLayout;
/*
<APPLET
CODE=windowlistenapp.class
WIDTH=500
HEIGHT=200 >
</APPLET>
*/
public class windowlistenapp extends WindowAdapter {

    public windowlistenapp() {
        JFrame frm = new JFrame("First JTextPane Example");
        frm.getContentPane().setLayout(new BorderLayout());

        JTextPane textpane = new JTextPane();
        Font fnt = new Font("Serif", Font.ITALIC, 20);
        textpane.setFont(fnt);
        textpane.setForeground(Color.blue);
        frm.getContentPane().add(textpane, BorderLayout.CENTER);

        frm.addWindowListener(this);
        frm.setSize(400, 400);
        frm.show();
    }

    public void windowClosing(WindowEvent evt) {
        System.exit(0);
    }

    public static void main(String[] args) {
        windowlistenapp instance = new windowlistenapp();
    }

}

Swing window pad


import java.awt.TextArea;
import java.awt.Frame;

import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;
import java.awt.event.WindowAdapter;

import java.awt.Color;
import java.awt.Font;
import java.awt.BorderLayout;
/*
<APPLET
CODE=windowlisten.class
WIDTH=500
HEIGHT=200 >
</APPLET>
*/
public class windowlisten extends WindowAdapter {

    public windowlisten() {
        Frame frm = new Frame("TextArea Example");
        frm.setLayout(new BorderLayout());

        TextArea texta = new TextArea();
        Font fnt = new Font("Serif", Font.ITALIC, 20);
        texta.setFont(fnt);
        texta.setForeground(Color.blue);
        frm.add(texta, BorderLayout.CENTER);

        frm.addWindowListener(this);
        frm.setSize(400, 400);
        frm.show();
    }

    public void windowClosing(WindowEvent evt) {
        System.exit(0);
    }

    public static void main(String[] args) {
        windowlisten instance = new windowlisten();
    }

}

Swing TextField with alignment


import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JFrame;
import javax.swing.JTextField;

/**
 *
 * @author Amol
 */
public class Main {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
final JTextField jTextField =new JTextField("press<enter>", 60);
jTextField.setHorizontalAlignment(JTextField.RIGHT);
jTextField.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent e) {
                int temp = jTextField.getHorizontalAlignment();
                if(temp==JTextField.LEFT){
                    jTextField.setHorizontalAlignment(JTextField.RIGHT);
                   
                }
if(temp==JTextField.RIGHT){
                    jTextField.setHorizontalAlignment(JTextField.CENTER);
                   
                }
                        if(temp==JTextField.CENTER){
                    jTextField.setHorizontalAlignment(JTextField.LEFT);
                   
                   
                }
           
           
            }
        });
        JFrame frame=new JFrame("TextField");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.getContentPane().setLayout(new java.awt.FlowLayout());
        frame.getContentPane().add(frame);
        frame.setSize(275, 75);
        frame.setVisible(true);
        frame.requestFocus();

    }

}

Swing TextField Program


import java.awt.*;
import javax.swing.*;
/* <APPLET
CODE=Texfield.class
WIDTH=300
HEIGHT=200 >
</APPLET>*/
public class Texfield extends JApplet {
JTextField text = new JTextField(20);
public void init()
{
Container contentPane = getContentPane();
contentPane.setLayout(new FlowLayout());
contentPane.add(text);
text.setText("Hello from Swing!");
}
}

Swing label with image 2


import java.awt.*;
import javax.swing.*;
/* <applet code="Lbl" width=600 height=250>
  </applet> */

public class Lbl extends JApplet {
public void init() {
   ImageIcon lb = new ImageIcon("illusion.gif");
   JLabel BL = new JLabel("Zollner's illusion", lb, JLabel.CENTER);
   add(BL);
}
}

Swing label with image


import java.awt.*;
import javax.swing.*;
/*
<APPLET
CODE=labelimage.class
WIDTH=400
HEIGHT=200 >
</APPLET>
*/
public class labelimage extends JApplet
{
public void init()
{
Container contentPane = getContentPane();
JLabel jlabel = new JLabel("Label", new ImageIcon("any image.jpg"),
JLabel.CENTER);
jlabel.setVerticalTextPosition(JLabel.BOTTOM);
jlabel.setHorizontalTextPosition(JLabel.CENTER);
contentPane.add(jlabel);
}
}

Swing Label


import java.awt.*;
import javax.swing.*;
/*
<APPLET
CODE=label.class
WIDTH=300
HEIGHT=200 >
</APPLET>
*/
public class label extends JApplet
{
public label()
{
Container contentPane = getContentPane();
JLabel jlabel = new JLabel("Hello from Swing!");
contentPane.setLayout(new FlowLayout());
contentPane.add(jlabel);
}
}

Tuesday, 25 December 2012

To Char Array


package chapter2;

import java.util.Locale;

/**
 *
 * @author Amol
 */
public class ToCharArray {
    public static void main(String[] args) {
        String s1="Hello Form java";
        char charAt = s1.charAt(4);
        System.out.println("charAt = " + charAt);
        char[] toCharArray = s1.toCharArray();
        for (int i = 0; i < toCharArray.length; i++) {
            char c = toCharArray[i];
            System.out.println("c = " + c);

        }
       

        System.out.println("AmolShendre".replaceFirst("Amol", "Bhushan"));
        System.out.println("AmolShendre".replaceFirst("A", "B"));
        System.out.println("AmolShendre".toLowerCase());
        System.out.println("AmolShendre".toString());
        System.out.println("AmolShendre".toUpperCase(Locale.JAPANESE));

    }
}

Core Java :- Characters


public class Main {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {

    char char3;
    char3=67;
        System.out.println("char3 = " + char3+1);
    char3='r';
        System.out.println("char3 = " + ++char3);
    char3=69;
        System.out.println("char3 = " + char3);
    char3=70;
        System.out.println("char3 = " + char3);
    char3=71;
        System.out.println("char3 = " + char3);
        String s1="I m Amol\"Shendre\"";
        System.out.println("s1 = " + s1);
        System.out.println("I m Amol Shendre".concat(" u Suked"));
       
        int tmp=10;
        System.out.println("tmp = " + tmp++);
        tmp=10;
        System.out.println("tmp = " + ++tmp);



    }

}

Core Java :- Number Format


import java.text.*;

/**fo
 *
 * @author Amol
 */
public class FormatNumbers {
    public static void main(String[] args) {
 
        double value=1.4647646764484;
    NumberFormat format=NumberFormat.getNumberInstance();
format.setMaximumFractionDigits(6);
        String format1 = format.format(value);
        System.out.println("format1 = " + format1);
    }



}

Java Swing :- Panel Border


import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.*;
/* <APPLET
CODE=border.class
WIDTH=300
HEIGHT=200 >
</APPLET>*/
public class border extends JApplet {
jpanel j;
public void init() {
Container contentPane = getContentPane();
j = new jpanel();
j.setBorder(BorderFactory.createRaisedBevelBorder());
contentPane.add(j);
}
}

class jpanel extends JPanel
{
public void paintComponent (Graphics g)
{
g.drawString("Swing with Border!",
getBorder().getBorderInsets(this).left, 50);
}
}

Java Swing:- Closing Frame Windows


import java.awt.*;
import javax.swing.*;
/* <APPLET
CODE=component.class
WIDTH=210
HEIGHT=200 >
</APPLET> */
public class component extends JApplet {
public void init() {
Container contentPane = getContentPane();
JNewLabel jnewlabel = new JNewLabel(
"This is a fake label.");
contentPane.setLayout(new FlowLayout());
contentPane.add(new JLabel("This is a real label."));
contentPane.add(jnewlabel);
}
}
class JNewLabel extends JTextField {
public JNewLabel(String s) {
super(s);
}
public void updateUI() {
super.updateUI();
setHighlighter(null);
setEditable(false);
LookAndFeel.installBorder(this, "Label.border");
LookAndFeel.installColorsAndFont(this, "Label.background",
"Label.foreground", "Label.font");
}
}

Java Swing :- Swing Pan


import java.awt.Color;
import java.awt.Container;
import java.awt.Graphics;
import javax.swing.JApplet;
import javax.swing.JPanel;

/**
 *
 * @author Amol
 */
public class SwaPan extends JApplet{
    JPanel j;
    public void init(){
        Container contentPane = getContentPane();
        j=new JPanel();
        contentPane.add(j);
       
    }
   
}
   
   
   
class jpanel extends JPanel{

    public jpanel() {
    setBackground(Color.white);
       
    }
    public void paintComponent(Graphics g){
        super.paintComponent(g);
        g.drawString("Hello from Swing ",0, 60);
       
    }
   
}

Java Swing : Applet Application


import java.awt.Color;
import java.awt.Container;
import java.awt.Graphics;
import java.awt.HeadlessException;
import javax.swing.JFrame;
import javax.swing.JPanel;

/**
 *
 * @author Amol
 */
public class Jpan extends JFrame{
   
    jpanel j;

    public Jpan() throws HeadlessException {
    super("Swing application");
        Container contentPane = getContentPane();
        j=new jpanel();
        contentPane.add(j);
       
    }
    public static void main(String[] args) {
       final JFrame jf = new Jpan();
        jf.setBounds(100, 100, 300, 300);
    jf.setVisible(true);  
    }
}


class jpan1 extends JPanel{

    public jpan1() {
   
    setBackground(Color.red);
   
    }
    public void paintComponent(Graphics g){
        super.paintComponent(g);
        g.drawString("Swing using Jpanel and JFrame", 50, 30);
        g.drawString("There is a problem here !", 70, 120);
        g.drawString("\"Closing me won't end the application \"", 35, 150);
       
    }
   
}

Java Swing :- Content Panes


import java.awt.Color;
import java.awt.GridLayout;
import java.awt.HeadlessException;
import javax.swing.JFrame;
import javax.swing.JPanel;

/**
 *
 * @author Amol
 */
public class ContF extends JFrame{

    public ContF() throws HeadlessException {
 super("Test Frame");
 setBackground(Color.red);
 setLayout(new GridLayout(7,7,2,2));
        for (int i = 0; i < 49; i++) {
            JPanel jp = new JPanel();
            jp.setBackground(new Color((int)(Math.random()*167714544)));
            add(jp);
            setSize(400,400);
            setVisible(true);
       
   
        }
       
    }
   
    public static void main(String[] args) {
        new ContF();
    }
}