Results 1 to 5 of 5
  1. #1
    bigj is offline Member
    Join Date
    Dec 2009
    Posts
    68
    Rep Power
    0

    Default the values in the row of a JTable doesnt look right

    Java Code:
    package xxTestxx;
    
    import java.awt.*;
    import javax.swing.*;
    import javax.swing.event.ListSelectionEvent;
    import javax.swing.event.ListSelectionListener;
    import javax.swing.table.DefaultTableModel;
    import javax.swing.table.TableCellRenderer;
    import javax.swing.table.TableModel;
    import javax.swing.table.TableRowSorter;
    
    @SuppressWarnings("serial")
    public class Inventory extends JFrame {
    
      private JTable inventoryTable;
      private JPanel inventoryPanel;
      private JScrollPane scroll;
      private TableModel model;
      
      public Inventory() {
          
        initializeInventory();
      }
    
      private void initializeInventory() {
    
        inventoryPanel = new JPanel();
        inventoryPanel.setLayout(null);
        
        final String[] columnNames = {"Item Code", "Item Quantity", "Item Desciption", "Item Cost",
                                          "Cost Extension", "Retail", "Retail Extension", "Expiration Code"};
    
            final Object[][] data = {{"1000008", new Integer(32), "TOBI MIX NUTS 45g", new Double(18.52), new Double(100.50),
                               new Double(21.75), new Double(150.50), new Long(100142)},
                               {"1000013", new Integer(45), "TOBI MELON SEEDs 100g", new Double(30.00), new Double(100.50),
                               new Double(21.75), new Double(150.50), new Long(100142)}};
    
    
            model = new DefaultTableModel(data, columnNames) {
    
                public Class getColumnClass(int column) {
    
                    if (column >= 0 && column <= getColumnCount()) {
    
                        return getValueAt(0, column).getClass();
                    }
                    else {
    
                        return Object.class;
                    }
                }
            };
            RowSorter<TableModel> sorter = new TableRowSorter<TableModel>(model);
            
            inventoryTable = new JTable(model);
            scroll = new JScrollPane(inventoryTable);
            scroll.setBounds(0, 50, 1395, 444);
            
            inventoryPanel.add(scroll);
    
            getContentPane().add(inventoryPanel);
    
            setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
            setTitle("Inventory Window");
            setSize(1400, 200);
            setLocationRelativeTo(null);
            setVisible(true);
      }
    
      public static void main(String[] args) {
    
          SwingUtilities.invokeLater(new Runnable() {
    
              public void run() {
    
                  new Inventory();
              }
          });
      }
    }
    as you can see in the table , the values in the first column rests in the left most part of the cell, while the values in the second columns rests at the right side... and so on...

    how can i make ALL values RESTS in either right side or left side?
    when i tried to include a TableModel that sorts by getting the class type of column i encountered this, Is there anyway to avoid this?

    I hope you understand my concern, thanks in advance!

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default

    What you are observing are the default properties of the JTable DefaultCellEditor. It will place Strings left-justified and numbers right-justified. If you use spreadsheets such as Excel, you'll see that this is their default behavior too -- so consider changing this at your own risk. If you want to change it though, one option is to create a custom cell editor for your table. The Sun tutorials can guide you in this.

  3. #3
    bigj is offline Member
    Join Date
    Dec 2009
    Posts
    68
    Rep Power
    0

    Default

    oh my... darn.. what a very noob I am.. im not familiar with excel spreadsheet(but somehow I am), its just that I dont recall all the constant behaviour of it,

    so consider changing this at your own risk
    so sir, before I try to make my own custom cell editor, and decide to change the default behaviour of a spreadsheet, what would be better to avoid inefficiency?
    Last edited by bigj; 01-30-2010 at 04:12 PM.

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default

    It's not an inefficiency, it's just a look that some users may not be used to, is all. Perhaps "at your own risk" is a bit too strong of a statement. Go ahead, change your cell editor and see if you like it. :)

  5. #5
    bigj is offline Member
    Join Date
    Dec 2009
    Posts
    68
    Rep Power
    0

Similar Threads

  1. values in jtable
    By blackpearlmoni in forum AWT / Swing
    Replies: 4
    Last Post: 11-04-2009, 07:48 PM
  2. [SOLVED] Reading values from a jtable
    By Manfizy in forum New To Java
    Replies: 10
    Last Post: 05-20-2009, 12:58 PM
  3. JTable doesnt show columun names!
    By phil128 in forum AWT / Swing
    Replies: 3
    Last Post: 03-08-2009, 10:39 PM
  4. Problem in printing JTable values
    By shanssat in forum AWT / Swing
    Replies: 3
    Last Post: 02-04-2009, 08:15 AM
  5. Getting integer values from a JTable
    By zikojam in forum New To Java
    Replies: 1
    Last Post: 12-11-2008, 02:06 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
  •