Please join us at the new JavaWorld Q&A Forums. Your existing login will work there. The discussions here are now read-only.


JavaWorld >> Java Beginner

Pages: 1
miguelAtos
stranger


Reged: 07/18/08
Posts: 2
Problem with text area and GridBagLayout
      #176750 - 07/29/08 07:47 AM

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);
}
}




Post Extras: Print Post   Remind Me!   Notify Moderator  
hiwa
Carpal Tunnel


Reged: 06/21/03
Posts: 7704
Loc: Japan
Re: Problem with text area and GridBagLayout [Re: miguelAtos]
      #176791 - 07/29/08 05:41 PM

# but I got no satisfatorial reponse
I gave a satisfactory response, already.
In your current main() method, old version of your class is instantiated.

--------------------
*stop cruelty* Annual number of euthanized cats&dogs: US 5M, JP 500K.*for our better karma*


Post Extras: Print Post   Remind Me!   Notify Moderator  
Pages: 1



Extra information
0 registered and 3 anonymous users are browsing this forum.

Moderator:   

Print Topic

Forum Permissions
      You cannot start new topics
      You cannot reply to topics
      HTML is disabled
      UBBCode is enabled

Rating:
Topic views: 4886

Rate this topic

Jump to

Contact us JavaWorld

Powered by UBB.threads™ 6.5.5