Example usage for javax.swing.table DefaultTableModel DefaultTableModel

List of usage examples for javax.swing.table DefaultTableModel DefaultTableModel

Introduction

In this page you can find the example usage for javax.swing.table DefaultTableModel DefaultTableModel.

Prototype

public DefaultTableModel() 

Source Link

Document

Constructs a default DefaultTableModel which is a table of zero columns and zero rows.

Usage

From source file:Balo.MainFrame.java

public MainFrame() throws IOException {
    initComponents();//from w w w .  java  2 s. com

    Object[] columns = { "Tn  vt", "S lng", "Gi tr", "Tr?ng lng" };
    model = new DefaultTableModel();
    model.setColumnIdentifiers(columns);
    getDataFileToJTable();

    Table_Dovat.setModel(model);

    //InitData();
    //getDataFileToJTable();
}

From source file:com.intuit.tank.tools.debugger.VariableDialog.java

public VariableDialog(AgentDebuggerFrame f, Map<String, String> variables) {
    super(f, true);
    this.f = f;/* w  ww .jav  a 2  s  . c  o m*/
    setLayout(new BorderLayout());
    setTitle("View Edit Project Variables");
    DefaultTableModel model = new DefaultTableModel();
    model.addColumn("Variable Name");
    model.addColumn("Variable Value");
    List<String> keys = new ArrayList<String>(variables.keySet());
    Collections.sort(keys);
    for (String key : keys) {
        Object[] data = new Object[2];
        data[0] = key;
        data[1] = variables.get(key);
        model.addRow(data);
    }
    table = new JTable(model);
    table.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
    table.getSelectionModel().addListSelectionListener(this);
    table.setGridColor(Color.GRAY);
    table.setAutoResizeMode(JTable.AUTO_RESIZE_ALL_COLUMNS);
    table.setBorder(BorderFactory.createLineBorder(Color.GRAY));
    table.setShowGrid(true);
    table.getTableHeader().setReorderingAllowed(false);

    JScrollPane sp = new JScrollPane(table);
    JPanel panel = new JPanel(new BorderLayout());

    panel.add(table.getTableHeader(), BorderLayout.NORTH);
    panel.add(sp, BorderLayout.CENTER);

    add(panel, BorderLayout.CENTER);
    add(createButtonPanel(), BorderLayout.SOUTH);
    setSize(new Dimension(800, 600));
    setBounds(new Rectangle(getSize()));
    setPreferredSize(getSize());
    WindowUtil.centerOnParent(this);
}

From source file:GUIpresentacion.GUIInformeAltasBajas.java

public DefaultTableModel mostrar() {

    DefaultTableModel table = new DefaultTableModel();

    ResultSet rs = null;/*from   w  ww  . jav a2 s.c  o  m*/
    String a = Utilidad.convertToSqlDate(fecha_desde.getSelectedDate());
    String b = Utilidad.convertToSqlDate(fecha_hasta.getSelectedDate());

    try {
        rs = ab.buscar(a, b);
    } catch (ClassNotFoundException | SQLException ex) {
        Logger.getLogger(GUIInformeAltasBajas.class.getName()).log(Level.SEVERE, null, ex);
    }

    table = Utilidad.crearTabla(rs);

    return (table);
}

From source file:anslab2.AnsLab2.java

/**
 * Creates new form AnsLab2GUI1// w  w  w. j  a  v a 2 s.c om
 */
public AnsLab2() {
    //dt = new Test();
    model = new DefaultTableModel();
    rawlist = new DefaultListModel();
    ascendingList = new DefaultListModel();

    initComponents();

    TypeGroups.add(AlphabeticSelect);
    TypeGroups.add(StringSelect);
    TypeGroups.add(NumericSelect);
    this.setLocationRelativeTo(null);
}

From source file:NewApplication.java

public void initGUI() {

    tm = new DefaultTableModel();

    tm.addColumn("ID");
    tm.addColumn("Account");

    jTable1.setModel(tm);/*from  w w w .  ja v  a  2s .co  m*/

}

From source file:Main.java

public Component getTreeCellRendererComponent(JTree tree, Object value, boolean selected, boolean expanded,
        boolean leaf, int row, boolean hasFocus) {
    final String v = (String) ((DefaultMutableTreeNode) value).getUserObject();
    table.setModel(new DefaultTableModel() {
        @Override/* w  ww  .  j  ava2s .c om*/
        public int getRowCount() {
            return 2;
        }

        @Override
        public int getColumnCount() {
            return 2;
        }

        @Override
        public Object getValueAt(int row, int column) {
            return v + ":" + row + ":" + column;
        }
    });
    table.setPreferredScrollableViewportSize(table.getPreferredSize());
    return this;
}

From source file:com.clough.android.adbv.view.UpdateTableDialog.java

public UpdateTableDialog(java.awt.Frame parent, boolean modal, String outputResult, String tableName,
        Object[] columnNames) {//from  w ww .j av  a 2 s .  co  m
    super(parent, modal);
    initComponents();
    this.outputResult = outputResult;
    this.tableName = tableName;
    this.columnNames = columnNames;
    this.mainFrame = (MainFrame) parent;
    setTitle("Update table `" + tableName + "`");
    setLocationRelativeTo(null);
    resultTable.setModel(new DefaultTableModel() {

        @Override
        public boolean isCellEditable(int row, int column) {
            return false;
        }

    });
    resultTable.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
    defaultTableModel = (DefaultTableModel) resultTable.getModel();
    for (Object columnName : columnNames) {
        defaultTableModel.addColumn(columnName);
    }
    tableColumnAdjuster = new TableColumnAdjuster(resultTable);

    processResult();
}

From source file:be.ugent.maf.cellmissy.gui.controller.analysis.doseresponse.DoseResponseController.java

protected void resetOnCancel() {
    dataTable.setModel(new DefaultTableModel());
    dRPanel.getGraphicsDRParentPanel().removeAll();
}

From source file:namedatabasescraper.MainWindow.java

private void populateNamesColumn() {
    String[] extensions = { "html", "htm" };
    File dir = new File(this.jDirectoryNameTextField.getText());
    if (dir.exists()) {
        this.htmlFiles = FileUtils.listFiles(dir, extensions, false);
        this.model = new DefaultTableModel();
        model.setNumRows(htmlFiles.size());
        model.setColumnCount(2);//  w w w .  j av a  2 s. c o m
        String[] columnNames = { "File Names", "Number of Words Extracted" };
        model.setColumnIdentifiers(columnNames);
        int rowCount = 0;
        for (File htmlFile : this.htmlFiles) {
            model.setValueAt(htmlFile.getName(), rowCount, 0);
            model.setValueAt("0", rowCount, 1);
            rowCount++;
        }
        this.jResultsTable.setModel(model);
        this.dirname = dir.getName();
    }
}

From source file:dnd.BasicDnD.java

public BasicDnD() {
    super(new BorderLayout());
    JPanel leftPanel = createVerticalBoxPanel();
    JPanel rightPanel = createVerticalBoxPanel();

    //Create a table model.
    DefaultTableModel tm = new DefaultTableModel();
    tm.addColumn("Column 0");
    tm.addColumn("Column 1");
    tm.addColumn("Column 2");
    tm.addColumn("Column 3");
    tm.addRow(new String[] { "Table 00", "Table 01", "Table 02", "Table 03" });
    tm.addRow(new String[] { "Table 10", "Table 11", "Table 12", "Table 13" });
    tm.addRow(new String[] { "Table 20", "Table 21", "Table 22", "Table 23" });
    tm.addRow(new String[] { "Table 30", "Table 31", "Table 32", "Table 33" });

    //LEFT COLUMN
    //Use the table model to create a table.
    table = new JTable(tm);
    leftPanel.add(createPanelForComponent(table, "JTable"));

    //Create a color chooser.
    colorChooser = new JColorChooser();
    leftPanel.add(createPanelForComponent(colorChooser, "JColorChooser"));

    //RIGHT COLUMN
    //Create a textfield.
    textField = new JTextField(30);
    textField.setText("Favorite foods:\nPizza, Moussaka, Pot roast");
    rightPanel.add(createPanelForComponent(textField, "JTextField"));

    //Create a scrolled text area.
    textArea = new JTextArea(5, 30);
    textArea.setText("Favorite shows:\nBuffy, Alias, Angel");
    JScrollPane scrollPane = new JScrollPane(textArea);
    rightPanel.add(createPanelForComponent(scrollPane, "JTextArea"));

    //Create a list model and a list.
    DefaultListModel listModel = new DefaultListModel();
    listModel.addElement("Martha Washington");
    listModel.addElement("Abigail Adams");
    listModel.addElement("Martha Randolph");
    listModel.addElement("Dolley Madison");
    listModel.addElement("Elizabeth Monroe");
    listModel.addElement("Louisa Adams");
    listModel.addElement("Emily Donelson");
    list = new JList(listModel);
    list.setVisibleRowCount(-1);/*  w ww .  ja v  a  2 s  . com*/
    list.getSelectionModel().setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);

    list.setTransferHandler(new TransferHandler() {

        public boolean canImport(TransferHandler.TransferSupport info) {
            // we only import Strings
            if (!info.isDataFlavorSupported(DataFlavor.stringFlavor)) {
                return false;
            }

            JList.DropLocation dl = (JList.DropLocation) info.getDropLocation();
            if (dl.getIndex() == -1) {
                return false;
            }
            return true;
        }

        public boolean importData(TransferHandler.TransferSupport info) {
            if (!info.isDrop()) {
                return false;
            }

            // Check for String flavor
            if (!info.isDataFlavorSupported(DataFlavor.stringFlavor)) {
                displayDropLocation("List doesn't accept a drop of this type.");
                return false;
            }

            JList.DropLocation dl = (JList.DropLocation) info.getDropLocation();
            DefaultListModel listModel = (DefaultListModel) list.getModel();
            int index = dl.getIndex();
            boolean insert = dl.isInsert();
            // Get the current string under the drop.
            String value = (String) listModel.getElementAt(index);

            // Get the string that is being dropped.
            Transferable t = info.getTransferable();
            String data;
            try {
                data = (String) t.getTransferData(DataFlavor.stringFlavor);
            } catch (Exception e) {
                return false;
            }

            // Display a dialog with the drop information.
            String dropValue = "\"" + data + "\" dropped ";
            if (dl.isInsert()) {
                if (dl.getIndex() == 0) {
                    displayDropLocation(dropValue + "at beginning of list");
                } else if (dl.getIndex() >= list.getModel().getSize()) {
                    displayDropLocation(dropValue + "at end of list");
                } else {
                    String value1 = (String) list.getModel().getElementAt(dl.getIndex() - 1);
                    String value2 = (String) list.getModel().getElementAt(dl.getIndex());
                    displayDropLocation(dropValue + "between \"" + value1 + "\" and \"" + value2 + "\"");
                }
            } else {
                displayDropLocation(dropValue + "on top of " + "\"" + value + "\"");
            }

            /**  This is commented out for the basicdemo.html tutorial page.
                       **  If you add this code snippet back and delete the
                       **  "return false;" line, the list will accept drops
                       **  of type string.
                      // Perform the actual import.  
                      if (insert) {
            listModel.add(index, data);
                      } else {
            listModel.set(index, data);
                      }
                      return true;
            */
            return false;
        }

        public int getSourceActions(JComponent c) {
            return COPY;
        }

        protected Transferable createTransferable(JComponent c) {
            JList list = (JList) c;
            Object[] values = list.getSelectedValues();

            StringBuffer buff = new StringBuffer();

            for (int i = 0; i < values.length; i++) {
                Object val = values[i];
                buff.append(val == null ? "" : val.toString());
                if (i != values.length - 1) {
                    buff.append("\n");
                }
            }
            return new StringSelection(buff.toString());
        }
    });
    list.setDropMode(DropMode.ON_OR_INSERT);

    JScrollPane listView = new JScrollPane(list);
    listView.setPreferredSize(new Dimension(300, 100));
    rightPanel.add(createPanelForComponent(listView, "JList"));

    //Create a tree.
    DefaultMutableTreeNode rootNode = new DefaultMutableTreeNode("Mia Familia");
    DefaultMutableTreeNode sharon = new DefaultMutableTreeNode("Sharon");
    rootNode.add(sharon);
    DefaultMutableTreeNode maya = new DefaultMutableTreeNode("Maya");
    sharon.add(maya);
    DefaultMutableTreeNode anya = new DefaultMutableTreeNode("Anya");
    sharon.add(anya);
    sharon.add(new DefaultMutableTreeNode("Bongo"));
    maya.add(new DefaultMutableTreeNode("Muffin"));
    anya.add(new DefaultMutableTreeNode("Winky"));
    DefaultTreeModel model = new DefaultTreeModel(rootNode);
    tree = new JTree(model);
    tree.getSelectionModel().setSelectionMode(TreeSelectionModel.DISCONTIGUOUS_TREE_SELECTION);
    JScrollPane treeView = new JScrollPane(tree);
    treeView.setPreferredSize(new Dimension(300, 100));
    rightPanel.add(createPanelForComponent(treeView, "JTree"));

    //Create the toggle button.
    toggleDnD = new JCheckBox("Turn on Drag and Drop");
    toggleDnD.setActionCommand("toggleDnD");
    toggleDnD.addActionListener(this);

    JSplitPane splitPane = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT, leftPanel, rightPanel);
    splitPane.setOneTouchExpandable(true);

    add(splitPane, BorderLayout.CENTER);
    add(toggleDnD, BorderLayout.PAGE_END);
    setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));
}