-1

I have the following bit of code:

The problem is cant seem to get the heading into a seperate columns in the table, rather my column headers as defined in this part

String[] columnNames ={container.toString()};

and is derived from

URLConnection yc = oracle.openConnection();
BufferedReader in = new BufferedReader(new InputStreamReader(
yc.getInputStream()));
//create array--------------
ArrayList list = new ArrayList();


Scanner scanner = new Scanner(in);
while (scanner.hasNextLine()) {
String line = scanner.nextLine();
//add line to array-------------
list.add(line);
}
//get headings of data------------------

//String heading[] = list.get(2).toString();
String[] simpleArray = new String[list.size()];

list.toArray(simpleArray);

String j = (String) list.get(2);

String [] items = j.split(";");
List<String> container = Arrays.asList(items);



createUserInterface(container); 

appears in its entirety in the first column (and therefore the only column in the table) I therefore only see "First Name" appear in the data field

Println of my headings are

[A, B, C, D, E, F, G, H, I, J, K, L, M, N, O, P, Q, R,S] *I have substituted my actual headings with letters

Help appreciated

code below--

public void createUserInterface(List<String>container) {
//create user interface---------------------    
setLayout( new BorderLayout());
setTitle("Fund");//setTitle is also a JAVA Parameter
setSize(2000, 200);
setBackground(Color.gray);
setDefaultCloseOperation(EXIT_ON_CLOSE);
// Create a panel to hold all other components
JPanel topPanel = new JPanel();
topPanel.setLayout( new BorderLayout() );
getContentPane().add(topPanel);
//


System.out.println(container.toString());

List list = Arrays.asList(container);

Object[] columnNames ={list};
Object[][] data = {{"First Name", "Last Name", "Sport", "# of Years","First Name", "Last Name", "Sport", "# of Years","First Name", "Last Name", "Sport", "# of Years","First Name", "Last Name", "Sport", "# of Years","First Name", "Last Name", "Sport", "# of Years"}};



table = new JTable(data, columnNames);
// Create a new table instance
scrollPane = new JScrollPane( table );
topPanel.add( scrollPane, BorderLayout.CENTER );

setVisible(true);   



}
Ingram
  • 654
  • 2
  • 7
  • 29

1 Answers1

2

What do you expect that this will return: container.toString()? How could that possibly represent your table headings?

If you want the letters from A through S to be your JTable column headings (that's what I guess you're trying to do), why not simply use this array?

String[] columnNames = {"A", "B", "C", "D", "E", "F", "G", "H", "I", "J", 
        "K", "L", "M", "N", "O", "P", "Q", "R", "S"}
Hovercraft Full Of Eels
  • 283,665
  • 25
  • 256
  • 373
  • I have updated my question. the letters were substituted exemplify the problem. The actual column names come from a url, I am taking the headings line in the url as per the code String j = (String) list.get(2); – Ingram Mar 18 '15 at 22:06
  • @MrAssistance: again, when you call `toString()` on *anything* you get just that, a **single** String. You need to pass into your JTable constructor an array or a `List`. – Hovercraft Full Of Eels Mar 18 '15 at 22:07
  • Okay so if i wrote String[] columnNames ={container}; isnt that an array? I have tried this but this still doesnt work... – Ingram Mar 18 '15 at 22:10
  • 1
    @MrAssistance: it's an array of **one item, one String**. Are you expecting a single column of data? I doubt it. So no, of course that won't work. You need to pass in an array **or `List` that makes sense, that holds each column heading as its own element. – Hovercraft Full Of Eels Mar 18 '15 at 22:11
  • You are right i am receiving output as if it is one item, one String. So in the first part my List container = Arrays.asList(items); ..container is being passed as a List, and now each element of that list needs to be broken into separate items and passed into the table.....do i need to split each element by "," and pass that into a new array? – Ingram Mar 18 '15 at 22:21
  • @ Hovercraft Full Of Eels. I have changed my code however i still seem to get the same result, advice please – Ingram Mar 19 '15 at 19:42
  • @ Hovercraft Full Of Eels could you offer help please? – Ingram Mar 21 '15 at 18:22