2016-03-24 3 views
1

В настоящее время я хочу отобразить структуру таблицы с использованием JPanel с gridbaglayout, но у меня есть некоторые проблемы с ее проектированием.Структура таблицы отображения с поворотным столом с Gridbaglayout

Мои данные хранятся в структуре узла (анализируется из XML-документа). В моем коде я каждый раз создаю новую панель, если узел имеет группу других узлов (рекурсивный метод showGroupField). У меня также есть метод, который создает gridbagconstraint, который дает мне структуру таблицы. Проблема в том, что когда новая панель добавляется в основную панель, она прикрепляется к первому столбцу gridbaglayout и не использует всю ширину основной панели. Кто-нибудь есть идея, как это решить? Большое спасибо!

Это то, что я сейчас:

What I have now

И это то, что я хочу:

What I want

Код:

package view; 

import domain.NodeInfo; 

public class InputScreen { 

    ... 

    public InputScreen(Node parentNode){ 
     this.parentNode = parentNode; 
     nodeInfo = new NodeInfo(); 
    } 

    public void initiateInputScreen() { 
     mainFrame = new JFrame("Front-End Generator"); 
     mainFrame.setSize(1000,1000); 

     contentPanel = new JPanel(); 
     inputPanel = new JPanel(); 

     makeContentPanel(); 

     mainFrame.setContentPane(contentPanel); 
     mainFrame.pack(); 
     mainFrame.setLocationRelativeTo(null); 
     mainFrame.setVisible(true); 
     mainFrame.setResizable(false); 
    } 

    private void makeContentPanel() { 
     contentPanel.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5)); 
     contentPanel.setLayout(new BorderLayout(5,5)); 

     makeInputPanel(); 

     contentPanel.add(inputPanel, BorderLayout.CENTER); 
    } 

    private void makeInputPanel() { 
     inputPanel.setBorder(BorderFactory.createTitledBorder("Inputs")); 
     inputPanel.setLayout(new GridBagLayout()); 

     inputPanel.add(new JLabel(nodeInfo.getNodeInformation(parentNode)), createGbc(0, 0)); 

     inputPanel.add(showGroupField(parentNode.getFirstChild()), createGbc(0,1));  
    } 

    private JPanel showGroupField(Node node) { 
     int count = 0; 
     JPanel tmpPanel = new JPanel(new GridBagLayout()); 
     tmpPanel.setBorder(BorderFactory.createTitledBorder("Test")); 

     NodeList listNode = node.getChildNodes(); 
     for(int i=0; i<listNode.getLength(); i++){ 
      if(nodeInfo.isGroupField(listNode.item(i))){ 
       tmpPanel.add(showGroupField(listNode.item(i)), createGbc(0, count)); 
       count++; 
      }else{ 
       tmpPanel.add(new JLabel("Test"), createGbc(0, count)); 
       tmpPanel.add(new JTextField(20), createGbc(1, count)); 
       count++; 
      } 
     } 
     return tmpPanel; 
    } 

    private GridBagConstraints createGbc(int x, int y){ 
     GridBagConstraints gbc = new GridBagConstraints(); 
     gbc.gridx = x; 
     gbc.gridy = y; 
     gbc.gridwidth = 1; 
     gbc.gridheight = 1; 

     gbc.anchor = (x==0) ? GridBagConstraints.WEST : GridBagConstraints.EAST; 
     gbc.fill = (x==0) ? GridBagConstraints.BOTH : GridBagConstraints.HORIZONTAL; 

     gbc.insets = (x==0) ? new Insets(5,0,5,5) : new Insets(5,5,5,0); 
     gbc.weightx = (x==0) ? 0.1 : 1.0; 
     gbc.weighty = 1.0; 
     return gbc; 
    } 
} 
+0

Для лучшей помощи в ближайшее время отправьте сообщение [MCVE] или [Short, Self Contained, Correct Example] (http://www.sscce.org/). –

ответ

0

Попробуйте добавить inputPanel к WEST pos ition.

contentPanel.add(inputPanel, BorderLayout.WEST); 
Смежные вопросы