Reputation: 136
I am new to Swing and seeking some help here. I need to show the data from an .xls file in a jTable. Below is my code which I followed from here :-
jbClick.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
jChooser.showOpenDialog(null);
File file = jChooser.getSelectedFile();
if(!file.getName().endsWith("xls")){
JOptionPane.showMessageDialog(null, "Please select only Excel file.", "Error",JOptionPane.ERROR_MESSAGE);
}
else {
fillData(file);
model = new DefaultTableModel(data, headers);
tableWidth = model.getColumnCount() * 150;
tableHeight = model.getRowCount() * 25;
table.setPreferredSize(new Dimension( tableWidth, tableHeight));
table.setModel(model);
jbClick.setVisible(false);
jbText.setVisible(true);
}
}
});
JPanel chooserPanel = new JPanel();
JPanel filterPanlel = new JPanel();
//filterPanlel.add(jbText,"OLa");
final Color alternate = new Color(186,246,244);
table = new JTable();
table.setAutoCreateRowSorter(true);
model = new DefaultTableModel(data, headers);
table.setModel(model);
table.setBackground(alternate);
table.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
table.setEnabled(false);
table.setRowHeight(25);
table.setRowMargin(4);
tableWidth = model.getColumnCount() * 200;
tableHeight = model.getRowCount() * 25;
table.setPreferredSize(new Dimension( tableWidth, tableHeight));
scroll = new JScrollPane(table);
scroll.setBackground(alternate);
scroll.setPreferredSize(new Dimension(500, 500));
scroll.setHorizontalScrollBarPolicy( JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
scroll.setVerticalScrollBarPolicy( JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED);
getContentPane().add(buttonPanel, BorderLayout.NORTH);
getContentPane().add(scroll, BorderLayout.CENTER);
//setSize(600, 600);
setResizable(true);
setExtendedState(JFrame.MAXIMIZED_BOTH);
//setUndecorated(true);
setVisible(true);
}
/** * Fill JTable with Excel file data. * * @param file * file :contains xls file to display in jTable */
@SuppressWarnings({ "unchecked", "rawtypes" })
void fillData(File file) {
Workbook workbook = null;
try {
try {
workbook = Workbook.getWorkbook(file);
}
catch (IOException ex) {
Logger.getLogger( excelTojTable.class. getName()).log(Level.SEVERE, null, ex);
}
Sheet sheet = workbook.getSheet(0);
headers.clear();
for (int i = 0; i < sheet.getColumns(); i++)
{
Cell cell1 = sheet.getCell(i, 0);
headers.add(cell1.getContents());
}
data.clear();
for (int j = 1; j < sheet.getRows(); j++)
{
Vector d = new Vector();
for (int i = 0; i < sheet.getColumns(); i++)
{
Cell cell = sheet.getCell(i, j);
d.add(cell.getContents());
}
d.add("\n");
data.add(d);
}
}
catch (BiffException e) {
e.printStackTrace();
}
}
Problem :- Now I need to add a editable checkbox in-front of each row in the table.
Please help.
Upvotes: 0
Views: 831
Reputation: 324197
I need to add a editable checkbox in-front of each row in the table.
Before your loops that create the "header" and "d" Vectors, you need to add your Boolean information.
So for the header you might do:
headers.clear();
header.add("Select");
and for each row:
Vector d = new Vector();
d.add( Boolean.FALSE );.
i got a new column in my table but its not a checkbox
Then you would need to set the default renderer for the first column of the table by updating the TableColumn
of the TableColumnModel
using the default renderer for the Boolean class:
TableColumn tc = table.getColumnModel().getColumn(0);
tc.setCellRenderer( table.getDefaultRenderer( Boolean.class ) );
i am new to swing
Start with the Swing tutorial on [How to Use Table] (http://docs.oracle.com/javase/tutorial/uiswing/components/table.html) for the basics of renderers.
Edit:
A simple SSCCE that demonstrate the solution:
import java.awt.*;
import java.util.*;
import javax.swing.*;
import javax.swing.text.*;
import javax.swing.table.*;
public class SSCCE extends JPanel
{
public SSCCE()
{
Vector<String> header = new Vector<String>();
header.add("Select");
header.add("Column1");
header.add("Column2");
header.add("Column3");
Vector<Vector<Object>> data = new Vector<Vector<Object>>();
for (int row = 0; row < 5; row++)
{
Vector<Object> d = new Vector<Object>();
d.add( Boolean.FALSE );
for (int column = 0; column < 3; column++)
{
d.add(row + " : " + column);
}
data.add(d);
}
DefaultTableModel model = new DefaultTableModel(data, header);
JTable table = new JTable( model );
table.setPreferredScrollableViewportSize(table.getPreferredSize());
TableColumn tc = table.getColumnModel().getColumn(0);
tc.setCellRenderer( table.getDefaultRenderer( Boolean.class ) );
tc.setCellEditor( table.getDefaultEditor( Boolean.class ) );
JScrollPane scrollPane = new JScrollPane(table);
add(scrollPane);
}
private static void createAndShowGUI()
{
JFrame frame = new JFrame("SSCCE");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add( new SSCCE() );
frame.setLocationByPlatform( true );
frame.pack();
frame.setVisible( true );
}
public static void main(String[] args)
{
EventQueue.invokeLater(new Runnable()
{
public void run()
{
createAndShowGUI();
}
});
}
}
Upvotes: 2