How to bind JTable and data in a text file ?

Discussion in 'Java' started by tobleron, Nov 4, 2008.

  1. tobleron

    tobleron Guest

    Hi,

    I'm using NetBeans 6.1. I have a form with JTable inside, and I have a
    text file contains data (file name ; ID; person name) :

    file1.dcm;ID001;Tobleron
    file2.dcm;ID002;Lucas
    file3.dcm;ID003;Mark

    I know how to bind JTable with database such as MySQL. But how to bind
    data in a text file into JTable ? Please help. Thank you in advance.
    tobleron, Nov 4, 2008
    #1
    1. Advertising

  2. tobleron wrote:
    > Hi,
    >
    > I'm using NetBeans 6.1. I have a form with JTable inside, and I have a
    > text file contains data (file name ; ID; person name) :
    >
    > file1.dcm;ID001;Tobleron
    > file2.dcm;ID002;Lucas
    > file3.dcm;ID003;Mark
    >
    > I know how to bind JTable with database such as MySQL. But how to bind
    > data in a text file into JTable ? Please help. Thank you in advance.



    A.
    Read the text file, split each line, add those elements to an array of
    arrays (Object[][]), pass that array to a JTable constructor.
    Optionally use a List within the read-loop and construct the array from
    it after the last record is read.

    or

    B.
    Have a subclass of AbstractTableModel read the text file.


    http://java.sun.com/docs/books/tutorial/uiswing/components/table.html

    --
    RGB
    RedGrittyBrick, Nov 4, 2008
    #2
    1. Advertising

  3. tobleron

    tobleron Guest

    @RGB

    I prefer with A option. But since my JTable component was created by
    NetBeans protected code, how can I modify it ?
    tobleron, Nov 4, 2008
    #3
  4. tobleron wrote:
    > @RGB
    >
    > I prefer with A option. But since my JTable component was created by
    > NetBeans protected code, how can I modify it ?


    Just edit the source code. How else?

    If NetBeans was getting in my way, I'd stop using it! Probably you just
    need to read some NetBeans IDE tutorials and better understand which
    parts of the source code are editable and which parts are generated by
    NetBeans such that any changes you make will be subsequently overwritten
    by NetBeans. Usually comments in the code identify the start and end of
    such sections.

    NetBeans isn't a straightjacket, maybe the way you are using it is
    preventing you from learning Java. I'd stop using those wizardy buttons
    and start using it as a glorified text editor.

    --
    RGB
    RedGrittyBrick, Nov 4, 2008
    #4
  5. tobleron wrote:
    > @RGB
    >
    > I prefer with A option. But since my JTable component was created by
    > NetBeans protected code, how can I modify it ?


    1. Start Notepad
    2. Cut & paste the text below into it
    3. Save as C:\temp\FileTable.java
    4. Open a Command Prompt and enter these commands
    5. cd \temp
    6. javac FileTable.java
    7. java FileTable
    8. Ponder if NetBeans is getting in the way of an education.
    9. Find an evening course at your local college.

    -----------------------------------8<----------------------------------
    import java.awt.BorderLayout;
    import java.io.BufferedReader;
    import java.io.FileReader;
    import java.io.FileWriter;
    import java.io.IOException;
    import java.util.ArrayList;
    import java.util.List;

    import javax.swing.JFrame;
    import javax.swing.JPanel;
    import javax.swing.JScrollPane;
    import javax.swing.JTable;
    import javax.swing.SwingUtilities;
    import javax.swing.SwingWorker;
    import javax.swing.table.AbstractTableModel;

    /**
    * @author: RedGrittyBrick
    */
    public class FileTable {

    public static void main(String[] args) {
    SwingUtilities.invokeLater(new Runnable() {
    public void run() {
    new FileTable().createAndShowGUI();
    }
    });
    }

    private static final String FILENAME = "ID.txt";

    private void createAndShowGUI() {

    final TextFileModel model = new TextFileModel();
    new SwingWorker() {
    @Override
    protected Object doInBackground() throws Exception {
    // Exception handling omitted for brevity - bad code!
    if (!f.isFile())
    writeFile(); // for 1st test only
    model.readFile();
    return null;
    }
    }.execute();

    JTable table = new JTable(model);

    JPanel p = new JPanel(new BorderLayout());
    p.add(new JScrollPane(table), BorderLayout.CENTER);

    JFrame f = new JFrame("");
    f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    f.add(p);
    f.pack();
    f.setLocationRelativeTo(null);
    f.setVisible(true);
    }

    // For testing only
    private void writeFile() {
    try {
    FileWriter fw = new FileWriter(FILENAME);
    fw.write("file1.dcm;ID001;Tobleron\n");
    fw.write("file2.dcm;ID002;Lucas\n");
    fw.write("file3.dcm;ID003;Mark\n");
    fw.close();
    } catch (IOException e) {
    System.err.println(e.getMessage());
    System.exit(1);
    }
    }

    class TextFileModel extends AbstractTableModel {

    List<ID> cache = new ArrayList<ID>();

    public void readFile() throws IOException {
    FileReader fr = new FileReader(FILENAME);
    BufferedReader br = new BufferedReader(fr);
    String s;
    while ((s = br.readLine()) != null) {
    String[] column = s.split(";");
    ID id = new ID(column[0], column[1], column[2]);
    cache.add(id);
    }
    fr.close();
    fireTableRowsInserted(0, cache.size() - 1);
    }

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

    @Override
    public int getRowCount() {
    return cache.size();
    }

    @Override
    public Object getValueAt(int row, int column) {
    ID id = cache.get(row);
    switch (column) {
    case 0:
    return id.fileName;
    case 1:
    return id.fileID;
    case 2:
    return id.personName;
    }
    return null;
    }

    }

    class ID {
    public String fileName, fileID, personName;

    ID(String fileName, String fileID, String personName) {
    this.fileName = fileName;
    this.fileID = fileID;
    this.personName = personName;
    }

    }

    }
    -----------------------------------8<----------------------------------


    --
    RGB
    RedGrittyBrick, Nov 4, 2008
    #5
  6. tobleron

    Nigel Wade Guest

    tobleron wrote:

    > @RGB
    >
    > I prefer with A option. But since my JTable component was created by
    > NetBeans protected code, how can I modify it ?


    Select the JTable component and modify either its Properties or Code via the
    Properties window.

    You can set the variable name for the model via the Properties, or generate
    custom code for various points in the lifetime of the JTable object
    (pre-/post-creation, actual creation, pre-/post-init etc.).

    --
    Nigel Wade
    Nigel Wade, Nov 4, 2008
    #6
  7. tobleron

    Mark Space Guest

    tobleron wrote:
    > @RGB
    >
    > I prefer with A option. But since my JTable component was created by
    > NetBeans protected code, how can I modify it ?


    I'm curious: how do you bind a database to a JTable in NetBeans, got a
    tutorial somewhere?

    To answer your question, you should edit the source, as indicated. Add
    a method to the JFrame/form that says something like "setTableModel(
    TableModel tm )".

    Then use DefaultTableModel. It takes an array, just like the
    constructor of JTable. So you can just:

    String[][] data = {{"Joe","Smith","123"},
    {"Jane","Doe","456"}};
    String [] names = {"First","Last","ID"};
    DefaultTableModel dtm = new DefaultTableModel( data, names );

    Read the file in, set up the DTM as shown, and use the (now public)
    setTabelModel method to add the DTM to the protected JTable. You can
    also add and remove rows and columns to the DTM later, you don't have to
    construct it all at once.

    This way, you can use the object NetBeans has made, and still configure
    it externally (to the JFrame) at runtime.
    Mark Space, Nov 4, 2008
    #7
  8. tobleron

    Mark Space Guest

    Nigel Wade wrote:

    > Select the JTable component and modify either its Properties or Code via the
    > Properties window.
    >
    > You can set the variable name for the model via the Properties, or generate
    > custom code for various points in the lifetime of the JTable object
    > (pre-/post-creation, actual creation, pre-/post-init etc.).
    >


    The problem with this is that it requires the variable with the model to
    be available to the init code in the constructor. Which means you
    couldn't modify the JTable later, which is pretty inconvenient.

    It's best to learn to the click on the source button, and edit the
    source. It only requires a little Java knowledge.

    NetBeans gives you code like this

    public class MyFrame extends JFrame {

    public MyFrame() {
    initComponents();
    }

    private void initComponents() {
    // generated code...
    }

    // generated variables
    JTable table1;
    }

    As long as you don't touch those two generated sections
    (initComponents() and the variables at the end), you can do whatever you
    want. Adding a public method to MyFrame is pretty easy.

    public class MyFrame extends JFrame {

    public MyFrame() {
    initComponents();
    }

    public void setTableModel( TableModel tm ) {
    table1.setTableModel( tm );
    }

    private void initComponents() {
    // generated code...
    }

    // generated variables
    JTable table1;
    }

    There might be a fancier way to do this with the GUI editor in NetBeans,
    but that's how I do it.
    Mark Space, Nov 4, 2008
    #8
  9. tobleron

    Lew Guest

    Mark Space wrote:
    > Nigel Wade wrote:
    >
    >> Select the JTable component and modify either its Properties or Code
    >> via the
    >> Properties window.
    >>
    >> You can set the variable name for the model via the Properties, or
    >> generate
    >> custom code for various points in the lifetime of the JTable object
    >> (pre-/post-creation, actual creation, pre-/post-init etc.).
    >>

    >
    > The problem with this is that it requires the variable with the model to
    > be available to the init code in the constructor. Which means you
    > couldn't modify the JTable later, which is pretty inconvenient.
    >
    > It's best to learn to the click on the source button, and edit the
    > source. It only requires a little Java knowledge.
    >
    > NetBeans gives you code like this
    >
    > public class MyFrame extends JFrame {
    >
    > public MyFrame() {
    > initComponents();
    > }
    >
    > private void initComponents() {
    > // generated code...
    > }
    >
    > // generated variables
    > JTable table1;
    > }
    >
    > As long as you don't touch those two generated sections
    > (initComponents() and the variables at the end), you can do whatever you
    > want. Adding a public method to MyFrame is pretty easy.
    >
    > public class MyFrame extends JFrame {
    >
    > public MyFrame() {
    > initComponents();
    > }
    >
    > public void setTableModel( TableModel tm ) {
    > table1.setTableModel( tm );
    > }
    >
    > private void initComponents() {
    > // generated code...
    > }
    >
    > // generated variables
    > JTable table1;
    > }
    >
    > There might be a fancier way to do this with the GUI editor in NetBeans,
    > but that's how I do it.


    RedGrittyBrick said:
    > If NetBeans was getting in my way, I'd stop using it!
    > ...
    > NetBeans isn't a straightjacket, maybe the way you are using it
    > is preventing you from learning Java.


    While the form generator in NB imposes a certain structure on the GUI code, it
    doesn't prevent anything reasonable that I've ever heard.

    --
    Lew
    Lew, Nov 5, 2008
    #9
  10. tobleron

    tobleron Guest

    >
    > I'm curious: how do you bind a database to a JTable in NetBeans, got a
    > tutorial somewhere?
    >


    Just open your NetBeans Help or see in the Sun's website, brother :)
    tobleron, Nov 5, 2008
    #10
  11. tobleron

    tobleron Guest

    @All,

    I tried to write my own code, because I had to fit this part into the
    previous java files. But I faced problem in this part :

    -------
    Object dataobject = "new Object [][] {" + data + "}, new String []
    {'DICOM File', 'Patient ID', 'Patient Name', 'Upload'}";

    tableDCM.setModel(new javax.swing.table.DefaultTableModel(
    dataobject
    )
    -------

    The error message is :

    Can not find symbol
    symbol : constructor DefaultTableModel(java.lang.Object)
    location : class javax.swing.table.DefaultTableModel

    How to solve it ?
    tobleron, Nov 5, 2008
    #11
  12. tobleron

    Nigel Wade Guest

    Mark Space wrote:

    > Nigel Wade wrote:
    >
    >> Select the JTable component and modify either its Properties or Code via the
    >> Properties window.
    >>
    >> You can set the variable name for the model via the Properties, or generate
    >> custom code for various points in the lifetime of the JTable object
    >> (pre-/post-creation, actual creation, pre-/post-init etc.).
    >>

    >
    > The problem with this is that it requires the variable with the model to
    > be available to the init code in the constructor.


    Yes, of course it does. I was assuming that much.

    > Which means you
    > couldn't modify the JTable later, which is pretty inconvenient.


    Sorry, I don't understand this statement. After the initialization code is
    complete you can do whatever you want with the JTable, just as you can with any
    Component regardless of whether you create it manually or using NetBeans GUI.
    The pre-/post-creation, creation, pre-/post-init etc. actually allow you to
    modify how the component is created in the initComponents() method which
    NetBeans doesn't allow you to modify manually.

    >
    > It's best to learn to the click on the source button, and edit the
    > source. It only requires a little Java knowledge.


    But NetBeans doesn't allow you to modify that part of the source. If you want to
    create a JTable with a model as the parameter to the constructor you can only
    do it via the above means.

    >
    > NetBeans gives you code like this
    >
    > public class MyFrame extends JFrame {
    >
    > public MyFrame() {
    > initComponents();
    > }
    >
    > private void initComponents() {
    > // generated code...
    > }
    >
    > // generated variables
    > JTable table1;
    > }
    >
    > As long as you don't touch those two generated sections
    > (initComponents() and the variables at the end), you can do whatever you
    > want. Adding a public method to MyFrame is pretty easy.
    >
    > public class MyFrame extends JFrame {
    >
    > public MyFrame() {
    > initComponents();
    > }
    >
    > public void setTableModel( TableModel tm ) {
    > table1.setTableModel( tm );
    > }
    >
    > private void initComponents() {
    > // generated code...
    > }
    >
    > // generated variables
    > JTable table1;
    > }
    >
    > There might be a fancier way to do this with the GUI editor in NetBeans,
    > but that's how I do it.


    --
    Nigel Wade
    Nigel Wade, Nov 5, 2008
    #12
  13. tobleron

    Lew Guest

    tobleron wrote:
    > @All,
    >
    > I tried to write my own code, because I had to fit this part into the
    > previous java files. But I faced problem in this part :
    >
    > -------
    > Object dataobject = "new Object [][] {" + data + "}, new String []
    > {'DICOM File', 'Patient ID', 'Patient Name', 'Upload'}";
    >
    > tableDCM.setModel(new javax.swing.table.DefaultTableModel(
    > dataobject
    > )
    > -------
    >
    > The error message is :
    >
    > Can not find symbol
    > symbol : constructor DefaultTableModel(java.lang.Object)
    > location : class javax.swing.table.DefaultTableModel
    >
    > How to solve it ?


    Read the Javadocs:
    <http://java.sun.com/javase/6/docs/api/javax/swing/table/DefaultTableModel.html>
    Use a constructor that actually exists.

    You tried to use a DefaultTableModel constructor that takes a single object (a
    'String') argument. There is not any such constructor.

    The fact that the 'String' contains a representation of pseudocode doesn't
    make it anything other than a 'String'.

    --
    Lew
    Lew, Nov 5, 2008
    #13
  14. tobleron wrote:
    > @All,
    >
    > I tried to write my own code, because I had to fit this part into the
    > previous java files. But I faced problem in this part :
    >
    > -------
    > Object dataobject = "new Object [][] {" + data + "}, new String []
    > {'DICOM File', 'Patient ID', 'Patient Name', 'Upload'}";
    >
    > tableDCM.setModel(new javax.swing.table.DefaultTableModel(
    > dataobject
    > )
    > -------
    >
    > The error message is :
    >
    > Can not find symbol
    > symbol : constructor DefaultTableModel(java.lang.Object)
    > location : class javax.swing.table.DefaultTableModel
    >
    > How to solve it ?


    You probably meant

    Object[][] values = new Object [][] { data.split(";") };

    But the above won't work - see the code I provided earlier for creating
    an Object[][] from your text file.

    String[] heading = new String []
    {'DICOM File', 'Patient ID', 'Patient Name', 'Upload'}";

    tableDCM.setModel(new DefaultTableModel(values, heading));

    I can't imagine what you were thinking!

    --
    RGB
    RedGrittyBrick, Nov 5, 2008
    #14
  15. tobleron

    Lew Guest

    RedGrittyBrick wrote:
    >    String[] heading = new String []
    >      {'DICOM File', 'Patient ID', 'Patient Name', 'Upload'}";
    >
    >    tableDCM.setModel(new DefaultTableModel(values, heading));


    Gotta get those quote marks straightened out.

    > I can't imagine what you were thinking!


    For the OP - perhaps the tutorial will help.
    <http://java.sun.com/docs/books/tutorial/index.html>

    --
    Lew
    Lew, Nov 5, 2008
    #15
  16. tobleron

    tobleron Guest

    >
    > I can't imagine what you were thinking!
    >


    Here is my code. Actually I tried to collect data from a text file and
    put them into JTable. But NetBeans shows warning at
    tableDCM.setModel() command.

    /*
    * DCMUpload.java
    *
    * Created on November 4, 2008, 11:09 PM
    */

    package ecgterminal3;
    ;
    import java.io.*;

    /**
    *
    * @author freebird
    */
    public class DCMUpload extends javax.swing.JDialog {

    public DCMUpload(javax.swing.JFrame app) {
    //super(parent);
    initComponents();
    }

    /** Creates new form DCMUpload */
    public DCMUpload(java.awt.Frame parent, boolean modal) {
    super(parent, modal);
    initComponents();
    }


    /** This method is called from within the constructor to
    * initialize the form.
    * WARNING: Do NOT modify this code. The content of this method is
    * always regenerated by the Form Editor.
    */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">
    private void initComponents() {


    File file2 = new File("D:\\NetBeanProject\\ECGTerminal3\\src\
    \ecgterminal3\\ToBeUploaded.txt");
    BufferedReader reader2 = null;
    try{
    reader2 = new BufferedReader(new FileReader(file2));
    }catch (FileNotFoundException e) {
    }

    String text2 = null;
    String[] words = null;

    jPanel1 = new javax.swing.JPanel();
    jScrollPane1 = new javax.swing.JScrollPane();
    tableDCM = new javax.swing.JTable();
    jLabel1 = new javax.swing.JLabel();
    uploadButton = new javax.swing.JButton();
    cancelButton = new javax.swing.JButton();


    setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
    setName("Form"); // NOI18N

    jPanel1.setName("jPanel1"); // NOI18N

    jScrollPane1.setName("jScrollPane1"); // NOI18N


    String data = null;
    try {
    data = "";
    while ((text2 = reader2.readLine()) != null)
    {
    words = text2.split(";");
    data = data + "{" + words[0] + "," + words[1] + "," +
    words[2] + "," + new Boolean(false) + "},";
    }
    data = data.substring(0, data.length()-1);
    }catch (IOException e) {
    }
    //String field = "DICOM File", "Patient's ID", "Patient's
    Name", "Upload";
    Object dataobject = "new Object [][] {" + data + "}, new
    String [] {'DICOM File', 'Patient ID', 'Patient Name', 'Upload'}";

    tableDCM.setModel(new javax.swing.table.DefaultTableModel(
    dataobject
    /*new Object [][] {
    {words[0], words[1], words[2], new Boolean(false)},
    {"bbb1", "bbb2", "bbb3", new Boolean(false)},
    {"ccc1", "ccc2", "ccc3", new Boolean(false)},
    {"ddd1", "ddd2", "ddd3", new Boolean(false)},
    {"eee1", "eee2", "eee3", new Boolean(false)}
    },
    new String [] {
    "DICOM File", "Patient's ID", "Patient's Name",
    "Upload"
    }*/
    ) {
    boolean[] canEdit = new boolean [] {
    false, false, false, false
    };

    public boolean isCellEditable(int rowIndex, int
    columnIndex) {
    return canEdit [columnIndex];
    }
    });
    tableDCM.setName("tableDCM"); // NOI18N

    tableDCM.setSelectionMode(javax.swing.ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);
    jScrollPane1.setViewportView(tableDCM);
    org.jdesktop.application.ResourceMap resourceMap =
    org.jdesktop.application.Application.getInstance(ecgterminal3.Main.class).getContext().getResourceMap(DCMUpload.class);

    tableDCM.getColumnModel().getColumn(0).setHeaderValue(resourceMap.getString("tableDCM.columnModel.title0")); //
    NOI18N

    tableDCM.getColumnModel().getColumn(1).setHeaderValue(resourceMap.getString("tableDCM.columnModel.title1")); //
    NOI18N

    tableDCM.getColumnModel().getColumn(2).setHeaderValue(resourceMap.getString("tableDCM.columnModel.title2")); //
    NOI18N

    tableDCM.getColumnModel().getColumn(3).setHeaderValue(resourceMap.getString("tableDCM.columnModel.title3")); //
    NOI18N

    jLabel1.setFont(resourceMap.getFont("jLabel1.font")); //
    NOI18N
    jLabel1.setText(resourceMap.getString("jLabel1.text")); //
    NOI18N
    jLabel1.setName("jLabel1"); // NOI18N


    uploadButton.setText(resourceMap.getString("uploadButton.text")); //
    NOI18N
    uploadButton.setName("uploadButton"); // NOI18N


    cancelButton.setText(resourceMap.getString("cancelButton.text")); //
    NOI18N
    cancelButton.setName("cancelButton"); // NOI18N

    javax.swing.GroupLayout jPanel1Layout = new
    javax.swing.GroupLayout(jPanel1);
    jPanel1.setLayout(jPanel1Layout);
    jPanel1Layout.setHorizontalGroup(

    jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
    .addGroup(jPanel1Layout.createSequentialGroup()
    .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
    .addGroup(jPanel1Layout.createSequentialGroup()
    .addContainerGap()
    .addComponent(jLabel1))
    .addGroup(jPanel1Layout.createSequentialGroup()
    .addGap(127, 127, 127)
    .addComponent(uploadButton)
    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
    .addComponent(cancelButton)))
    .addContainerGap(133, Short.MAX_VALUE))
    .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
    .addGroup(jPanel1Layout.createSequentialGroup()
    .addGap(12, 12, 12)
    .addComponent(jScrollPane1,
    javax.swing.GroupLayout.PREFERRED_SIZE, 375,
    javax.swing.GroupLayout.PREFERRED_SIZE)
    .addContainerGap(13, Short.MAX_VALUE)))
    );
    jPanel1Layout.setVerticalGroup(

    jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
    .addGroup(jPanel1Layout.createSequentialGroup()
    .addContainerGap()
    .addComponent(jLabel1)
    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED,
    230, Short.MAX_VALUE)
    .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
    .addComponent(cancelButton)
    .addComponent(uploadButton))
    .addGap(21, 21, 21))
    .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING,
    jPanel1Layout.createSequentialGroup()
    .addContainerGap(39, Short.MAX_VALUE)
    .addComponent(jScrollPane1,
    javax.swing.GroupLayout.PREFERRED_SIZE, 201,
    javax.swing.GroupLayout.PREFERRED_SIZE)
    .addGap(60, 60, 60)))
    );

    javax.swing.GroupLayout layout = new
    javax.swing.GroupLayout(getContentPane());
    getContentPane().setLayout(layout);
    layout.setHorizontalGroup(

    layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
    .addGap(0, 400, Short.MAX_VALUE)
    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
    .addComponent(jPanel1,
    javax.swing.GroupLayout.DEFAULT_SIZE,
    javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
    );
    layout.setVerticalGroup(

    layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
    .addGap(0, 300, Short.MAX_VALUE)
    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
    .addComponent(jPanel1,
    javax.swing.GroupLayout.DEFAULT_SIZE,
    javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
    );

    pack();
    }// </editor-fold>

    /**
    * @param args the command line arguments
    */
    public static void main(String args[]) {
    java.awt.EventQueue.invokeLater(new Runnable() {
    public void run() {
    DCMUpload dialog = new DCMUpload(new
    javax.swing.JFrame(), true);
    dialog.addWindowListener(new
    java.awt.event.WindowAdapter() {
    public void
    windowClosing(java.awt.event.WindowEvent e) {
    System.exit(0);
    }
    });
    dialog.setVisible(true);
    }
    });
    }

    // Variables declaration - do not modify
    private javax.swing.JButton cancelButton;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTable tableDCM;
    private javax.swing.JButton uploadButton;
    // End of variables declaration

    }
    tobleron, Nov 8, 2008
    #16
  17. tobleron

    Lew Guest

    tobleron wrote:
    > //String field = "DICOM File", "Patient's ID", "Patient's
    > Name", "Upload";
    > Object dataobject = "new Object [][] {" + data + "}, new
    > String [] {'DICOM File', 'Patient ID', 'Patient Name', 'Upload'}";
    >
    > tableDCM.setModel(new javax.swing.table.DefaultTableModel(
    > dataobject
    > ) {
    > boolean[] canEdit = new boolean [] {
    > false, false, false, false
    > };
    >
    > public boolean isCellEditable(int rowIndex, int
    > columnIndex) {
    > return canEdit [columnIndex];
    > }
    > });


    I repeat, there is no constructor of DefaultTableModel that takes a single
    'Object' parameter. Read the Javadocs. That constructor does not exist.

    The Javadocs:
    <http://java.sun.com/javase/6/docs/api/javax/swing/table/DefaultTableModel.html>

    You will note a no-arg constructor, one that takes two ints, one that takes an
    'Object [][]' and an 'Object []', one that takes an 'Object []' and an int,
    one that takes a 'Vector' (yecch) and an int, and one that takes two 'Vector's
    (yecch squared). Not one single solitary constructor that takes a single
    'Object' parameter.

    --
    Lew
    Lew, Nov 8, 2008
    #17
  18. tobleron wrote:
    >
    > Here is my code.


    Are you sure it is all your code? I'm sorry to say this, but it looks
    like the tattered remnants of someone else's code after being mangled by
    someone who doesn't know Java.

    > Actually I tried to collect data from a text file and
    > put them into JTable. But NetBeans shows warning at
    > tableDCM.setModel() command.
    >
    > /*
    > * DCMUpload.java
    > *
    > * Created on November 4, 2008, 11:09 PM
    > */
    >
    > package ecgterminal3;
    > ;
    > import java.io.*;
    >
    > /**
    > *
    > * @author freebird



    Is freebird you tobleron? There is another poster named freebird with
    history of posting to comp.lang.java

    > */
    > public class DCMUpload extends javax.swing.JDialog {
    >
    > public DCMUpload(javax.swing.JFrame app) {
    > //super(parent);


    It looks like this was intended to be either
    public DCMUpload(JFrame app) {
    super(app);
    or
    public DCMUpload(JFrame parent) {
    super(parent);
    the latter is consistent with the following constructor.


    > initComponents();
    > }
    >
    > /** Creates new form DCMUpload */
    > public DCMUpload(java.awt.Frame parent, boolean modal) {
    > super(parent, modal);
    > initComponents();
    > }
    >
    >
    > /** This method is called from within the constructor to
    > * initialize the form.
    > * WARNING: Do NOT modify this code. The content of this method is
    > * always regenerated by the Form Editor.
    > */


    So the whole of initComponents is generated by netbeans and should not
    be edited?

    > @SuppressWarnings("unchecked")
    > // <editor-fold defaultstate="collapsed" desc="Generated Code">
    > private void initComponents() {
    >
    >
    > File file2 = new File("D:\\NetBeanProject\\ECGTerminal3\\src\
    > \ecgterminal3\\ToBeUploaded.txt");
    > BufferedReader reader2 = null;
    > try{
    > reader2 = new BufferedReader(new FileReader(file2));
    > }catch (FileNotFoundException e) {
    > }
    >
    > String text2 = null;
    > String[] words = null;
    >
    > jPanel1 = new javax.swing.JPanel();
    > jScrollPane1 = new javax.swing.JScrollPane();
    > tableDCM = new javax.swing.JTable();
    > jLabel1 = new javax.swing.JLabel();
    > uploadButton = new javax.swing.JButton();
    > cancelButton = new javax.swing.JButton();
    >
    >
    > setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
    > setName("Form"); // NOI18N
    >
    > jPanel1.setName("jPanel1"); // NOI18N
    >
    > jScrollPane1.setName("jScrollPane1"); // NOI18N
    >
    >


    I have a hard time believing that netbeans created the following code.
    If the following code is created by tobleron it should not be in
    initComponents().

    > String data = null;
    > try {
    > data = "";


    Do you want data to be initialised to null or to "", please make up your
    mind!

    > while ((text2 = reader2.readLine()) != null)


    Variable names like text2 make me wonder what text1 represents!
    Is there a text1 in the same scope?

    > {
    > words = text2.split(";");
    > data = data + "{" + words[0] + "," + words[1] + "," +
    > words[2] + "," + new Boolean(false) + "},";
    > }


    What is this trying to accomplish? Please explain the reasoning for
    having your program construct, at run time, a fragment of source code in
    a String?

    > data = data.substring(0, data.length()-1);
    > }catch (IOException e) {
    > }


    Don't catch exceptions if you are going to ignore them. At the very
    least emit a stack trace!

    > //String field = "DICOM File", "Patient's ID", "Patient's
    > Name", "Upload";


    You can't assign a list of values to a String variable.


    > Object dataobject = "new Object [][] {" + data + "}, new
    > String [] {'DICOM File', 'Patient ID', 'Patient Name', 'Upload'}";


    Why is this still here? Didn't I and other people point out how wrong
    this was and suggest syntactically valid working ways to assign useful
    values to a String [].


    >
    > tableDCM.setModel(new javax.swing.table.DefaultTableModel(
    > dataobject
    > /*new Object [][] {
    > {words[0], words[1], words[2], new Boolean(false)},
    > {"bbb1", "bbb2", "bbb3", new Boolean(false)},
    > {"ccc1", "ccc2", "ccc3", new Boolean(false)},
    > {"ddd1", "ddd2", "ddd3", new Boolean(false)},
    > {"eee1", "eee2", "eee3", new Boolean(false)}
    > },
    > new String [] {
    > "DICOM File", "Patient's ID", "Patient's Name",
    > "Upload"
    > }*/


    Do omit commented out code, it is very distracting.

    > ) {
    > boolean[] canEdit = new boolean [] {
    > false, false, false, false
    > };


    I do hope we are back to netbeans generated code.

    >
    > public boolean isCellEditable(int rowIndex, int
    > columnIndex) {
    > return canEdit [columnIndex];
    > }
    > });
    > tableDCM.setName("tableDCM"); // NOI18N
    >
    > tableDCM.setSelectionMode(javax.swing.ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);
    > jScrollPane1.setViewportView(tableDCM);
    > org.jdesktop.application.ResourceMap resourceMap =
    > org.jdesktop.application.Application.getInstance(ecgterminal3.Main.class).getContext().getResourceMap(DCMUpload.class);
    >
    > tableDCM.getColumnModel().getColumn(0).setHeaderValue(resourceMap.getString("tableDCM.columnModel.title0")); //
    > NOI18N
    >
    > tableDCM.getColumnModel().getColumn(1).setHeaderValue(resourceMap.getString("tableDCM.columnModel.title1")); //
    > NOI18N
    >
    > tableDCM.getColumnModel().getColumn(2).setHeaderValue(resourceMap.getString("tableDCM.columnModel.title2")); //
    > NOI18N
    >
    > tableDCM.getColumnModel().getColumn(3).setHeaderValue(resourceMap.getString("tableDCM.columnModel.title3")); //
    > NOI18N


    Earlier you were trying to assign column headers. From the preceding few
    lines, I have the impression that netbeans provides a means for you to
    do that.

    >
    > jLabel1.setFont(resourceMap.getFont("jLabel1.font")); //
    > NOI18N
    > jLabel1.setText(resourceMap.getString("jLabel1.text")); //
    > NOI18N
    > jLabel1.setName("jLabel1"); // NOI18N
    >
    >
    > uploadButton.setText(resourceMap.getString("uploadButton.text")); //
    > NOI18N
    > uploadButton.setName("uploadButton"); // NOI18N
    >
    >
    > cancelButton.setText(resourceMap.getString("cancelButton.text")); //
    > NOI18N
    > cancelButton.setName("cancelButton"); // NOI18N
    >
    > javax.swing.GroupLayout jPanel1Layout = new
    > javax.swing.GroupLayout(jPanel1);
    > jPanel1.setLayout(jPanel1Layout);
    > jPanel1Layout.setHorizontalGroup(
    >
    > jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
    > .addGroup(jPanel1Layout.createSequentialGroup()
    > .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
    > .addGroup(jPanel1Layout.createSequentialGroup()
    > .addContainerGap()
    > .addComponent(jLabel1))
    > .addGroup(jPanel1Layout.createSequentialGroup()
    > .addGap(127, 127, 127)
    > .addComponent(uploadButton)
    > .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
    > .addComponent(cancelButton)))
    > .addContainerGap(133, Short.MAX_VALUE))
    > .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
    > .addGroup(jPanel1Layout.createSequentialGroup()
    > .addGap(12, 12, 12)
    > .addComponent(jScrollPane1,
    > javax.swing.GroupLayout.PREFERRED_SIZE, 375,
    > javax.swing.GroupLayout.PREFERRED_SIZE)
    > .addContainerGap(13, Short.MAX_VALUE)))
    > );
    > jPanel1Layout.setVerticalGroup(
    >
    > jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
    > .addGroup(jPanel1Layout.createSequentialGroup()
    > .addContainerGap()
    > .addComponent(jLabel1)
    > .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED,
    > 230, Short.MAX_VALUE)
    > .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
    > .addComponent(cancelButton)
    > .addComponent(uploadButton))
    > .addGap(21, 21, 21))
    > .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
    > .addGroup(javax.swing.GroupLayout.Alignment.TRAILING,
    > jPanel1Layout.createSequentialGroup()
    > .addContainerGap(39, Short.MAX_VALUE)
    > .addComponent(jScrollPane1,
    > javax.swing.GroupLayout.PREFERRED_SIZE, 201,
    > javax.swing.GroupLayout.PREFERRED_SIZE)
    > .addGap(60, 60, 60)))
    > );
    >
    > javax.swing.GroupLayout layout = new
    > javax.swing.GroupLayout(getContentPane());
    > getContentPane().setLayout(layout);
    > layout.setHorizontalGroup(
    >
    > layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
    > .addGap(0, 400, Short.MAX_VALUE)
    > .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
    > .addComponent(jPanel1,
    > javax.swing.GroupLayout.DEFAULT_SIZE,
    > javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
    > );
    > layout.setVerticalGroup(
    >
    > layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
    > .addGap(0, 300, Short.MAX_VALUE)
    > .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
    > .addComponent(jPanel1,
    > javax.swing.GroupLayout.DEFAULT_SIZE,
    > javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
    > );
    >
    > pack();
    > }// </editor-fold>
    >
    > /**
    > * @param args the command line arguments
    > */
    > public static void main(String args[]) {
    > java.awt.EventQueue.invokeLater(new Runnable() {
    > public void run() {
    > DCMUpload dialog = new DCMUpload(new
    > javax.swing.JFrame(), true);
    > dialog.addWindowListener(new
    > java.awt.event.WindowAdapter() {
    > public void
    > windowClosing(java.awt.event.WindowEvent e) {
    > System.exit(0);
    > }
    > });
    > dialog.setVisible(true);
    > }
    > });
    > }
    >
    > // Variables declaration - do not modify
    > private javax.swing.JButton cancelButton;
    > private javax.swing.JLabel jLabel1;
    > private javax.swing.JPanel jPanel1;
    > private javax.swing.JScrollPane jScrollPane1;
    > private javax.swing.JTable tableDCM;
    > private javax.swing.JButton uploadButton;
    > // End of variables declaration


    Well, ick. computer generated code is fit only for reading by computers
    and not by humans.

    >
    > }


    Are you sure you want to program in Java? If so you need to get to grips
    with the basics. Don't try to run before you can walk. I'm sure others
    have suggested the Java tutorials, do read them.

    http://java.sun.com/docs/books/tutorial/getStarted/cupojava/index.html

    Have you sucessfuly written a "hello world" in Java? If not, do so. Then
    write something that creates an Object[][] and uses System.out.print()
    to output the values of that array in a tabular arrangement. Only when
    you have done this should you make the leap to GUI programming.


    --
    RGB
    A gazillion WTFs were deleted in the construction of this post. RIP.
    RedGrittyBrick, Nov 9, 2008
    #18
  19. tobleron

    tobleron Guest

    >
    > Is freebird you tobleron? There is another poster named freebird with
    > history of posting to comp.lang.java
    >


    I used tobleron for my Google forum's nick name. But I used freebird
    for my windows login. So all of my java files generated by Netbeans
    will have "@author freebird". So I think there's another person used
    freebird for his/her Goggle forum's nick name. It's not me.
    tobleron, Nov 10, 2008
    #19
  20. tobleron

    tobleron Guest

    >
    > So the whole of initComponents is generated by netbeans and should not
    > be edited?
    >
    > I have a hard time believing that netbeans created the following code.
    > If the following code is created by tobleron it should not be in
    > initComponents().
    >


    1. I used NetBeans to design the layout of the form, added components
    such as label, panel, scroolpanel, etc.
    2. As I mentioned, NetBeans didn't allow me to edit its generated
    code, so I copied the whole code, opened a new blank java file, pasted
    it, and tried to modify it.

    >
    > What is this trying to accomplish? Please explain the reasoning for
    > having your program construct, at run time, a fragment of source code in
    > a String?
    >


    I used a text file to stored the data. So, I tried to opened the text
    file, extracted the values, and looped until the end. I tried to
    constructed the JTable with this values. That's what I tried to do.

    >
    > Why is this still here? Didn't I and other people point out how wrong
    > this was and suggest syntactically valid working ways to assign useful
    > values to a String [].
    >


    I studied. Give me time to understand and to implement your points.
    Don't be confused with /* .... */ code. That was the original NetBeans
    code that I tried to modify.

    What I want to do is :

    1. Open the text file, extract the values.
    2. Construct the JTable using those values, including rows and columns
    values.
    3. Add select option in each row, so user can select which record will
    be processed.
    4. Add "OK" button to process each selected records.

    I hope you understand what I'm figuring out.
    tobleron, Nov 10, 2008
    #20
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. mr2_93
    Replies:
    1
    Views:
    3,587
    mr2_93
    Oct 2, 2005
  2. Tivo Escobar
    Replies:
    1
    Views:
    7,276
    manusa
    Apr 12, 2007
  3. Tintin92
    Replies:
    1
    Views:
    1,676
    Andrew Thompson
    Feb 14, 2007
  4. Replies:
    0
    Views:
    581
  5. Haircuts Are Important
    Replies:
    3
    Views:
    302
    Haircuts Are Important
    Jun 4, 2013
Loading...

Share This Page