0% found this document useful (0 votes)
15 views

Ajp Expt 6

Study with me

Uploaded by

janhavi1292006
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
15 views

Ajp Expt 6

Study with me

Uploaded by

janhavi1292006
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

// Exp 6.

1
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.io.*;
import javax.swing.table.*;

public class exp61chng


{
public static void main(String[] args)
{
String [][] data = {
{"ADVANCED JAVA","550"},
{"ADVANCED MICROPROCESSOR","450"},
{"LINUX PROGRAMMING","800"},
{"MANAGEMENT","600"},
{"SOFTWARE TESTING","950"},
};
String [] headers = {" Title "," Price "};
JFrame frame = new JFrame("JTable to Excel File");
DefaultTableModel model = new DefaultTableModel(data,headers);
final JTable table = new JTable(model);
JScrollPane scroll = new JScrollPane(table);
JButton save= new JButton("EXPORT");
frame.add(save);

save.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent action)
{
try {
ExcelExporter exp = new ExcelExporter();
exp.exportTable(table, new File("results.xls"));
}
catch (IOException ex)
{
System.out.println(ex.getMessage());
}
}
});

frame.getContentPane().add("Center",scroll);
frame.getContentPane().add("South",save);
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}

class ExcelExporter
{
public ExcelExporter() {}

1 Mrs. Chavan P.P.


public void exportTable(JTable table, File file) throws IOException
{
TableModel model = table.getModel();
FileWriter out = new FileWriter(file);

for(int i=0; i < model.getColumnCount();i++)


{
out.write(model.getColumnName(i)+"\t");
}
out.write("\n");
for(int i=0; i < model.getRowCount();i++)
{
for(int j=0;j < model.getColumnCount();j++)
{
if(j==0)
{
out.write(model.getValueAt(i,j).toString() + "\t");
}
else
{
out.write("\"" +model.getValueAt(i,j).toString()+ "\t"+ "\"");
}
}
out.write("\n");
}
out.close();
System.out.println(" " + file);
}
}

//Expt 6.5

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

class RButtonTest extends JFrame implements ActionListener


{
private JRadioButton red;
private JRadioButton blue;
private JRadioButton green;
Container container;

public RButtonTest()
{
super("Radio Button Test");
container = getContentPane();
container.setLayout(new FlowLayout());
setVisible(true);
setSize(400,250);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

2 Mrs. Chavan P.P.


red=new JRadioButton(" red");
blue=new JRadioButton(" blue");
green=new JRadioButton(" green");
ButtonGroup group=new ButtonGroup();
group.add(red);
group.add(blue);
group.add(green);
red.addActionListener(this);
blue.addActionListener(this);
green.addActionListener(this);
container.add(red);
container.add(blue);
container.add(green);
}
public void actionPerformed(ActionEvent e)
{
if(e.getSource()== red)
{
container.setBackground(Color.red);
}

if(e.getSource()== blue)
{
container.setBackground(Color.blue);
}
if(e.getSource()== green)
{
container.setBackground(Color.green);
}
}
}

public class exp65


{
public static void main(String[] args)
{
RButtonTest s=new RButtonTest();
}
}

3 Mrs. Chavan P.P.

You might also like