Hi,
I posted recently, but I got no satisfatorial reponse. I guess I hadnīt perfectly exposed my problem. The problem is the following... why in the following code the GridBagLayout is not respected (9/1 proportion) when adding a new text line to text area?
Thanks,
Miguel
Code:
package Main;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.text.*;
public class ExampleGUI3 extends JFrame{
private JTabbedPane tabbedPane;
private JTextPane messageArea;
JPanel mainPanel;
private class NewTabListener implements ActionListener{
public void actionPerformed(ActionEvent arg0){
StyledDocument doc = (StyledDocument)messageArea.getDocument();
try{
doc.insertString(doc.getLength(), "new line\n", null);
}
catch (BadLocationException e){
e.printStackTrace();
}
tabbedPane.add("tab", new JPanel());
mainPanel.revalidate();
}
}
private GridBagConstraints setGridBagConstraints(int gridx,
int gridy,
int gridwidth,
int gridheight,
double weightx,
double weighty,
int fill,
int anchor,
int insets1,
int insets2,
int insets3,
int insets4){
GridBagConstraints gridConstr = new GridBagConstraints();
gridConstr.gridx = gridx;
gridConstr.gridy = gridy;
gridConstr.gridwidth = gridwidth;
gridConstr.gridheight = gridheight;
gridConstr.weightx = weightx;
gridConstr.weighty = weighty;
gridConstr.fill = fill;
gridConstr.anchor = anchor;
/* Insets(int top, int left, int bottom, int right) */
gridConstr.insets = new Insets(insets1, insets2, insets3, insets4);
return gridConstr;
}
public ExampleGUI3(){
setTitle("Example GUI");
setDefaultCloseOperation(EXIT_ON_CLOSE);
tabbedPane = new JTabbedPane();
tabbedPane.setPreferredSize(new Dimension(800, 540));
messageArea = new JTextPane(){
public Dimension getPreferredScrollableViewportSize(){
return new Dimension(800, 60);
}
};
JScrollPane jsp = new JScrollPane(messageArea);
mainPanel = new JPanel();
mainPanel.setPreferredSize(new Dimension(800, 600));
mainPanel.setLayout(new GridBagLayout());
GridBagConstraints gridConstr;
gridConstr = setGridBagConstraints
(0, 0, 1, 1, 1.0, 0.9, GridBagConstraints.BOTH,
//gx,gy,gw,gh,wx, wy
GridBagConstraints.CENTER, 0, 0, 0, 0);
mainPanel.add(tabbedPane, gridConstr);
gridConstr = setGridBagConstraints
(0, 1, 1, 1, 1.0, 0.1, GridBagConstraints.BOTH,
GridBagConstraints.CENTER, 0, 0, 0, 0);
mainPanel.add(jsp, gridConstr);
JMenuBar menuBar = new JMenuBar();
JMenu menu = new JMenu("menu");
JMenuItem newTab = new JMenuItem("new tab");
menu.add(newTab);
menuBar.add(menu);
newTab.addActionListener(new NewTabListener());
setJMenuBar(menuBar);
getContentPane().add(mainPanel, BorderLayout.CENTER);
pack();
}
public static void main(String[] args){
ExampleGUI exampleGUI = new ExampleGUI();
exampleGUI.setVisible(true);
}
}