Results 1 to 3 of 3
  1. #1
    jcman01 is offline Member
    Join Date
    Aug 2011
    Posts
    2
    Rep Power
    0

    Default Redisplay New JTable and Columns

    I am working on a little side project for myself. I want to enter some input in a text area (SQL statement), and receive the results in the output pane (Jtable). However, I cannot get it to work. The following should be runnable by anyone. Can someone tell me what I need to do to get the new column labels (a,b,c) to show when I select the Run SQL menu item?


    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    import javax.swing.table.*;
    import javax.swing.JMenu;
    import javax.swing.JMenuBar;
    import javax.swing.table.AbstractTableModel;
    import javax.swing.JMenuItem;
    import javax.swing.JTable;

    public class ADemo extends JPanel implements ActionListener {
    private JTable table;
    private JSplitPane splitPane;
    private JTextArea textArea;
    private JFrame frame;
    private JMenuBar jmenubar;
    private JMenu jmenu;
    private JMenuItem jmenuitemRun;
    private JMenuItem jmenuitemExit;
    private JScrollPane SQLtextScrollPane;
    private JScrollPane resultsScrollPane;
    private static final long serialVersionUID = 1L;

    private String[] columnNames = {"init"};
    private Object[][] data = {{" "}};
    private TableColumn column;



    public ADemo() {
    frame = new JFrame("ADemozzz");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOS E);

    textArea = new JTextArea();
    textArea.setColumns(80);
    textArea.setRows(25);

    table = new JTable(new initialTableModel());
    table.setPreferredScrollableViewportSize(new Dimension(5, 120));
    table.setFillsViewportHeight(true);

    SQLtextScrollPane = new JScrollPane(textArea);
    resultsScrollPane = new JScrollPane(table);

    splitPane = new JSplitPane(JSplitPane.VERTICAL_SPLIT,SQLtextScroll Pane,resultsScrollPane);
    splitPane.setDividerLocation(0.5);
    splitPane.setSize(new Dimension(500, 500));


    jmenubar = new JMenuBar();
    jmenu = new JMenu("File");
    jmenuitemRun = new JMenuItem("Run SQL");
    jmenuitemExit = new JMenuItem("Exit");
    jmenu.add(jmenuitemRun);
    jmenu.addSeparator();
    jmenu.add(jmenuitemExit);
    jmenubar.add(jmenu);
    jmenuitemRun.addActionListener(this);

    frame.setJMenuBar(jmenubar);
    frame.getContentPane().add(splitPane);
    frame.setExtendedState(JFrame.MAXIMIZED_BOTH);
    frame.pack();
    frame.setVisible(true);
    }


    class initialTableModel extends AbstractTableModel {
    private static final long serialVersionUID = 1L;

    public int getColumnCount() {
    return columnNames.length;
    }

    public int getRowCount() {
    return data.length;
    }

    public String getColumnName(int col) {
    return columnNames[col];
    }

    public Object getValueAt(int row, int col) {
    return data[row][col];
    }
    }


    public void actionPerformed(ActionEvent ae) {
    runSQL();
    }


    public void runSQL() {
    columnNames = new String[3];
    columnNames[0] = "a";
    columnNames[1] = "b";
    columnNames[2] = "c";

    table = new JTable(new initialTableModel());
    table.repaint();
    }



    public static void main(String[] args) throws Exception {
    ADemo f = new ADemo();
    }
    }

  2. #2
    camickr is online now Senior Member
    Join Date
    Jul 2009
    Posts
    1,226
    Rep Power
    6

    Default

    Don't use the "Bold" tags to highlight your code. Instead use the "Code" tags.

    Don't recreate the table, just change the TableModel:

    Java Code:
    //table = new JTable(new initialTableModel());
    //table.repaint();
    table.setModel( new initialTableModel() );
    Also, there is no need for you to create a custom TableModel. Instead use the DefaultTableModel and save yourself some work.

  3. #3
    jcman01 is offline Member
    Join Date
    Aug 2011
    Posts
    2
    Rep Power
    0

Similar Threads

  1. Problem with sorting columns using Jtable
    By aspiring_programmer in forum AWT / Swing
    Replies: 0
    Last Post: 12-02-2010, 02:43 PM
  2. Sorting and hiding columns in JTable
    By ProgrammingPup in forum Advanced Java
    Replies: 1
    Last Post: 11-17-2009, 09:59 PM
  3. How to sort a JTable for multiple columns?
    By BLR in forum Advanced Java
    Replies: 2
    Last Post: 03-16-2009, 10:41 AM
  4. Right Align columns in JTable
    By Laura Warren in forum New To Java
    Replies: 2
    Last Post: 12-18-2008, 09:01 PM
  5. sort columns in jtable
    By Alan in forum AWT / Swing
    Replies: 2
    Last Post: 05-14-2007, 05:18 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •