JAVA: Alignement on GridBagLayout

Asked by At

I'm looking for doing an Overlay of Teamspeak3...

The problem is, I can't manage to align a JTable as I want

Screenshot:

http://you-belge.eu/files/2014-03-21_230603.jpg

I want the Table (with 2 names on the screen) to be on Top Left

Here is my code:

public void setPanelContent(Object[][] clientList, Object[][] msgList)
{

    JPanel jp_ = new JPanel();

    jp_.setLayout(new GridBagLayout());

    GridBagConstraints g = new GridBagConstraints();

    g.gridx = 1;
    //g.fill = GridBagConstraints.HORIZONTAL;
    g.anchor = GridBagConstraints.FIRST_LINE_START;
    g.weightx = 1;

    String  title[] = {"A", "B"};
    JTable tableau = new JTable(clientList, title);


    tableau.setTableHeader(null);
    tableau.setRowHeight(20);
    tableau.setShowGrid(false);
    tableau.setEnabled(false);

    // tableau.setOpaque(false);
    ((DefaultTableCellRenderer)tableau.getDefaultRenderer(Object.class)).setOpaque(false);

    tableau.getColumn("A").setMaxWidth(30);
    tableau.getColumn("B").setMinWidth(210);
    // tableau.getColumn("B").setMaxWidth(520);
    tableau.getColumnModel().getColumn(0).setCellRenderer(new ImageRenderer());

    jp_.add(tableau, g);
    jp_.setBackground(Color.CYAN); // for test

    jp_.setPreferredSize(new Dimension(jp.getWidth(), jp.getHeight()));

    jp.removeAll();
    jp.invalidate();

    jp.add(jp_);
    jp.updateUI();
    this.repaint();
}

Help would be appreciated :>

Thanks in advance

2 Answers

0
Mathisca On Best Solutions

Try to make a JPanel in the top of your window (BorderLayout.NORTH), a new JPanel inside the JPanel you just created with BorderLayout WEST and then add your JTable insite this last JPanel.

0
xav On
  • If you want your table content to be on Top Left (not your table itself, since it would take all GridBagLayout vertical space), you can extend this JTable height by adding this:

    g.fill = GridBagConstraints.VERTICAL;
    g.weighty = 1;
    
  • If you want your table itself to be on Top Left, you can use this:

    g.anchor = GridBagConstraints.NORTHWEST;
    g.weighty = 1;