At the press of a button, several ArrayList<Integer>
, then calculations are made and the results are written to these ArrayList
. It is necessary to display each ArrayList
in a separate column of the table ( JTable
). Tell me how to do this, please.
Closed due to the fact that the essence of the question is not clear to the participants of Vartlok , user194374, D-side , fori1ton , Saidolim 5 Apr '16 at 15:03 .
Try to write more detailed questions. To get an answer, explain what exactly you see the problem, how to reproduce it, what you want to get as a result, etc. Give an example that clearly demonstrates the problem. If the question can be reformulated according to the rules set out in the certificate , edit it .
|
1 answer
enSO - add a function that throws an ArrayList / array
import java.util.ArrayList; import java.util.List; import javax.swing.JFrame; import javax.swing.JScrollPane; import javax.swing.JTable; public class Test { private static ArrayList<String> rowA = new ArrayList(); private static ArrayList<String> rowB = new ArrayList(); private static ArrayList<String> rowC = new ArrayList(); private static ArrayList<String> titel = new ArrayList(); private static ArrayList<ArrayList<String>> table = new ArrayList(); public static void main(String[] args) { titel.add("Column 1"); titel.add("Column 2"); titel.add("Column 3"); addRows("I", "dont", "want"); addRows("to", "search", "by"); addRows("myself", "a simple", "question"); table.add(rowA); table.add(rowB); table.add(rowC); Object[] tempTitel = titel.toArray(); String[][] tempTable = new String[table.size()][]; int i = 0; for (List<String> next : table) { tempTable[i++] = next.toArray(new String[next.size()]); } JTable EndTable = new JTable(tempTable,tempTitel); JFrame frame = new JFrame("Demo"); frame.getContentPane().add(new JScrollPane(EndTable)); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.pack(); frame.setVisible(true); } public static void addRows(String rowa, String rowb, String rowc) { rowA.add(rowa); rowB.add(rowb); rowC.add(rowc); } }
- Thank. But the output is obtained in a row and not in a column. Both the frame and the table do not need to be created; they already are ready. - Binary
- What line is the output?
addRows("I", "dont", "want")
adds a column to the table. The frame and the table was created for tests - now turn and twist. - Denis
|