public class TradesHistoryTable
{
public TradesHistoryTable(LinkedList<Trade> tradesList)
{
JScrollPane pane = Table.createTradesHistoryTable(tradesList);
FrameInitializer.initializeTableFrame(pane, "History of trades");
}
}
public class FrameInitializer
{
public static void initializeTableFrame(JScrollPane scrollPane, String tableName)
{
JFrame frame = new JFrame(tableName);
Container pane = frame.getContentPane();
pane.add(scrollPane);
alignFrameToCenter(frame);
frame.setSize(Sizes.TABLE_WIDTH, Sizes.TABLE_HEIGHT);
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
frame.setVisible(true);
}
...
}
public class Table
{
static JScrollPane createTradesHistoryTable(LinkedList<Trade> tradesList)
{
String[] columnsNames = initializeTradesHistoryTableColumnsNames();
String[][] tableData = getTradesHistoryTableData(tradesList);
JTable tradesHistoryTable = createTable(tableData, columnsNames);
tradesHistoryTable = formatTable(tradesHistoryTable, Sizes.TRADES_HISTORY_TABLE_COLUMNS_COUNT,
Sizes.TABLE_COLUMN_WIDTH);
tradesHistoryTable.getTableHeader().setBackground(Colors.TABLE_HEADER_COLOR);
tradesHistoryTable.getTableHeader().setForeground(Colors.LETTER_COLOR);
tradesHistoryTable.setBackground(Colors.TABLE_COLOR);
tradesHistoryTable.setForeground(Colors.LETTER_COLOR);
JScrollPane scrollPane = new JScrollPane();
scrollPane.add(tradesHistoryTable.getTableHeader());
scrollPane.add(tradesHistoryTable);
return scrollPane;
}
private static String[] initializeTradesHistoryTableColumnsNames()
{
String[] columnsNames = {"Date", "Id", "Type", "Amount of money", "Description"};
return columnsNames;
}
private static String[][] getTradesHistoryTableData(LinkedList<Trade> tradesList)
{
String[][] tableData = new String[tradesList.size()][Sizes.TRADES_HISTORY_TABLE_COLUMNS_COUNT];
int i = 0;
for (Trade trade: tradesList)
{
tableData[i] = getTradesHistoryTableLine(trade);
i++;
}
return tableData;
}
private static String[] getTradesHistoryTableLine(Trade trade)
{
String tradeType;
if (trade.tradeIsIncome())
tradeType = "income";
else
tradeType = "expenditure";
String tradeDescription;
if (trade.getTradeDescription() == "No description for this trade")
tradeDescription = "";
else
tradeDescription = trade.getTradeDescription();
String[] tableLine = {DataFormatter.getDateInString(trade.getTradeDate()),
String.valueOf(trade.getTradeId()), tradeType, String.valueOf
(trade.getCash()), tradeDescription};
return tableLine;
}
private static JTable createTable(String[][] tableData, String[] columnsNames)
{
DefaultTableModel tableModel = new DefaultTableModel(tableData, columnsNames);
JTable table = new JTable(tableModel)
{
public boolean isCellEditable(int rowIndex, int colIndex)
{
return false;
}
};
table.setShowGrid(false);
return table;
}
private static JTable formatTable(JTable table, int columnsCount, int columnWidth)
{
table.setRowHeight(Sizes.ROW_HEIGHT);
table.setShowGrid(false);
DefaultTableCellRenderer alignmentRenderer = new DefaultTableCellRenderer();
alignmentRenderer.setHorizontalAlignment(SwingConstants.CENTER);
for (int i = 0; i < columnsCount; i++)
{
TableColumn column = table.getColumnModel().getColumn(i);
column.setCellRenderer(alignmentRenderer);
column.setWidth(columnWidth);
}
table.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
return table;
}
}
Any tips what to fix?
3 replies to this topic
#1
Posted 02 February 2011 - 06:49 AM
Hi, i create jtable and add it to jscrollpane. Then i add jscrollpane to jframe. Unfortunatelly, when i run program, only empty jframe is visible. It must be silly mistake. Here's my code:
|
|
|
#2
Posted 02 February 2011 - 09:53 AM
Can you set a BoxLayout for the JFrame and see if that helps? Just throw it into the JFrame initialization code, I can't build your source since it's incomplete.
Wow I changed my sig!
#3
Posted 02 February 2011 - 10:00 AM
The add method of the scrollPane doesn't really add it to the scrollPane :P
For the JScrollPane the add method is actually pretty useless, it exists because the add method exists in the Container class which the JScrollPane extends.
The object that will get scrollbars, and is added to the frame, is added with the setViewportView method (or just pass the object to the JScrollPane's constructor)
Because you can only add 1 object to each scrollPane you should add your 2 things to a JPanel, and use this JPanel in the JScrollPane's constructor(or setviewportview method).
For the JScrollPane the add method is actually pretty useless, it exists because the add method exists in the Container class which the JScrollPane extends.
The object that will get scrollbars, and is added to the frame, is added with the setViewportView method (or just pass the object to the JScrollPane's constructor)
Because you can only add 1 object to each scrollPane you should add your 2 things to a JPanel, and use this JPanel in the JScrollPane's constructor(or setviewportview method).
#4
Posted 03 February 2011 - 04:36 AM
thanks wim DC, it helped me
1 user(s) are reading this topic
0 members, 1 guests, 0 anonymous users


Sign In
Create Account


Back to top









