Results 1 to 5 of 5
- 01-30-2010, 03:37 PM #1
Member
- Join Date
- Dec 2009
- Posts
- 68
- Rep Power
- 0
the values in the row of a JTable doesnt look right
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...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(); } }); } }
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!
-
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.
- 01-30-2010, 03:56 PM #3
Member
- Join Date
- Dec 2009
- Posts
- 68
- Rep Power
- 0
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 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?so consider changing this at your own riskLast edited by bigj; 01-30-2010 at 04:12 PM.
-
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. :)
- 01-30-2010, 04:49 PM #5
Member
- Join Date
- Dec 2009
- Posts
- 68
- Rep Power
- 0
Similar Threads
-
values in jtable
By blackpearlmoni in forum AWT / SwingReplies: 4Last Post: 11-04-2009, 07:48 PM -
[SOLVED] Reading values from a jtable
By Manfizy in forum New To JavaReplies: 10Last Post: 05-20-2009, 12:58 PM -
JTable doesnt show columun names!
By phil128 in forum AWT / SwingReplies: 3Last Post: 03-08-2009, 10:39 PM -
Problem in printing JTable values
By shanssat in forum AWT / SwingReplies: 3Last Post: 02-04-2009, 08:15 AM -
Getting integer values from a JTable
By zikojam in forum New To JavaReplies: 1Last Post: 12-11-2008, 02:06 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks