fubarable

Using vector<String> with JTable

Nov 9th, 2014
274
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.27 KB | None | 0 0
  1. import java.util.Vector;
  2. import javax.swing.*;
  3.  
  4. public class Foo2 extends JPanel {
  5.    private String[] nameLst = { "One", "Two", "Three", "Four" };
  6.    private JTable namTab;
  7.    private JPanel namPan = new JPanel();
  8.  
  9.    public Foo2() {
  10.       printNames();
  11.       add(namPan);
  12.    }
  13.  
  14.    public void printNames() {
  15.       Vector<String> label = new Vector<String>();
  16.       Vector<Vector<String>> helper = new Vector<Vector<String>>();
  17.       for (String nametmp : nameLst) {
  18.          Vector<String> a = new Vector<String>();
  19.          a.add(nametmp);
  20.          helper.add((Vector<String>/* just a cast */) a.clone());
  21.       }
  22.       label.add("Currently active:");
  23.       namTab = new JTable(helper, label);
  24.       namPan.add(new JScrollPane(namTab));
  25.    }
  26.  
  27.    private static void createAndShowGui() {
  28.       Foo2 mainPanel = new Foo2();
  29.  
  30.       JFrame frame = new JFrame("Foo2");
  31.       frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
  32.       frame.getContentPane().add(mainPanel);
  33.       frame.pack();
  34.       frame.setLocationByPlatform(true);
  35.       frame.setVisible(true);
  36.    }
  37.  
  38.    public static void main(String[] args) {
  39.       SwingUtilities.invokeLater(new Runnable() {
  40.          public void run() {
  41.             createAndShowGui();
  42.          }
  43.       });
  44.    }
  45. }
Add Comment
Please, Sign In to add comment