problem i'm having is getSelectedRows() and getSelectedColumns().... they dont return anything when i select the rows.
am i doing it wrong?
i need it so when the button is pressed to output the selected strings[]
import java.awt.BorderLayout;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.*;
import javax.swing.table.AbstractTableModel;
import java.io.*;
import java.util.*;
import javax.swing.table.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.JButton.*;
import javax.swing.event.ListSelectionEvent;
public class panel extends JFrame
{
private JTable table;
private JButton button;
static ArrayList<String[]> rosterList = new ArrayList<String[]>();
private JPanel tablepanel;
private JPanel buttonpanel;
private JFrame tableframe;
private JScrollPane scrollPane;
[B]int [] rowIndex;
int [] colIndex;[/B]
public panel()
{
setTitle("Roster Sorter");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLayout(new BorderLayout());
textRead();
jTable();
jButton();
add(buttonpanel,BorderLayout.SOUTH);
add(scrollPane,BorderLayout.NORTH);
pack();
setVisible(true);
}
private void jTable()
{
TableModel myData = new MyTableModel();
table = new JTable(myData);
scrollPane = new JScrollPane(table);
scrollPane.setPreferredSize(new Dimension(1400, 700));
JTableHeader header = table.getTableHeader();
header.setBackground(Color.yellow);
table.setAutoCreateRowSorter(true);
[B]rowIndex = table.getSelectedRows();
colIndex = table.getSelectedColumns();[/B]
for(int index = 1; index == 26; index++)
{
TableColumn col = table.getColumnModel().getColumn(index);
int width = 150;
col.setPreferredWidth(width);
}
int index0 = 0;
TableColumn col = table.getColumnModel().getColumn(index0);
int width = 250;
col.setPreferredWidth(width);
}
private class MyTableModel extends AbstractTableModel {
private String[] columnNames = { "Name" , "Age", "Pos", "Nat", "St", "Tk", "Ps", "Sh", "Ag", "Kab", "Tab", "Pab", "Sab", "Gam", "Sub", "Min", "Mom", "Sav", "Con", "Ktk", "Kps", "Sht", "Gls", "Ass", "DP", "Inj", "Sus"};
public int getColumnCount() {
return columnNames.length;
}
public int getRowCount() {
return rosterList.size();
}
public String getColumnName(int col) {
return columnNames[col];
}
public Object getValueAt(int row, int col)
{
return rosterList.get(row)[col];
}
}
private static void textRead()
{
BufferedReader br = null;
try
{
br = new BufferedReader(new FileReader("rosters.txt"));
String line = br.readLine();
while (line != null )
{
String [] rowfields = line.split("[ ]+");
rosterList.add(rowfields);
line = br.readLine();
}
}
catch (FileNotFoundException e)
{
// can be thrown when creating the FileReader/BufferedReader
// deal with the exception
e.printStackTrace();
}
catch (IOException e)
{
// can be thrown by br.readLine()
// deal with the exception
e.printStackTrace();
}
}
private void jButton()
{
buttonpanel = new JPanel();
button = new JButton("Save selected to .txt");
button.setMnemonic(KeyEvent.VK_C);
buttonpanel.add(button);
buttonpanel.setSize(50,20);
button.addActionListener(new ValueButtonListener());
}
private class ValueButtonListener implements ActionListener{
public void actionPerformed (ActionEvent event){
for(int i = 0; i < rowIndex.length && i < colIndex.length;i++)
{
System.out.print(rowIndex[i]);
System.out.print(colIndex[i]);
}
}
}
}