Label Component in Java Swing

Simple code that explains how the JLabel component can be created in Java Swing.

File:la.java

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
class la extends JFrame
{
JLabel l;
la()
{
setTitle(“Components Window”);
JPanel content=new JPanel();

l=new JLabel(“Label in Java Swing”);
l.setFont(new Font(“Arial”,Font.PLAIN,16));
content.add(l);
getContentPane().add(content);

myadapter my=new myadapter();
addWindowListener(my);
}
public class myadapter extends WindowAdapter
{
public void windowClosing(WindowEvent e)
{
System.exit(0);
}
}
public static void main(String ar[])
{
la a=new la();
a.setSize(400,400);
a.setVisible(true);
}
}

Output

Advertisements

Implementation of Notepad Editor in Java

I Had some knowledge while doing those kind of working flow of a particular technology.

Editor2.java

import java.awt.*;
import java.awt.event.*;
import java.awt.datatransfer.*;
import java.io.*;
import javax.swing.*;
import javax.swing.event.*;
public class Editor2 extends JFrame
{
private Color colorValues[] = {Color.black, Color.blue, Color.red, Color.green };
private JRadioButtonMenuItem colorItems[], fonts[];
private JCheckBoxMenuItem styleItems[];
private ButtonGroup fontGroup, colorGroup;
private int style;
String filename;
JTextArea ta;
JScrollPane sp;
JMenuBar mb;
JMenu f,e,h,tb,formatMenu;
JMenuItem n,o,s,s1,ex,cut,copy,paste,cmd,abt,tools;
Clipboard clip=getToolkit().getSystemClipboard();
public Editor2()
{
super(“TextEditor”);
Container c=getContentPane();
c.setLayout(new GridLayout(1,1));
ta=new JTextArea();
c.add(ta);
sp=new JScrollPane(ta);
c.add(sp);
mb=new JMenuBar();
f=new JMenu(“File”);
f.setMnemonic(‘F’);
n=new JMenuItem(“New”);
n.setMnemonic(‘N’);
o=new JMenuItem(“Open”);
o.setMnemonic(‘O’);
s=new JMenuItem(“Save”);
s.setMnemonic(‘S’);
s1=new JMenuItem(“SaveAs”);
s1.setMnemonic(‘A’);
ex=new JMenuItem(“Exit”);
ex.setMnemonic(‘x’);
n.addActionListener(new New());
f.add(n);
o.addActionListener(new Open());
f.add(o);
s.addActionListener(new Save());
f.add(s);
f.addSeparator();
s1.addActionListener(new SaveAs());
f.add(s1);
ex.addActionListener(new Exit());
f.add(ex);
e=new JMenu(“Edit”);
e.setMnemonic(‘E’);
cut=new JMenuItem(“Cut”);
cut.setMnemonic(‘u’);
copy=new JMenuItem(“Copy”);
copy.setMnemonic(‘C’);
paste=new JMenuItem(“Paste”);
paste.setMnemonic(’s’);
cut.addActionListener(new Cut());
e.add(cut);
copy.addActionListener(new Copy());
e.add(copy);
paste.addActionListener(new Paste());
e.add(paste);
formatMenu = new JMenu( “Format” );
formatMenu.setMnemonic( ‘r’ );
String colors[] = { “Black”, “Blue”, “Red”, “Green”};
JMenu colorMenu = new JMenu( “Color” );
colorMenu.setMnemonic( ‘C’ );
colorItems = new JRadioButtonMenuItem[ colors.length ];
colorGroup = new ButtonGroup();
ItemHandler itemHandler = new ItemHandler();
for ( int i = 0; i < colors.length; i++ )
{
colorItems[ i ] =
new JRadioButtonMenuItem( colors[ i ] );
colorMenu.add( colorItems[ i ] );
colorGroup.add( colorItems[ i ] );
colorItems[ i ].addActionListener( itemHandler );
}
colorItems[ 0 ].setSelected( true );
formatMenu.add( colorMenu );
formatMenu.addSeparator();
String fontNames[] = { “TimesRoman”, “Courier”, “Helvetica” };
JMenu fontMenu = new JMenu( “Font” );
fontMenu.setMnemonic( ‘n’ );
fonts = new JRadioButtonMenuItem[ fontNames.length ];
fontGroup = new ButtonGroup();
for( int i = 0; i < fonts.length; i++ )
{
fonts[ i ] =new JRadioButtonMenuItem( fontNames[ i ] );
fontMenu.add( fonts[ i ] );
fontGroup.add( fonts[ i ] );
fonts[ i ].addActionListener( itemHandler );
}
fonts[ 0 ].setSelected( true );
fontMenu.addSeparator();
String styleNames[] = { “Bold”, “Italic” };
styleItems = new JCheckBoxMenuItem[ styleNames.length ];
StyleHandler styleHandler = new StyleHandler();
for ( int i = 0; i < styleNames.length; i++ )
{
styleItems[ i ] =new JCheckBoxMenuItem( styleNames[ i ] );
fontMenu.add( styleItems[ i ] );
styleItems[ i ].addItemListener( styleHandler );
}
formatMenu.add( fontMenu );
mb.add( formatMenu );
h=new JMenu(“Help”);
h.setMnemonic(‘H’);
cmd=new JMenuItem(“Commands”);
cmd.setMnemonic(‘m’);
cmd.addActionListener(new Commands());
h.add(cmd);
h.addSeparator();
abt=new JMenuItem(“About”);
abt.setMnemonic(‘A’);
abt.addActionListener(new About());
h.add(abt);
mb.add(f);
c.add(mb);
mb.add(e);
c.add(mb);
mb.add(formatMenu);
c.add(mb);
mb.add(h);
c.add(mb);
setJMenuBar(mb);
MyWindowAdapter mylist=new MyWindowAdapter();
addWindowListener(mylist);
}
class MyWindowAdapter extends WindowAdapter
{
public void windowClosing(WindowEvent e)
{
System.exit(0);
}
}
class New implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
filename=”TextEditor 1″;
ta.setText(“”);
setTitle(filename);
}
}
class Open implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
FileDialog fd=new FileDialog(Editor2.this,”SelectFile”,FileDialog.LOAD);
fd.show();
if(fd.getFile()!=null)
{
filename=fd.getDirectory()+fd.getFile();
setTitle(filename);
ReadFile();
}
ta.requestFocus();
}
}
class Save implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
FileDialog fd=new FileDialog(Editor2.this,”SaveFile”,FileDialog.SAVE);
fd.show();
if(fd.getFile()!=null)
{
filename=fd.getDirectory()+fd.getFile();
setTitle(filename);
try
{
DataOutputStream d=new DataOutputStream(new FileOutputStream(filename));
String line=ta.getText();
BufferedReader br=new BufferedReader(new StringReader(line));
while((line=br.readLine())!=null)
d.writeBytes(line+”\r\n”);
d.close();
}
catch(Exception ex)
{
System.out.println(“File not found”);
}
ta.requestFocus();
}
}
}
class SaveAs implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
FileDialog fd=new FileDialog(Editor2.this,”SaveAsFile”,FileDialog.SAVE);
fd.show();
if(fd.getFile()!=null)
{
filename=fd.getDirectory()+fd.getFile();
setTitle(filename);
try
{
DataOutputStream d=new DataOutputStream(new FileOutputStream(filename));
String line=ta.getText();
BufferedReader br=new BufferedReader(new StringReader(line));
while((line=br.readLine())!=null)
d.writeBytes(line+”\r\n”);
d.close();
}
catch(Exception ex)
{
System.out.println(“File not found”);
}
ta.requestFocus();
}
}
}
class Exit implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
System.exit(0);
}
}
void ReadFile()
{
BufferedReader d;
StringBuffer sb=new StringBuffer();
try
{
d=new BufferedReader(new FileReader(filename));
String line;
while((line=d.readLine())!=null)
sb.append(line+”\n”);
ta.setText(sb.toString());
d.close();
}
catch(FileNotFoundException ex){}
catch(IOException ex){ }
}
class Cut implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
String se=ta.getSelectedText();
StringSelection ss=new StringSelection(se);
clip.setContents(ss,ss);
ta.replaceRange(“”,ta.getSelectionStart(),ta.getSelectionEnd());
}
}
class Copy implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
String se=ta.getSelectedText();
StringSelection clipstring=new StringSelection(se);
clip.setContents(clipstring,clipstring);
}
}
class Paste implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
Transferable cliptran=clip.getContents(Editor2.this);
try
{
String se=(String)cliptran.getTransferData(DataFlavor.stringFlavor);
ta.replaceRange(se,ta.getSelectionStart(),ta.getSelectionEnd());
}
catch(Exception ex)
{
System.out.println(“not string flavour”);
}
}
}
class ItemHandler implements ActionListener
{
public void actionPerformed( ActionEvent e )
{
for ( int i = 0; i < colorItems.length; i++ )
if ( colorItems[ i ].isSelected() )
{
ta.setForeground( colorValues[ i ] );
break;
}
for ( int i = 0; i To Select a whole document\nCtrl+x–>To Cut the Selected Text\nCtrl+c–>To copy the Selected Text\nCtrl+v–>To Paste the Selected Text Again”);
}
}
class About implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
JOptionPane.showMessageDialog(Editor2.this,”TextEditor Version 1.0\nKarthik\n”);
}
}
public static void main(String args[])
{
JFrame ed=new Editor2();
ed.setSize(500,500);
ed.setVisible(true);
}
}