-1

I'm using model, that I could refresh content of table? But I addedd it to a JPanel, and now my second JPanel, which contans button, goes very deep. I'm using GridLayout for controling size of column(preferredSize()). At start I have http://www.funkyimg.com/u2/3264/877/343679Untitled.jpg and then I have to manually make it bigger to http://www.funkyimg.com/u2/3264/878/776480Untitled2.jpg Source code I'm using from previous post, slightly modified:

public class Arsti3 {
JFrame main = new JFrame("Arst");
JPanel tP = new JPanel(new GridLayout(1,0));
JPanel bP = new JPanel();
JButton one = new JButton("Test");
JButton two = new JButton("Two");
JTable table = new JTable();
DefaultTableModel model;
Vector columnNames = new Vector();
Vector data = new Vector();

Arsti3() {
    main.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    main.setSize(840,400);
    try {
        reloadData();
        model = new DefaultTableModel(data,columnNames);
        table.setModel(model);

        //table.setRowHeight(24);
        table.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
        TableColumn col;
        for(int i=0; i<table.getColumnCount(); i++) {
            col = table.getColumnModel().getColumn(i);
            col.setPreferredWidth(100);
            col.setMaxWidth(500);
        }
        //table.setPreferredScrollableViewportSize(new Dimension(500,500));
        //model.fireTableDataChanged();
        tP.add(new JScrollPane(table));
        bP.add(one);
        bP.add(two);
        main.add(tP,BorderLayout.NORTH);
        main.add(bP,BorderLayout.SOUTH);
    } catch(Exception e) {
        e.printStackTrace();
    }
    main.setVisible(true);

}

private void reloadData() throws ClassNotFoundException, SQLException {
    columnNames.clear();
    data.clear();
    Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
    String Base = "jdbc:odbc:Driver={Microsoft Access Driver (*.mdb)}; DBQ=SL.mdb";
    Connection con = DriverManager.getConnection(Base,"","");
    Statement st = con.createStatement();
    ResultSet res = st.executeQuery("SELECT * FROM Arsti");
    ResultSetMetaData rsmd = res.getMetaData();
    int column = rsmd.getColumnCount();         
    columnNames.addElement("ID");
    columnNames.addElement("Vards");
    columnNames.addElement("Uzvards");
    columnNames.addElement("Dzimums");
    columnNames.addElement("Personas kods");
    columnNames.addElement("Telefona numurs");
    columnNames.addElement("Nodalas ID");
    columnNames.addElement("Amata ID");
    while(res.next()) {
        Vector row = new Vector(column);
        for(int i=1; i<=column; i++) {
            row.addElement(res.getObject(i));
        }
        data.addElement(row);
    }
}

public static void main(String[] args) {
    new Arsti3();
}
}
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
usr999
  • 157
  • 2
  • 7
  • 20
  • I was about to advise using PNG instead of JPG when I thought to check the current images first. I ended up at a site that did not have English text, except for a prompt to 'click something' & no image. I'm out of there! – Andrew Thompson Mar 18 '13 at 18:52
  • For better help sooner, post an [SSCCE](http://sscce.org/). – Andrew Thompson Mar 18 '13 at 18:53
  • -1 for not learning (as I commented in your last question: you _must not_ change the data structure under the feet of the tableModel, instead do the reload with model api) – kleopatra Mar 18 '13 at 18:55
  • @kleopatra if I could understand what are you talking about, I would try to do It)) – usr999 Mar 18 '13 at 18:57
  • which part of _use model api_ is so hard to understand? Read the api doc of DefaultTableModel, find the methods that allow to add a row and ... use them ;-) – kleopatra Mar 18 '13 at 19:00
  • *"if I could understand"* When you do not understand, ***ask*** instead of ignoring the advice! – Andrew Thompson Mar 18 '13 at 19:01
  • @kleopatra's point about the model is right: you can _update_ it in place, as shown below. – trashgod Mar 19 '13 at 01:10

1 Answers1

3

Kudos for using a layout, but don't forget to pack() the enclosing Window. Also,

  • setPreferredScrollableViewportSize() is a bit arbitrary, so you may as well make the Dimension an integral multiple of something.

  • Swing GUI objects should be constructed and manipulated only on the event dispatch thread.

  • Use model.setRowCount(0) to clear your model, rather than replacing it each time.

image

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.GridLayout;
import java.util.Vector;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableColumn;

public class Arsti3 {

    private static final int WIDE = 100;
    private JFrame main = new JFrame("Arst");
    private JPanel tP = new JPanel(new GridLayout());
    private JPanel bP = new JPanel();
    private JButton one = new JButton("One");
    private JButton two = new JButton("Two");
    private Vector<String> columnNames = new Vector<String>();
    private Vector<Object> data = new Vector<Object>();
    private DefaultTableModel model = new DefaultTableModel(data, columnNames);
    private JTable table = new JTable();

    Arsti3() {
        reloadData();
        table.setModel(model);
        table.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
        for (int i = 0; i < table.getColumnCount(); i++) {
            TableColumn col = table.getColumnModel().getColumn(i);
            col.setPreferredWidth(WIDE);
            col.setMaxWidth(WIDE * 5);
        }
        table.setPreferredScrollableViewportSize(new Dimension(
            table.getColumnCount() * WIDE, table.getRowHeight() * 16));
        tP.add(new JScrollPane(table));
        bP.add(one);
        bP.add(two);
        main.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        main.add(tP, BorderLayout.CENTER);
        main.add(bP, BorderLayout.SOUTH);
        main.pack();
        main.setVisible(true);

    }

    private void reloadData() {
        // could be factored out
        columnNames.clear();
        columnNames.addElement("ID");
        columnNames.addElement("Vards");
        columnNames.addElement("Uzvards");
        columnNames.addElement("Dzimums");
        columnNames.addElement("Personas kods");
        columnNames.addElement("Telefona numurs");
        columnNames.addElement("Nodalas ID");
        columnNames.addElement("Amata ID");
        model.setRowCount(0); // clear rows
        // add rows here
    }

    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                new Arsti3();
            }
        });
    }
}
trashgod
  • 203,806
  • 29
  • 246
  • 1,045