Recommended: Sing it, brah! 5 fabulous songs for developers
JW's Top 5
Optimize with a SATA RAID Storage Solution
Range of capacities as low as $1250 per TB. Ideal if you currently rely on servers/disks/JBODs
Page 2 of 2
import java.awt.*;
import javax.swing.*;
public class Frame1 extends Frame
{
BorderLayout borderLayout1 = new BorderLayout();
JTable jTable1 ;
Object[][] data=new Object[4][4];
Object header[]= {"Jan","Feb","Mar","Apr"};
public static void main(String args[])
{
Frame1 myframe=new Frame1();
myframe.setSize(new Dimension(250,250));
myframe.setVisible(true);
}
public Frame1()
{
super();
try
{
jbInit();
}
catch (Exception e)
{
e.printStackTrace();
}
}
private void jbInit() throws Exception
{
for (int i=0;i<4;i++)
for (int j=0;j<4;j++)
data[i][j]=new Integer(i*10+j);
System.out.println("Header length="+header[1]);
jTable1=new JTable(data,header);
jTable1.setCellSelectionEnabled(true);
this.setTitle("Excel Lent JTABLE");
jTable1.setBackground(Color.pink);
this.setLayout(borderLayout1);
this.setSize(new Dimension(400, 300));
this.setBackground(Color.white);
this.add(jTable1, BorderLayout.CENTER);
// This is the line that does all the magic!
ExcelAdapter myAd = new ExcelAdapter(jTable1);
}
}
Excel has a very simple clipboard format. It separates elements on the same row with tabs and separates rows with newline characters. So, when you copy a set of contiguous and/or adjacent cells, Excel simply tokenizes the spreadsheet data into one long string, with the individual cell values separated by tabs and newlines within that string. What if the cells selected are not adjacent? Simple: Excel will not let you copy your selection to the clipboard. This behavior is mimicked by the adapter described here, which will also not let you copy data if the cells selected are not adjacent. In Excel, a dialog box pops up to tell us that you have not been permitted to copy; again, this behavior is mimicked by the adapter.
To use this feature, you need to download the ExcelAdapter.java file, compile it, and add the last line from the sample application to your code somewhere in order to activate the adapter
on the JTable.
Within the adapter, the activating keystrokes for both copy and paste functions are registered. Thereafter, any time the activating
key is typed, the actionPerformed method is called. If it is the copy action, then the selected cells' data are tokenized appropriately for Excel and written
to the system clipboard. If the action is paste, then the data within the system clipboard are converted to its string flavor
and parsed to populate the JTable cells, depending on which cell is selected.
Users of tables written in Java often want to be able to do something familiar in Excel with the data from JTables. Instead of writing code to enable copy-paste functionality on each JTable, it is much simpler to write a generic adapter that brings the desired functionality to every JTable with just one line of additional code. The adapter presented in this Java Tip does just that.
Read more about Core Java in JavaWorld's Core Java section.
datatransfer package is especially useful: