the values in the row of a JTable doesnt look right
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!