I am facing a trouble, that I can't insert a string array into a jTable. The scene is that I have some selected values in an array, and I want to insert that value into jTable.
My code is here:
import java.util.Arrays;
import java.util.Vector;
import javax.swing.table.DefaultTableModel;
public class as extends javax.swing.JFrame {
public as() {
initComponents();
}
@SuppressWarnings("unchecked")
private void initComponents() {
jScrollPane1 = new javax.swing.JScrollPane();
t1 = new javax.swing.JTable();
jScrollPane2 = new javax.swing.JScrollPane();
t2 = new javax.swing.JTable();
jButton1 = new javax.swing.JButton();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
t1.setModel(new javax.swing.table.DefaultTableModel(
new Object [][] {
{"122", "a", null, null},
{"123", "b", null, null},
{"124", "c", null, null},
{"125", "d", null, null},
{"126", "e", null, null}
},
new String [] {
"Title 1", "Title 2", "Title 3", "Title 4"
}
));
jScrollPane1.setViewportView(t1);
t2.setModel(new javax.swing.table.DefaultTableModel(
new Object [][] {
},
new String [] {
"Title 1", "Title 2"
}
));
private void jButton1MouseReleased(java.awt.event.MouseEvent evt) {
// TODO add your handling code here:
Vector R_D=new Vector();
if(t1.getRowCount()>0){
String[] ary = new String[t1.getRowCount()];
try{
for(int i=0;i<t1.getRowCount();i++){
R_D.add(t1.getValueAt(i, 0).toString());
ary[i]=t1.getValueAt(i, 0).toString();
System.out.println(Arrays.toString(ary)+i);
((DefaultTableModel) t2.getModel()).addRow(ary);
}
}
catch(Exception d)
{System.out.println(d.getMessage());}
}
}
/**
* @param args the command line arguments
*/
public static void main(String args[]) {
/* Create and display the form */
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new as().setVisible(true);
}
});
}
// Variables declaration - do not modify
private javax.swing.JButton jButton1;
private javax.swing.JScrollPane jScrollPane1;
private javax.swing.JScrollPane jScrollPane2;
private javax.swing.JTable t1;
private javax.swing.JTable t2;
// End of variables declaration
I have the right answer in that Array named ary[]
.
I need is to copy all array values into separate jTable Rows.
But the result ends in some empty table without showing any errors
just see the cropped screen shots
This is the table having selected employees list.
After clicking Create Attendance List button, the result is
The table will look like this.
But I need a result which something look like this
Please help me.
that I can't insert a string array into a jTable.
Well I don't see any code where you attempt to do that.
Start by reading the DefaultTableModel
API. You can easily create a DefaultTableModel with your data. You can:
create an empty DefaultTableModel and then use the addRow(...)
method to add a row in your loop.
You can create a DefaultTableModel with the number of rows that your want. Then you can use the setValueAt(...)
method to change the data in each row.
If you need more help then post a proper SSCCE that demonstrates the problem you are having.
Edit:
An example of a SSCCE
that simply adds a row of data to a table when a button is pressed. The code creates an Array with two values and then invokes the addRow(...)
method of the DefaultTableModel.
import java.awt.*;
import java.beans.*;
import java.awt.event.*;
import java.util.*;
import java.io.*;
import javax.swing.*;
import javax.swing.event.*;
import javax.swing.table.*;
public class TableRowColumn2 extends JFrame
{
private final static String LETTERS = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
JTable table;
DefaultTableModel model;
JPanel buttonPanel;
JButton button;
public TableRowColumn2()
{
// Create table
Object[][] data =
{
{new Integer(1), "A"},
{new Integer(2), "B"},
{new Integer(3), "C"}
};
String[] columnNames = {"Number","Letter"};
model = new DefaultTableModel(data, columnNames);
table = new JTable(model)
{
public boolean isCellEditable(int row, int column)
{
return true;
}
};
table.putClientProperty("terminateEditOnFocusLost", Boolean.TRUE);
table.setPreferredScrollableViewportSize(table.getPreferredSize());
table.changeSelection(0, 0, false, false);
table.setSelectionMode(ListSelectionModel.SINGLE_INTERVAL_SELECTION);
table.getColumnModel().getColumn(0).setCellRenderer( table.getDefaultRenderer(Integer.class) );
// Add table and a Button panel to the frame
final JScrollPane scrollPane = new JScrollPane( table );
getContentPane().add( scrollPane, BorderLayout.CENTER );
buttonPanel = new JPanel();
getContentPane().add( buttonPanel, BorderLayout.NORTH );
button = new JButton( "Add Row" );
button.setMnemonic('A');
buttonPanel.add( button );
button.addActionListener( new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
model.addRow( createRow() );
int row = table.getRowCount() - 1;
table.changeSelection(row, 0, false, false);
table.requestFocusInWindow();
}
});
}
private Object[] createRow()
{
Object[] newRow = new Object[2];
int row = table.getRowCount();
newRow[0] = Integer.toString(row + 1);
row = (row % 26) + 1;
newRow[1] = LETTERS.substring(row-1, row);
return newRow;
}
public static void main(String[] args)
{
TableRowColumn2 frame = new TableRowColumn2();
frame.setDefaultCloseOperation( EXIT_ON_CLOSE );
frame.pack();
frame.setLocationRelativeTo( null );
frame.setVisible(true);
}
}
In your case you will need to get each selected row. Then for each row you will:
So basically you need to change the code in the ActionListner to add a loop to do the above two steps.