Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie
Hi there,
There is an issue with role permissions that is being worked on at the moment.
If you are having trouble with access or permissions on regional forums please post here to get access: https://www.boards.ie/discussion/2058365403/you-do-not-have-permission-for-that#latest

Java: list boxs and columns

  • 06-03-2004 8:13pm
    #1
    Registered Users, Registered Users 2 Posts: 383 ✭✭


    Evening all,

    a question, I am using a Swing JList() component, and I am just wondering, is it possible to add columns?

    Basicailly I have serveral bits of info to display about a series of results, I want to make them appear nicely, and been able to add columns would be great. I've tried googling and checking the Sun tutorials, but couldn't fin anything.

    thx for the help.


Comments

  • Closed Accounts Posts: 423 ✭✭Dizz


    Why not a JTable?

    Dizz


  • Registered Users, Registered Users 2 Posts: 383 ✭✭cherrio


    @Dizz,

    yes I gave up on JList and went to with JTable. But now I am having problems with that!!

    How do you update the content (well number of 'rows' in the table? Basicallt the app stats it should see the table (empty), the only way I can get this is by having 1 row of empty data.

    I then run into problems trying to add more that 1 row. I was able to get around it by using table.setModel(new updateTableModel())

    Where updateTableModel is another class that I created that "extends AbstractTableModel". This works fine, it can insert the rows and things, but it resets the column names from "ID, name, address..." to "A, B, C, D...".

    I have no idea why or how to fix it! Any help would be really appreciated.

    thx.


  • Closed Accounts Posts: 423 ✭✭Dizz


    Haven't used JTables in ages... I've some code using it in a JApplet if you want it...

    Dizz


  • Registered Users, Registered Users 2 Posts: 383 ✭✭cherrio


    thx Dizz,

    I eventually managed to find a solution. In case any one needs it, I've put it below,

    
    String columnNames = new String[] {"ID", "Name ", "Address 1   ", "Address 2   ", "Country"};
    
    for (int i = 0; i < columnNames.length; i++) {
          TableColumn column = table.getColumnModel().getColumn(table.convertColumnIndexToView(i));
          column.setHeaderValue(columnNames[i]);
    }
    table.getTableHeader().repaint();
    
    


  • Closed Accounts Posts: 1,152 ✭✭✭sound_wave


    sorry for jumping on your thread!

    Anyone know how i can while using a JTable disable the cells so the user cant edit the values stored in them?

    thanks


  • Advertisement
  • Closed Accounts Posts: 302 ✭✭Auburn


    sound_wave: you need to override the table model's default
    public boolean isCellEditable
    
    method


  • Registered Users, Registered Users 2 Posts: 383 ✭✭cherrio


    @sound_wave,

    Yes, it was one of the problems I was having as well. What you have to do is create a new class that extends AbstractTableModel. This class must then define the method (amoungest others), public boolean isCellEditable(int row, int col). All you have to do is "return false;" to this method.

    // updateTable.java
    import javax.swing.table.AbstractTableModel;
    import javax.swing.table.DefaultTableCellRenderer;
    import javax.swing.table.TableCellRenderer;
    import javax.swing.table.TableColumn;
    import javax.swing.*;
    
    
    /**
     * <p>Title: </p>
     * <p>Description: </p>
     * <p>Copyright: Copyright (c) 2004</p>
     * <p>Company: </p>
     * @author not attributable
     * @version 1.0
     */
    
    class updateTable extends AbstractTableModel {
    
      private Object[] columnNames;
      private Object[][] data;
      Debug error = new Debug();
    
      public updateTable(Object[][] dataIn, Object[] colsIn){
        columnNames = colsIn;
        data = dataIn;
      }
    
    
            public int getColumnCount() {
                return columnNames.length;
            }
    
            public int getRowCount() {
                return data.length;
            }
    
    
            public Object getValueAt(int row, int col) {
                return data[row][col];
            }
    
            /*
             * JTable uses this method to determine the default renderer/
             * editor for each cell.  If we didn't implement this method,
             * then the last column would contain text ("true"/"false"),
             * rather than a check box.
             */
            public Class getColumnClass(int c) {
                return getValueAt(0, c).getClass();
            }
    
            /*
             * Don't need to implement this method unless your table's
             * editable.
             */
            public boolean isCellEditable(int row, int col) {
                //Note that the data/cell address is constant,
                //no matter where the cell appears onscreen.
                //if (col < 2) {
                    return false; // we don't want any fields editable, but you could have some if you wanted.
                //} else {
                //    return true;
                //}
            }
    
            /*
             * Don't need to implement this method unless your table's
             * data can change.
             */
            public void setValueAt(Object value, int row, int col) {
              error.add("row: " + row + " - Col: " + col + " - Value: " + value);
              data[row][col] = value;
              fireTableCellUpdated(row, col);
    
            }
    
        }
    

    then you actual calling class would have something like,
    String columnNames = new String[] {"ID", "Name ", "Address 1   ", "Address 2   ", "Country"};
    
    Object[][] data = {
                {"", "", "", "", ""} // empty
    };
    
    
    
    JTable table = new JTable(data, columnNames);
    table.setSelectionMode(ListSelectionModel.SINGLE_INTERVAL_SELECTION); // can only select 1 row at a time
    table.setModel(new updateTable(data, columnNames));
    

    I extrapalted all my code from examples from this very handy page: http://www.chka.de/swing/table/faq.html


  • Closed Accounts Posts: 1,152 ✭✭✭sound_wave


    ok thanks you guys!


Advertisement