Hi, i have created a jpanel and set the layout to be BoxLayout.Y_AXIS. Here are few problems that i have:
1) firstly, i add JLabel object with some text. i set horizontal text position to be center and set the width of jlabel the same as that of jpanel. However, text is always on the left. Don't know if there's any connection with jtable which i add to the panel below the jlabel.
2) as i have montioned i add jtable to jpanel. i set the width of every column of jtable so that sum of all width's would be equal to width of jpanel. however only some of columns are displayed on jpanel. others can be accesed with scrollbar. However, all columns are displayed on the other jpanel where i dont add any jlabels (add only jtable).
Any tips what to do?
6 replies to this topic
#1
Posted 28 January 2011 - 04:00 AM
|
|
|
#2
Posted 29 January 2011 - 11:11 AM
You can put layouts on top of layouts, think of the movie "Inception."(Dreams within Dreams) Maybe you want to put your BoxLayout on BorderLayout.Center using BorderLayout.North to use for jlabel. Post your code, and Ill help you display what you want correctly.
#3
Posted 29 January 2011 - 11:49 AM
JLabels by default change their width to fit their content.
Try setting the width using:
Try setting the width using:
label.setPreferredSize(new Dimension(width, height));
#4
Posted 31 January 2011 - 03:58 AM
Here's the code:
any ideas mr mike ?
public DayTable(Day day)
{
JPanel panel = new JPanel();
panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS));
panel = Table.createAndAddMealDiaryTableToPanel(day, panel);
JScrollPane scrollPane = new JScrollPane(panel);
TableFrameInitializer.initializeTableFrame(scrollPane, "Dienos mitybos statistika",
SizesOfGuiComponents.mealDiaryFrameWidth);
}
static JPanel createAndAddMealDiaryTableToPanel(Day day, JPanel panel)
{
String[] columnsNames = {"Maisto produktas/patiekalas", "Nr.", "Suvartotas kiekis (gramai)","Kilokalorijos", "Baltymai (gramai)", "Angliavandeniai (gramai)", "Riebalai (gramai)"};
String tableData[][] = getMealDiaryTableData(day);
JTable table = createTable(tableData, columnsNames);
JLabel dateLabel = new JLabel("Data: " + DataFormatter.getDateInString(day.getDate()));
dateLabel.setSize(SizesOfGuiComponents.mealDiaryFrameWidth, SizesOfGuiComponents.tableRowHeight);
dateLabel.setHorizontalTextPosition(JLabel.CENTER);
dateLabel.setBackground(GuiColor.dateLabelColor);
panel.add(dateLabel);
formatTable(table, SizesOfGuiComponents.mealDiaryTableColumnsCount, SizesOfGuiComponents.mealDiaryTableLeftColumnWidth, SizesOfGuiComponents.mealDiaryTableOtherColumnsWidth);
panel.add(table.getTableHeader());
panel.add(table);
JLabel dayStatisticsLabel = new JLabel(DataFormatter.getDayStatisticsInString(day));
dayStatisticsLabel.setSize(SizesOfGuiComponents.mealDiaryFrameWidth,
SizesOfGuiComponents.tableRowHeight);
dayStatisticsLabel.setBackground(GuiColor.dayStatisticsLabelColor);
panel.add(dayStatisticsLabel);
return panel;
}
private static JTable formatTable(JTable table, int columnsCount,
int leftColumnWidth, int otherColumnsWidth)
{
table.setRowHeight(SizesOfGuiComponents.tableRowHeight);
table.setBackground(GuiColor.tableColor);
table.getTableHeader().setBackground(GuiColor.tableHeaderColor);
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);
if (i == 0)
column.setWidth(leftColumnWidth);
else
column.setWidth(otherColumnsWidth);
}
table.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
return table;
}
static void initializeTableFrame(JScrollPane pane, String tableName, int frameWidth)
{
JFrame frame = new JFrame(tableName);
GuiFormatter.alignFrameToCenter(frame);
frame.setSize(frameWidth, SizesOfGuiComponents.tableFrameHeight);
frame.add(pane);
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
frame.setVisible(true);
}
any ideas mr mike ?
Edited by ZekeDragon, 31 January 2011 - 03:50 PM.
Please use [CODE] tags (the # button) when posting code.
#5
Posted 01 February 2011 - 01:43 PM
Sorry for the late response, been really busy. Here goes the basics for what i mean and how I would accomplish this:
You can put all of the jtable information in a method and add it to the center panel. This way will let you organize your code a little better and make it more readable. To display this just create your driver class, or class with your main method using JFrame, same as your initializerMethod().
Hope this puts you in the right direction.
import java.awt.BorderLayout;
import javax.swing.JPanel;
import javax.swing.JTable;
/**
* Program:
*
* Description:
*
* Formula used:
*
* @author Mike S
*/
public class MealChartGui extends JPanel{
private JPanel mainPanel;
private JPanel centerPanel;
private JTable table;
public MealChartGui(){
mainPanel = new JPanel(new BorderLayout());
mainPanel.add(centerPanel, BorderLayout.CENTER);
}
public JPanel centerPanel(){
centerPanel = new JPanel();
table = new JTable();
// put in table data
centerPanel.add(table);
return centerPanel;
}// end of centerpanel
// add more Panel methods to add to your constructor for west?, east?, south?
} // end of class
You can put all of the jtable information in a method and add it to the center panel. This way will let you organize your code a little better and make it more readable. To display this just create your driver class, or class with your main method using JFrame, same as your initializerMethod().
public class FoodChartTest{
public static void main(String [] arrrrgssss){
JFrame frame = new JFrame("Diet Chart");
// set your size
// instanciate your panel class, the above code
// frame.add what you named panel
// frame set close operation
// frame set visible
// frame.pack()
}
}
Hope this puts you in the right direction.
#6
Posted 02 February 2011 - 06:29 AM
Thanks mr mike, you idea help me, the alignment is fine now.
But what about second question? I still can't make the possibility to watch entrire table without using scrollbar to move to left part of the table.
As i mentioned above, when i use jtable without jlabel above, entire table is displayed on screen
Here's my code:
But what about second question? I still can't make the possibility to watch entrire table without using scrollbar to move to left part of the table.
As i mentioned above, when i use jtable without jlabel above, entire table is displayed on screen
Here's my code:
//class with sizes needed by table
public class SizesOfGuiComponents
{
...
public static final int tableRowHeight = 35;
public static final int mealDiaryTableColumnsCount = 7;
public static final int mealDiaryFrameWidth = 850;
public static final int mealDiaryTableLeftColumnWidth = 130;
public static final int mealDiaryTableOtherColumnsWidth = 120;
...
}
static JPanel createAndAddMealDiaryTableToPanel(Day day, JPanel mainPanel)
{
....
formatTable(table, SizesOfGuiComponents.mealDiaryTableColumnsCount, SizesOfGuiComponents.
mealDiaryTableLeftColumnWidth, SizesOfGuiComponents.mealDiaryTableOtherColumnsWidth);
tablePanel.add(table.getTableHeader());
tablePanel.add(table);
....
}
//method that sets appropriate sizes for table
private static JTable formatTable(JTable table, int columnsCount,
int leftColumnWidth, int otherColumnsWidth)
{
table.setRowHeight(SizesOfGuiComponents.tableRowHeight);
table.setBackground(GuiColor.tableColor);
table.getTableHeader().setBackground(GuiColor.tableHeaderColor);
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);
if (i == 0)
column.setWidth(leftColumnWidth);
else
column.setWidth(otherColumnsWidth);
}
table.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
return table;
}
#7
Posted 03 February 2011 - 05:30 AM
No need for help from now, solved the problem myself
1 user(s) are reading this topic
0 members, 1 guests, 0 anonymous users


Sign In
Create Account


Back to top









