Jump to content

problem with BorderLayout

- - - - -

  • Please log in to reply
7 replies to this topic

#1
thatsme

thatsme

    Programmer

  • Members
  • PipPipPipPip
  • 176 posts
hi, i have a mainPanel (JPanel) with BorderLayout. I put subPanel (JPanel) containing JLabel to BorderLayot.NORTH. Then i put another subPanel (JPanel) containing JTable to BorderLayot.CENTER. Finally i put third subPanel containing JLabel to BorderLayot.SOUTH. In this third subPanel label's text always appears to be very far from BorderLayout's CENTER area, so there happens to be a silly gap between JTable and JLabel below. I used setSize method for label and i noticed that when i increase size of lebel, text of label goes closer to JTable, but not enough. Any tips?

#2
lethalwire

lethalwire

    while(false){ ... }

  • Members
  • PipPipPipPipPipPipPip
  • 748 posts
  • Programming Language:Java, PHP
  • Learning:Java, PHP
it's almost hard to say right now. Did you try using setPreferredSize ( new Dimension( width, height ) ) instead of setSize?

#3
thatsme

thatsme

    Programmer

  • Members
  • PipPipPipPip
  • 176 posts
yes, i tried it

#4
mr mike

mr mike

    Learning Programmer

  • Members
  • PipPipPip
  • 96 posts
I see your placing the panels on panels, I like it. Does that make your code more readable?
Possible solutions: Try on that borderLayout center to place a borderLayout panel with JTable in the south and label in the north or just do a box layout on center panel using Component.CENTER_ALIGNMENT for your label and table.

#5
thatsme

thatsme

    Programmer

  • Members
  • PipPipPipPip
  • 176 posts
1) when i place jtable and jlabel in the same panel, jlabel loses center alignment (it is always diplayed on the left side of frame)
2) attemp to work with Component.CENTER_ALIGNMENT failed too. maybe i have done something wrong

What i am trying to do is to create a panel with many jtables each having two jlabels (one above and other below it).
Here's my code:

    //main panel has BoxLayout.Y_AXIS
    static JPanel createMealDiaryTablesAndAddToPanel(LinkedList<Day> daysList, JPanel mainPanel)
    {       
        for (int i = 0; i < daysList.size(); i++)
        {
            createAndAddMealDiaryTableToPanel(daysList.get(i), mainPanel);
            mainPanel.add(Box.createVerticalStrut(Sizes.ROW_HEIGHT));
        }
        
        PeriodStatistics statistics = PeriodStatisticsCalculator.calculateNutritionStatistics(daysList);
        JLabel periodStatisticsLabel = createJLabel(DataFormatter.
                getPeriodStatisticsInString(statistics), Colors.PERIOD_STATISTICS_LABEL_COLOR);
        JPanel labelPanel = new JPanel();
        labelPanel.add(periodStatisticsLabel);
        mainPanel.add(labelPanel);
        
        return mainPanel;
    }

static JPanel createAndAddMealDiaryTableToPanel(Day day, JPanel mainPanel)
    {        
        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);
        
        JPanel labelsAndTablePanel = new JPanel(new BorderLayout());
        
        JPanel tablePanel = new JPanel();
        tablePanel.setLayout(new BoxLayout(tablePanel, BoxLayout.Y_AXIS));
        labelsAndTablePanel.add(tablePanel, BorderLayout.CENTER);
 
        JLabel dateLabel = createJLabel(DataFormatter.getDateInString
                (day.getDate()), Colors.DATE_LABEL_COLOR);
        labelsAndTablePanel.add(dateLabel, BorderLayout.NORTH);     
        
        formatTable(table, Sizes.MEAL_DIARY_TABLE_COLUMNS_COUNT, 
                Sizes.MEAL_DIARY_TABLE_LEFT_COLUMN_WIDTH, 
                Sizes.MEAL_DIARY_TABLE_COLUMN_WIDTH);
        tablePanel.add(table.getTableHeader());
        tablePanel.add(table);
        
        JLabel dayStatisticsLabel = createJLabel(DataFormatter.
                getDayStatisticsInString(day), Colors.DAY_STATISTICS_LABEL_COLOR);
        Dimension dimension = new Dimension(Sizes.MEAL_DIARY_TABLE_FRAME_WIDTH,
                Sizes.DAY_STATISTICS_LABEL_HEIGHT);
        dayStatisticsLabel.setPreferredSize(dimension);
        dayStatisticsLabel.setVerticalTextPosition(SwingConstants.TOP);
        labelsAndTablePanel.add(dayStatisticsLabel, BorderLayout.SOUTH);
        
        mainPanel.add(labelsAndTablePanel);
        
        return mainPanel;
    }


#6
lethalwire

lethalwire

    while(false){ ... }

  • Members
  • PipPipPipPipPipPipPip
  • 748 posts
  • Programming Language:Java, PHP
  • Learning:Java, PHP
I'm not sure if I'm understanding you correctly, but if you are going to be adding several panels that contain: 2 jlabels, and 1 jtable, then I might set up a separate class alone that extends jpanel.
Be sure to pass the data you need for jlabel and jtable INTO this class.
You'll have to do a little calculation on how you want the width of this jpanel.

In order for the jlabels to lineup, set the scrollpane of the jtable, or the jtable itself ( depends on how you are adding it to the panel ) width as wide as the panel. That may sound confusing, so here's a picture example:

Attached File  jtable spacing.png   17.05K   56 downloads

If you set the width correctly, it should correctly push the jlabels into the correct position and the jtable.

Now that you have this class, you can add it to your main panel as much as you'd like.
Example:

Attached File  jtable spacing example.png   44.55K   79 downloads

I hope this is near/close to what you want. I'm usually great at misunderstanding questions. :)

#7
thatsme

thatsme

    Programmer

  • Members
  • PipPipPipPip
  • 176 posts
Acctually, what i need is tables each being below previuos table, not to the right side of previuos :worry:

#8
lethalwire

lethalwire

    while(false){ ... }

  • Members
  • PipPipPipPipPipPipPip
  • 748 posts
  • Programming Language:Java, PHP
  • Learning:Java, PHP
Then you'll change the height of your main panel and reduce the width.




1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users