ohh i see so how would i convert rosterList into an array? :S

ive tried the toArray method but i keep getting errors :S

No! Don't convert rosterList to something else, what you've got is fine.Your problem is in
ArrayList rosterList
Because you don't say what the members are, all that Java knows is that they must be Objects. If you declare it fully, Java will know what they are String arrays and be able to handle them properly:
ArrayList<String[]> rosterList = new ArrayList<String[]>();

===========

Hi BJSJC - thanks for covering this while I was offline. OP has a data file with a variable number of lines, each containing a fixed number of separate values. While working on the read file part of this we ended up with an ArrayList for the lines, each consisting of an array of the String values. Hence ArrayList<String[]> and the get(line number)[data item number] notation

put your line in and i still get an error:

array required, but java.util.ArrayList<java.lang.String[]> found

fixed the error with your notation in your edditted post:

[public Object getValueAt(int row, int col) 
        {    
             return rosterList.get(row)[col];
                              
        }

it compiles and runs, but now no rows are being displayed... not even empty boxes

Did you remember to call the method that reads the file before you try to display the table? If so, let's see all your code for the datamodel class.

got it working... thanks for all your help :) your a legend mate! couldnt have done it without ya!... learnt alot :) thanks.

ohh actually one more question... this is basically my first program, i am using BlueJ compiler... i have finished the program.. how would i put it onto an exe so i can send it to a mate and he can use it?

Briefly: you put all the compiled classes into a .jar file. When your mate double clicks that, the Java runtime on his machine will open the jar and execute the code in it. There's lots & lots of tutorials etc on the web, just Google Java jar.
Anyway, glad its working. Main thing to remember is to write & test code one little bit at a time, and when it goes wrong just stop for a cup of coffee and then think carefully thru what the error message is telling you.
Good luck
James

cool thanks... do you think its possible if i were to make a button on the JTable which when u click it outputs the highlighted cells in a JTable to a new .txt file in the same format as the original file without any splits and stuff?

Yes. You're gonna have to do this yourself, but there are JTble methods to get the currently highlighted cells. So you can loop thru the selected cells and write them to a text file (use print(..) for each cell value, then print a \n at the end of each row)

yeah ill give it a shot..
i managed to get the jar working :D

found this in the API: i think this is what i need to get highlighted data. thanks for your help mate.

getTableCellEditorComponent(JTable table,
Object value,
boolean isSelected,
int row,
int column)
isSelected - true if the cell is to be rendered with highlighting

No, that's something else.
If you want to select/write whole rows, look at JTable getSelectedRows(). There's also getSelectedColumns if you need it. Details, as always, are in the API doc.

so far i have come up with this... it compiles but it doesnt print - do i need to use a mouse even listener? like mouse pressed and mouse released?
sorry for asking again, but i am new to all this... is this going to be harder then what ive done so far? i may aswell stop while im in early stages if it is.

public class RosterSorter extends JPanel {
    private boolean DEBUG = false;
    static ArrayList<String[]> rosterList = new ArrayList<String[]>();
    int[] rowIndices;

    public RosterSorter() {
        super(new GridLayout(1,0));

        JTable table = new JTable(new MyTableModel());
        table.setAutoCreateRowSorter(true);
        JTableHeader header = table.getTableHeader();
        header.setBackground(Color.yellow);
        table.setPreferredScrollableViewportSize(new Dimension(1700, 700));
        table.setFillsViewportHeight(true);
        
        if (table.getColumnSelectionAllowed()
            && !table.getRowSelectionAllowed()) {
        // Column selection is enabled
        // Get the indices of the selected columns
        int[] vColIndices = table.getSelectedColumns();
    } else if (!table.getColumnSelectionAllowed()
            && table.getRowSelectionAllowed()) {
        // Row selection is enabled
        // Get the indices of the selected rows
        rowIndices = table.getSelectedRows();
        
    } 
       if(rowIndices.length > 0)
       {
        for (int i = 0; i <= rowIndices.length; i++)
        {
            System.out.println(rowIndices[i]);
        }
    }

ok created the the button as an object and initiate it in main:
the button shows up successfully as a different window to the JTable and when i click it it changes text etc.. but now i dont know how to pass it the values from the rowIndices string array, says variable not found... did i do it right in creating the button as an object?

public static void main(String[] args) {
        //Schedule a job for the event-dispatching thread:
        //creating and showing this application's GUI.
        javax.swing.SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                creatArr();
                createAndShowGUI();
                 new Output();
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;


public class Output extends JFrame{
    TextField text = new TextField(20);
    Button b;
    private JPanel imagePanel;     // To hold the label
    private JPanel buttonPanel;    // To hold a button
    private JLabel imageLabel;     // To show an image
    private JButton button;        // To get an image



     public Output()
   {
      // Set the title.
      setTitle("Output File");

      // Specify an action for the close button.
      setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

      // Create a BorderLayout manager.
      setLayout(new BorderLayout());

      // Build the panels.
      buildImagePanel();
      buildButtonPanel();

      // Add the panels to the content pane.
      add(imagePanel, BorderLayout.CENTER);
      add(buttonPanel, BorderLayout.SOUTH);

      // Pack and display the window.
      pack();
      setVisible(true);
   }

   /**
      The buildImagePanel method adds a label to a panel.
   */

   private void buildImagePanel()
   {
      // Create a panel.
      imagePanel = new JPanel();

      // Create a label.
      imageLabel = new JLabel("Click to save highlighted to .txt");
      
      // Add the label to the panel.
      imagePanel.add(imageLabel);
   }

   /**
      The buildButtonPanel method adds a button
      to a panel.
   */

   private void buildButtonPanel()
   {
      ImageIcon smileyImage;

      // Create a panel.
      buttonPanel = new JPanel();

    
      // Create a button.
      button = new JButton("SAVE");
      

      // Register an action listener with the button.
      button.addActionListener(new ButtonListener());

      // Add the button to the panel.
      buttonPanel.add(button);
   }

   /**
      Private inner class that handles the event when
      the user clicks the button.
   */

   private class ButtonListener implements ActionListener
   {
      public void actionPerformed(ActionEvent e)
      {
        
         

         // Remove the text from the label.
         imageLabel.setText(null);
         imageLabel.setText("File saved in directory as rosters1.txt");
         buttonPanel.remove(button);

         // Pack the frame again to accomodate the 
         // new size of the label.
         pack();
      }
   }

}

Best to have the button in the same window as the table.
ps going offline 'till tomorrow now.

oh ok np... thanks for all the help

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.