JTable for a Layman :(

Hello Everybody
im doing my final project...I wanted to take a list of input from user against some predefined words e.g user will have to enter a value for each of the following letters: ABCDEGFHKLRN.......... The only way i found to do that was thru table, which i have seeb first time.My idea was to show the user the predefined letters "ABCDEG..etc" in 1st column of the table and take the corresponding values from the user from the 2nd colum of table.
I had absolutely no idea of how to use a table, i have tried working out by following n reading the tutorial: [[http://java.sun.com/docs/books/tutorial/uiswing/components/table.html#data]
but i am very confused,i am unable to understand what i have to do for my purpose because the tutorial has so many things related to each other.Please help me as soon as possible
Im using netbeans.... i tried by using tablemodellistener but i was unable to get the value entered in the table,also listener reads the data at runtime, i dont need to read at runtime, i want to process the data after the whole data (the list of values) have been added.... please help i have tried alot

You're probably using a JTable with a DefaultTableModel (which declares all columns as editable), and the JTable uses a default editor that enables to type Strings. So no, you don't need a specific editor. That approach has drawback though (I didn't get the goal of the app, but it seems undesirable that the user be able to edit the first column), but work on sketching something that works first, and then robustify it afterwards.
You didn't comment reply #3. Did you get it?
A JTable has un underlying TableModel, a non-graphical object, which enables to access the "logical content" of each cell: this is the point of the method getValueAt(int row, int column).
( EDIT I just discover it: there's a getValueAt(row,col) on JTable itself. There are subtleties with it (reordering of columns), but as a first shot you can use it (immediately after though, you really should invest in understanding the TableModel, as I no serious work can be undertaken on JTable without mastering that). )
Wrapping up Darryl's and jboeing's posts, you should have thought about:
- setting up a JButton beside the JTable.
- an ActionListener on the button, that would iterate over all cells in the "second column", and get the value (probaly casting it to String), and do whatever you want with the values.
Edited by: jduprez on Sep 25, 2009 8:17 PM

Similar Messages

  • Multiple selection in JTable for deletion

    Dears,
    I'm using Jdeveloper 9.0.3.3 on 8.1.7 Oracle DB.
    I noticed that when a JTable is bound to a View object ==> only one row can be selected.
    My situation -which I think is very common- I have a screen with JTable where I can edit a row and commit my updates or select multiple selection in JTable and delete them at once ; thats what I need.
    It is very like the test of any application module but allow multiple selection and deletion in JTable for user's selected rows.
    How this could be done in JClient?
    Thankx in advance.

    Thanks for your suggestions! They both would work as workarounds, I think. I tried another way:
    Following code changes a TableBinding bound JTable's selection model to Multi select (thanks to Shailesh!!).
    void setMultiSelectionModel(JTable tbl)
    class MultiSelectionListListener implements javax.swing.event.ListSelectionListener
    ListSelectionModel defSelModel;
    MultiSelectionListListener(ListSelectionModel model)
    defSelModel = model;
    public void valueChanged(javax.swing.event.ListSelectionEvent e)
    if (!e.getValueIsAdjusting())
    ListSelectionModel listModel = (ListSelectionModel)e.getSource();
    int leadIndex = listModel.getLeadSelectionIndex();
    if (leadIndex == listModel.getAnchorSelectionIndex()
    && leadIndex == listModel.getMaxSelectionIndex()
    && leadIndex == listModel.getMinSelectionIndex())
    //change currency on the bound iterator only if
    //one row is being selected.
    defSelModel.setSelectionInterval(leadIndex, leadIndex);
    ListSelectionModel newModel = new DefaultListSelectionModel();
    newModel.setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);
    newModel.addListSelectionListener(new MultiSelectionListListener(tbl.getSelectionModel()));
    tbl.setSelectionModel(newModel);
    This fixes the bug if you select some rows and then select a row before the first selected one. Seems to work great.

  • How to select a row in JTable for right mouse click ?

    Hi All,
    I need to select a row on JTable for right mouse click ?
    Can any one help me ?
    Thanks a lot.
    Seelam.

    Got solution...
    tabel.addRowSelectionInterval(..) works.
    thanks.

  • What's the best way to print content of javax Object (JTable for example)?

    Hi there,
    1) I would like to know the best way to print content of jtable (for example)
    2) I would like to know how can I Print content of SQL statment (for example "SELECT ID, NAME FROM COMPANY")?
    Best Regard,
    Eyal
    [email protected]

    Depends on what you want printed. You can do something like System.out.println(myTable);but you may not like what you see. If you want to print anything else, extend JTable and create a new toString() method that prints out what you want.
    As for printing a SQL statement - assuming it is in a String, you can just do System.out.println(mySQLStatement);

  • Different JCombobox items in JTable for the same column

    Hello Everyone,
    I am reading from a file and displaying in a JTable. I want all cells in a specific column to be JComboBoxes but the items in the JComboBox differ from one cell to another, how can this be achieved?
    The following is a snippet from the Sun tutorials,
    JComboBox comboBox = new JComboBox();
    comboBox.addItem("Snowboarding");
    comboBox.addItem("Rowing");
    comboBox.addItem("Knitting");
    comboBox.addItem("Speed reading");
    comboBox.addItem("Pool");
    comboBox.addItem("None of the above");
    sportColumn.setCellEditor(new DefaultCellEditor(comboBox));
    And hence every cell in the sportColumn has the exact same set of options in the JComboBox which is not what I want since I read from an external file.
    Any help or sample code would be highly appreciated.
    Thank you in advance

    Override getCellEditor(int row, int column) in JTable to control which editor gets used for which cell.

  • Handling keyTyped events in JTable for TAB key

    In my app, I have a JTable. Some columns are non-editable.
    I have attached a keyListener to the table and have overridden keyTyped() and keyReleased() methods.
    In keyReleased(), I do something depending on the key code. For example: if its the VK_DELETE, I delete the row. If its some other user configured key, then I show a popup dialog where the user can enter some data, etc...
    In keyTyped(), I first check if the column is one of the specific columns. Then I get the keyChar. After this, I show a dialog and pre-populate a JTextField on this dialog with that keyChar.
    My issue is that 'TAB' key events arrive in keyTyped() and not in keyReleased(). As a result, the dialog is shown and the tab takes place inside the JTextfield which is incorrect.
    I would like to ignore TAB keyTyped events. When I look up the keyCode in keyTyped() method, it is 0. So there is no way for me to tell what key was typed.
    How can I ignore TAB events in my keyTyped() method?
    thx

    I now do the following to determine if TAB key has been typed
    public void keyTyped(KeyEvent e) {
    char c = e.getKeyChar();
    if (c == KeyEvent.VK_TAB) {
    Is this correct ?
    Edited by: tsc on Sep 28, 2007 12:40 PM

  • Is it possible to buffer the entire JTable for JScrollPane scrolling?

    With the following code, when you scroll up and down you get lots of "called" output.
    I will know when my jtable is updated, so is there
    some way I can get the scrollpane to buffer the entire jtable component
    so that subsequent scrolling just does drawing of the appropriate part
    of the jtable from a buffered image? Note that the cells still need to
    be editable (I've got cell editors). Possible? Here's the code :
    public class TableBufTest {
        public static void main(String[] args) {
            String[][] data = new String[30][2];
            for (int i=0; i<2; i++) {
                for (int j=0; j<30; j++) {
                    data[j] = Integer.toString(j*10 + i);
    String[] columnNames = new String[] {"1", "2"};
    final JLabel jLabel = new JLabel();
    JTable table = new JTable(data, columnNames);
    table.getColumnModel().getColumn(0).setCellRenderer(new TableCellRenderer() {
    public Component getTableCellRendererComponent(JTable table, Object value,
    boolean isSelected, boolean hasFocus,
    int row, int column) {
    System.out.println("called");
    jLabel.setText((String) value);
    return jLabel;
    JScrollPane scrollPane = new JScrollPane(table);
    JFrame jFrame = new JFrame();
    jFrame.getContentPane().add(scrollPane);
    jFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    jFrame.setSize(200,200);
    jFrame.setLocation(300,300);
    jFrame.show();
    Thanks!

    Thanks.
    I ended up doing a couple of things - first of all using JLabels instead of JTextFields
    and then creating my own FastJLabel using the DefaultTableCellRenderer as a basis.
    (I couldn't use it because I have to return different components for different rows.)

  • Multiple JTables for One TableModel

    i have 3 tabs, and 5 tables, they arent that big mind you...the biggest table is about 150rows, 10cols, and the rest are about 10-20rows, 15cols
    they update every 5 secs, so i create a new datavector and set the tables model to the new data vector, every time
    this probably isnt the best way to do it :P
    the thing is, the last 4 table's data depends on the first (big) table's data, what is the best way to have multiple jtables displaying different data from the same data source, this way i just have to create and update the main datavector once while the other 4 just change their views
    take advantage of MVC alittle better...
    thanks

    Here is a simple [url http://forum.java.sun.com/thread.jsp?forum=31&thread=411506]example of a TableModel being shared by two tables.
    After you update the main data vector you would need to fireTableDataChanged(...); This should cause all views to be updated.

  • JTable for Invoice

    hi there.... sorry for my poor english.... i'm implementing a software that manage the invoices and the archieve of user.... i'm blocked in this point: i have finished the interface now i must build the final screen (a summary of invoice) where i must put more information: logo(image), all articles that i saved in a vector, and other information...
    how i can build a grid like this image? or there are any suggestion?
    help me pls.... i'm despaired!
    very thanks....
    http://img231.imageshack.us/img231/1995/girdbt0.jpg <------------ the grid!!!

    {color:#0000ff}http://catb.org/~esr/faqs/smart-questions.html{color}
    db

  • How to keep look of JTable for several models

    Hi,
    a (probably easy to answer question). I have a JTable, change the size of columns or remove columns. How can i keep the look of the JTable, when i assign a new model (same type) to the table?
    Thanks!
    Andrej

    Hi,
    provide an own TableColumnModel - otherwise the TableColumnModel will be autocreated using the data of the model - i.e. column names, column count - and setAutoCreateColumnsFromModel(false);
    greetings Marsian

  • Inserting JTable for JDBC swing project in programme.

    i,ve a problem to develop an application contains table area
    with fixed columns but multiple rows in it.give me a suitable suggestion plz.

    http://forum.java.sun.com/thread.jsp?forum=57&thread=271752
    Take a look at this thread and please read carefully.
    James

  • Multiple keystrokes selection for a JComboBox in JTable

    Has anyone used multiple keystrokes selection in a JComboBox inside JTable before? I can get it done outside JTable by using: http://javaalmanac.com/egs/javax.swing/combobox_CbMultiKey.html
    Looks like JTable has all kinds of problems to support JComboBox.
    Suggestions?
    Thanks,
    James

    If I read you right, you want to use a multiple keystroke combo box as an editor in a JTable?
    If you create the JComboBox as you would like it and then install it as an editor in the column(s) JTable the editor will work like the JComboBox
    Example:
    //- you would have that keyselection Manager class
    // This key selection manager will handle selections based on multiple keys.
    class MyKeySelectionManager implements JComboBox.KeySelectionManager {    ....    };
    //- Create the JComboBox with the multiple keystroke ability
    //- Create a read-only combobox
    String[] items = {"Ant", "Ape", "Bat", "Boa", "Cat", "Cow"};
    JComboBox cboBox = new JComboBox(items);
    // Install the custom key selection manager
    cboBox.setKeySelectionManager(new MyKeySelectionManager());
    //- combo box editor for the JTable
    DefaultCellEditor cboBoxCellEditor = new DefaultCellEditor(cboBox);
    //- set the editor to the specified COlumn in the JTable - for example the first column (0)
    tcm.getColumn(0).setCellEditor(cboBoxCellEditor); Finally, it may be necessary to to put a KeyPressed listener for the Tab key, and if you enter the column that has the JComboBox:
    1) start the editting
    table.editCellAt(row, col);2) get the editor component and cast it into a JComboBox (in this case)
    Component comp = table.getEditorComponent();
    JComboBox cboComp = (JComboBox) comp;3) give this compent the foucus to do its deed     
    cboComp.requestFocus();Hope this helps!
    dd

  • Listener for JTable

    hi all
    i'm using JTable for displaying data retrieved from database. I need to count the no of rows dynamically. ie whenever a row is added/deleted to JTable the rowcounter should be in/decremented. is there any event to do this

    You need to add a table model listener to the JTable. Within the table model listener you can get an event when a row is inserted, deleted or updated,
    Cheers
    Marco
    User Interface Blog
    http://www.user-interface.org

  • How to set Right Alignment for a JTable column

    Hi,
    I am using a JTable for displaying database records using AbstractTableModel. It works fine. But, for the numeric fields I want to make records right aligned using setHorizontalAlignment. I tested with TableColumn.setHorizontalAlignment but it ends up with an error. Is there a solution? Please help me. Thanks in advance.
    bhuvana.

    In you table cell renderer you can specify the alignment of the text.
    public class MyTableCellRenderer extends JLabel implements TableCellRenderer
    public MyTableCellRenderer
    setOpaque(true);
    setBackground(Color.white);
    public Component getTable.....(JTable table,Object value,......) //fill all these things
    setFont(table.getFont());
    if(value instanceof Float)
    setHorizontalAlignment(SwingConstants.RIGHT);
    setText(value);
    return this;
    This would set all the Floats to RIGHT alignment.
    Thanks,
    Kalyan

  • Event Handler for a JComboBox in JTable

    Hi,
    I am using a JTable with one column having JComboBox as CellEditor, I Want to handle event for jcomboBox such that if I am selecting any item from JcomboBox item , i check if there is any field before that in the Jtable Cell if yes then I am setting it to null or the previous value. I have implemented CellEditorListener to it but it is not working fine, so if anyone can please help me out..

    If I read you right, you want to use a multiple keystroke combo box as an editor in a JTable?
    If you create the JComboBox as you would like it and then install it as an editor in the column(s) JTable the editor will work like the JComboBox
    Example:
    //- you would have that keyselection Manager class
    // This key selection manager will handle selections based on multiple keys.
    class MyKeySelectionManager implements JComboBox.KeySelectionManager {    ....    };
    //- Create the JComboBox with the multiple keystroke ability
    //- Create a read-only combobox
    String[] items = {"Ant", "Ape", "Bat", "Boa", "Cat", "Cow"};
    JComboBox cboBox = new JComboBox(items);
    // Install the custom key selection manager
    cboBox.setKeySelectionManager(new MyKeySelectionManager());
    //- combo box editor for the JTable
    DefaultCellEditor cboBoxCellEditor = new DefaultCellEditor(cboBox);
    //- set the editor to the specified COlumn in the JTable - for example the first column (0)
    tcm.getColumn(0).setCellEditor(cboBoxCellEditor); Finally, it may be necessary to to put a KeyPressed listener for the Tab key, and if you enter the column that has the JComboBox:
    1) start the editting
    table.editCellAt(row, col);2) get the editor component and cast it into a JComboBox (in this case)
    Component comp = table.getEditorComponent();
    JComboBox cboComp = (JComboBox) comp;3) give this compent the foucus to do its deed     
    cboComp.requestFocus();Hope this helps!
    dd

Maybe you are looking for

  • Help! Help! benefit enrollment conversion API doesn't work well!!!

    We just acquired a big company. Now we are converting new employees benefit information from their system to our OAB. As per Note:365034.1 suggested, I have been trying to use API 'ben_election_information.election_information_w' to create new enroll

  • Booting from eSATA2 RAID Cards??

    is this possible yet? if so, which card(s) support it? Quad G5 Mac OS X (10.4.4) thanks

  • Satellite A55 How to remove Bottom part of the notebook?

    I need to remove the bottom of my A55. I've removed all the visible screws but something is hanging up in the battery area. Can someone please help me? Thanks so much.

  • How to make JComboBox display an item not in list?

    hi, i'd like a gui component which is basically a jcombobox that presents a list A,B,C,D. The combobox is paired with another gui component which allows choosing sets of items. A etc.. represent preselected sets of items, so the user can click on A a

  • Affichages multiples et temps réel de signaux

    Je dispose d'un vi d'acquisition d'un signal (+s voies et 2 cartes PCI-4472 et PCI-4474) qui me génère un tableau de waveforms en sortie (DAQmx). Je veux écrire différents vis d'affichage (f(t), FFT, XY,...) de ce signal et qui devront être en temps