Swing Programs

Download as pdf or txt
Download as pdf or txt
You are on page 1of 5

1.

JTable Program
/*JTable displays data in horizontal and vertical columns in a two-dimensional format. The table can
be resized or moved. Here, table is created and added to a scroll pane. In turn, scroll pane is added
to the container. We create a JTable and add it to a scroll pane. Then, scroll pane is added to the
container.*/
import java.awt.*;
import java.awt.event.*;
import javax.swing.* ;
public class JTableDemo extends JFrame
{
public JTableDemo()
{
Container c = getContentPane();
c.setLayout( new BorderLayout()) ;

String fields[] = { "Sid", "Sname", "CGPA" } ;

String details[][] = {{ "1", "Rolex", "95.50" },


{ "2","karthi","67.50" },{ "3", "Anirudh", "86.30" },
{ "4", "Vijay", "75.75" },{ "5", "Siva", "70.25" }
};

JTable jt = new JTable(details, fields);

int v = ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED;
int h = ScrollPaneConstants.HORIZONTAL_SCROLLBAR_AS_NEEDED;
JScrollPane jsp = new JScrollPane(jt , v , h);
c.add(jsp, "Center");
setVisible(true);
setSize(500,500);
pack();
}
public static void main(String[] args)
{
new JTableDemo();
}
}

/*The JTable constructor takes the column data and column headers as parameters. The columns in
JTable can be slided by dragging the column name.
Other options for ScrollPaneConstants:
ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED;
ScrollPaneConstants.VERTICAL_SCROLLBAR_NEVER;
ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS;*/

2. TextAndPassValidation program using JTextField and JPassword


import javax.swing.*;
import java.awt.*;
import java.awt.event.* ;
public class TextAndPassValidation extends JFrame implements ActionListener
{
JTextField tf1, tf2;
JPasswordField pf ;
public TextAndPassValidation( )
{
Container c = getContentPane();
c.setLayout(new GridLayout(3, 2, 10, 10));
tf1 = new JTextField(15);
c.add(tf1);
tf2 = new JTextField("Sorry,U cannot enter", 25);
tf2.setEditable(false);
c.add(tf2);
pf = new JPasswordField(10);
c.add(pf); pf.addActionListener(this);
c.add(new JLabel("Enter User Name"));
c.add(tf1);
c.add(new JLabel("Enter Password"));
c.add(pf);
c.add(new JLabel("RESULT"));
c.add(tf2);
setSize(350,200);
setVisible(true);
}
public void actionPerformed(ActionEvent e)
{
String str1 = tf1.getText();
String str2 = pf.getText();
if(str1.equalsIgnoreCase("CSE2") && str2.equals("java"))
{
tf2.setText("VALID");
}
else
{
tf2.setText("INVALID");
}
}
public static void main(String args[])
{
new TextAndPassValidation();
}
}

3. Jcombobox
import javax.swing.*;
import java.awt.* ;
import java.awt.event.* ;
public class ComboBoxDemo extends JFrame implements ItemListener
{
JComboBox cbox;
JTextField jf;
public ComboBoxDemo()
{
Container c = getContentPane();
c.setLayout(new FlowLayout());
String sarray[] = {"Idly", "Dosa", "Vada"};
jf = new JTextField ("Displays the selection", 20);
cbox = new JComboBox(sarray); cbox.addItem("Puri");
cbox.addItem("Uthappa");
cbox.addItem("Coffee");
cbox.addItemListener(this);
c.add(cbox);
c.add(jf);
setSize(300,150);
setVisible(true);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public void itemStateChanged(ItemEvent e)
{
String str = (String)cbox.getSelectedItem();
jf.setText("You selected " + str);
}
public static void main(String args[])
{
new ComboBoxDemo();

}
}

4.
import javax.swing.*;
import java.awt.*;
import java.awt.event.* ;
public class JButtonDemo extends JFrame implements ActionListener
{
JButton rb, gb, bb ;
Container c;
public JButtonDemo()
{
c = getContentPane();
c.setLayout(new FlowLayout()) ;
ImageIcon ic1 = new ImageIcon("i1.jpg");
ImageIcon ic2 = new ImageIcon("i2.jpg");
rb = new JButton("Red", ic1);
rb.setRolloverIcon(ic2) ;
rb.setMnemonic('R'); //alt+R(short key)
rb.addActionListener(this);
c.add(rb);
gb = new JButton("Green");
gb.setToolTipText("VARDHAMAN");
gb.addActionListener(this);
c.add(gb);
bb = new JButton("My Beautiful Blue");
bb.setBorder(BorderFactory.createTitledBorder("Blue Button"));
bb.addActionListener(this);
c.add(bb);
addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent e)
{
System.exit(0);
}
});

setTitle("Learning JButton");
setSize(400,300);
setVisible(true);
}
public void actionPerformed(ActionEvent e)
{
String str = e.getActionCommand( ); // returns the label of the button
JButton btn = (JButton) e.getSource( ); // returns the object of the button
if(btn == rb )
c.setBackground(Color.red);
else if(btn == gb )
c.setBackground(Color.green);
else if(btn == bb )
c.setBackground(Color.blue);
JOptionPane.showMessageDialog(null, "You clicked " + str + " Button", "Learning JButton",
JOptionPane.INFORMATION_MESSAGE);
}
public static void main( String args[])
{
new JButtonDemo();
}
}
5. import java.awt.*;
import javax.swing.*;
public class JL extends JFrame
{

public JL()
{
setSize(300, 300);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLayout(new FlowLayout(FlowLayout.LEFT));
Icon icon = new ImageIcon("3.png");
JLabel label1 = new JLabel("Full Name :", icon, JLabel.LEFT);
JLabel label2 = new JLabel("Address :", JLabel.RIGHT);
label2.setIcon(new ImageIcon("4.png"));
getContentPane().add(label1);
getContentPane().add(label2);
setVisible(true);
}

public static void main(String[] args)


{
new JL();
}
}

You might also like