automatically enlarging the components problem

Hi,

I am new to swing. I am doing small experiments in Swing application.

With the following code I am facing a problem,

panel2.setLayout(new GridBagLayout());

((GridBagLayout)panel2.getLayout()).columnWidths = new int[] {151, 0};

((GridBagLayout)panel2.getLayout()).rowHeights = new int[] {496, 0};

splitPane1.setRightComponent(panel2);

gridbag.setConstraints(splitPane1, c);

add(splitPane1);

In the bolded lines above i have given the column and row size of a panel. I added this panel. I extended Applet to my class. so i am using add(...) method. I am adding this class to a JFrame.

Now when i enlarge the my Frame, the Panel should automatically enlarge. When i make my window as small according the Panel should behave, But it is not happening so. Panel is added in the center of my Frame. My Panel is not behaving according to it's super window(i.e., Frame).

Please Help in this regard.

Regards,

Ravi

[1001 byte] By [rkrgarlapatia] at [2007-11-26 19:20:09]
# 1
You might want to post a Short,Self Contained, Compilable and Executable, Example Program.
zadoka at 2007-7-9 21:37:53 > top of Java-index,Desktop,Core GUI APIs...
# 2

actually i was asking whether my bolded lines should be placed or not.

i was also asking how my internal components will behave as my JFrame behaves....i was using GridBagLayout....in this all my Panel components are coming into CENTER of my Frame. my internal Panel is not behaving as my JFame....how all my internal frame will get elarged automatically..................... plz help me....i am totally cofused with this concept......

rkrgarlapatia at 2007-7-9 21:37:53 > top of Java-index,Desktop,Core GUI APIs...
# 3

> actually i was asking whether my bolded lines should

> be placed or not.

> i was also asking how my internal components will

> behave as my JFrame behaves....i was using

> GridBagLayout....in this all my Panel components are

> coming into CENTER of my Frame. my internal Panel is

> not behaving as my JFame....how all my internal frame

> will get elarged automatically.....................

> plz help me....i am totally cofused with this

> concept......

1. It appears that your period key gets stuck when you are typing sometimes.

2. You might want to post a Short,Self Contained, Compilable and Executable, Example Program.

zadoka at 2007-7-9 21:37:53 > top of Java-index,Desktop,Core GUI APIs...
# 4
zakok,i dont understand what u mean.... :-(r u asking my complete code.
rkrgarlapatia at 2007-7-9 21:37:53 > top of Java-index,Desktop,Core GUI APIs...
# 5
> zakok,> > i dont understand what u mean.... :-(> r u asking my complete code. http://mindprod.com/jgloss/sscce.html
zadoka at 2007-7-9 21:37:53 > top of Java-index,Desktop,Core GUI APIs...
# 6

plz find my code :::: It of nearly 400 lines

/*

* Created by JFormDesigner on Mon Feb 05 10:48:09 GMT+05:30 2007

*/

import java.awt.*;

import java.awt.event.*;

import javax.swing.tree.*;

import javax.swing.*;

import java.awt.event.ActionListener;

import java.io.*;

import java.util.*;

import javax.swing.event.*;

/**

* @author Appu b

*/

public class MainFrame extends JFrame {

public MainFrame() {

initComponents();

}

class Closer1 extends WindowAdapter {

public void windowClosing (WindowEvent event) {

System.exit (0);

}

}

class Closer2 implements ActionListener {

public void actionPerformed (ActionEvent event) {

System.exit (0);

}

}

DefaultMutableTreeNode child1 =null;

private void sub_nodes(DefaultMutableTreeNode child1)

{

DefaultMutableTreeNode subchild1 = new DefaultMutableTreeNode("Queues");

DefaultMutableTreeNode subchild2 = new DefaultMutableTreeNode("Channels");

DefaultMutableTreeNode subchild3 = new DefaultMutableTreeNode("Client Connections");

DefaultMutableTreeNode subchild4 = new DefaultMutableTreeNode("Process Definitions");

DefaultMutableTreeNode subchild5 = new DefaultMutableTreeNode("Namelists");

child1.add(subchild1);

child1.add(subchild2);

child1.add(subchild3);

child1.add(subchild4);

child1.add(subchild5);

}

public void showContextMenuTree(MouseEvent me, JTree comp) {

Point pt = SwingUtilities.convertPoint(me.getComponent(),me.getPoint(),comp);

JPopupMenu popup = new JPopupMenu();

int x = pt.x;

int y = pt.y;

//System.out.println("Here 111");

if(x <=48 && y <=16) // This condition will popup the menu whenever we click on 'Root node'

{

ActionListener menuListener = new ActionListener() {

public void actionPerformed(ActionEvent event) {

if(event.getActionCommand().equals("Add Queue Manager"))

add_manager();

}

};

JMenuItem item;

popup.add(item = new JMenuItem("Add Queue Manager", KeyEvent.VK_A));

item.addActionListener(menuListener);

popup.show(comp, x,y);

}

}

private void tree_data_display()

{

root = new DefaultMutableTreeNode("Root");

try

{

BufferedReader in2 = new BufferedReader(new FileReader("DataFile.txt"));

StringTokenizer st = null;

String qmname = "",str = "";

while((str = in2.readLine()) != null)

{

st = new StringTokenizer(str,"~");

qmname = st.nextToken();

child1 = new DefaultMutableTreeNode(qmname);

sub_nodes(child1);

root.add(child1);

}

}catch(Exception ex){System.out.println(ex);}

treeModel = new DefaultTreeModel(root);

tree = new JTree(treeModel);

tree.getSelectionModel().setSelectionMode(TreeSelectionModel.SINGLE_TREE_SELECTION);

tree.addTreeSelectionListener(new TreeSelectionListener()

{

public void valueChanged(TreeSelectionEvent e) {

DefaultMutableTreeNode node = (DefaultMutableTreeNode)

tree.getLastSelectedPathComponent();

if (node == null) return;

Object nodeInfo = node.getUserObject();

if (node.isLeaf()) {

//System.out.print(node.toString());

if(node.toString().equals("Queues"))

{

scrollPane2.setViewportView(new TreeDemo());

panel2.add(scrollPane2, new GridBagConstraints(0, 0, 1, 1, 0.0, 0.0,

GridBagConstraints.CENTER, GridBagConstraints.BOTH,

new Insets(0, 0, 0, 0), 0, 0));

{

panel2.setLayout(new GridBagLayout());

((GridBagLayout)panel2.getLayout()).columnWidths = new int[] {517, 0};

((GridBagLayout)panel2.getLayout()).rowHeights = new int[] {493, 0};

((GridBagLayout)panel2.getLayout()).columnWeights = new double[] {0.0, 1.0E-4};

((GridBagLayout)panel2.getLayout()).rowWeights = new double[] {0.0, 1.0E-4};

}

splitPane1.setRightComponent(panel2);

}

}

else

{

}

}

});

tree.addMouseListener(new MouseAdapter() {

public void mouseClicked(MouseEvent e) {}

public void mousePressed(MouseEvent e) {

//if (e.isPopupTrigger())

if (e.getButton() == 3) // right click value is 3

{

if (SwingUtilities.isRightMouseButton(e)) {

TreePath selPath = tree.getPathForLocation(e.getX(), e.getY());

tree.setSelectionPath(selPath);

showContextMenuTree(e, tree);

}

}

if (e.isPopupTrigger())

add_manager();

}

public void mouseReleased(MouseEvent e) {}

public void mouseEntered(MouseEvent e) {}

public void mouseExited(MouseEvent e) {}

});

getContentPane().add(new JScrollPane(tree));

panel1.setLayout(new GridBagLayout());

((GridBagLayout)panel1.getLayout()).columnWidths = new int[] {151, 0};

((GridBagLayout)panel1.getLayout()).rowHeights = new int[] {496, 0};

((GridBagLayout)panel1.getLayout()).columnWeights = new double[] {0.0, 1.0E-4};

((GridBagLayout)panel1.getLayout()).rowWeights = new double[] {0.0, 1.0E-4};

scrollPane1.setViewportView(tree);

panel1.add(scrollPane1, new GridBagConstraints(0, 0, 1, 1, 0.0, 0.0,

GridBagConstraints.CENTER, GridBagConstraints.BOTH,

new Insets(0, 0, 0, 0), 0, 0));

splitPane1.setLeftComponent(panel1);

}

void add_manager()

{

//createQM myDialog = new createQM(f, true);

tree_data_display();

}

private void initComponents() {

// JFormDesigner - Component initialization - DO NOT MODIFY //GEN-BEGIN:initComponents

// Generated using JFormDesigner Evaluation license - Appu b

f=new JFrame();

menuPanel = new JPanel();

splitPane1 = new JSplitPane();

panel1 = new JPanel();

scrollPane1 = new JScrollPane();

scrollPane2 = new JScrollPane();

//tree1 = new JTree();

panel2 = new JPanel();

//tree_data_display();

//======== this ========

contentPane = getContentPane();

contentPane.setLayout(new GridBagLayout());

((GridBagLayout)contentPane.getLayout()).columnWidths = new int[] {680, 0};

((GridBagLayout)contentPane.getLayout()).rowHeights = new int[] {0, 246, 0};

((GridBagLayout)contentPane.getLayout()).columnWeights = new double[] {0.0, 1.0E-4};

((GridBagLayout)contentPane.getLayout()).rowWeights = new double[] {0.0, 0.0, 1.0E-4};

//======== menuPanel ========

{

menuPanel.setLayout(new GridBagLayout());

//((GridBagLayout)menuPanel.getLayout()).columnWidths = new int[] {676, 0};

//((GridBagLayout)menuPanel.getLayout()).rowHeights = new int[] {0};

//((GridBagLayout)menuPanel.getLayout()).columnWeights = new double[] {0.0, 1.0E-4};

//((GridBagLayout)menuPanel.getLayout()).rowWeights = new double[] {1.0E-4};

Handler = new Closer1 ();

exitHandler = new Closer2();

menuBar = new JMenuBar();

//Build the first menu.

fileMenu = new JMenu("File");

fileMenu.setMnemonic(KeyEvent.VK_F);

menuBar.add(fileMenu);

categoriesMenu = new JMenu("Categories");

categoriesMenu.setMnemonic(KeyEvent.VK_C);

fileMenu.add(categoriesMenu);

//a group of JMenuItems

categoriesMenuItem1 = new JMenuItem("Add Queue Manager", KeyEvent.VK_A);

categoriesMenuItem1.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_1, ActionEvent.ALT_MASK));

//categoriesMenuItem1.addActionListener(QMHandler);

categoriesMenuItem1.addActionListener(new ActionListener() {

public void actionPerformed(ActionEvent e) {

//f.enable(false);

//new createQM();

add_manager();

//System.out.println("hi ::: "+f);

}

});

categoriesMenu.add(categoriesMenuItem1);

categoriesMenu.addSeparator();

categoriesMenuItem2 = new JMenuItem("New Category", KeyEvent.VK_W);

categoriesMenuItem2.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_2, ActionEvent.ALT_MASK));

categoriesMenu.add(categoriesMenuItem2);

categoriesMenuItem3 = new JMenuItem("Modify Category", KeyEvent.VK_M);

categoriesMenuItem3.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_3, ActionEvent.ALT_MASK));

categoriesMenuItem3.setEnabled(false);

categoriesMenu.add(categoriesMenuItem3);

categoriesMenuItem4 = new JMenuItem("Delete Category", KeyEvent.VK_D);

categoriesMenuItem4.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_4, ActionEvent.ALT_MASK));

categoriesMenuItem4.setEnabled(false);

categoriesMenu.add(categoriesMenuItem4);

queueManagerMenuItem = new JMenuItem("Queue Managers");

queueManagerMenuItem.setMnemonic(KeyEvent.VK_M);

queueManagerMenuItem.setEnabled(false);

fileMenu.add(queueManagerMenuItem);

queueMenuItem = new JMenuItem("Queues");

queueMenuItem.setMnemonic(KeyEvent.VK_Q);

queueMenuItem.setEnabled(false);

fileMenu.add(queueMenuItem);

channelMenuItem = new JMenuItem("Channels");

channelMenuItem.setMnemonic(KeyEvent.VK_H);

channelMenuItem.setEnabled(false);

fileMenu.add(channelMenuItem);

clientConnectionMenuItem = new JMenuItem("Client Connections");

clientConnectionMenuItem.setMnemonic(KeyEvent.VK_L);

clientConnectionMenuItem.setEnabled(false);

fileMenu.add(clientConnectionMenuItem);

processDefinitionMenuItem = new JMenuItem("Process Definitions");

processDefinitionMenuItem.setMnemonic(KeyEvent.VK_P);

processDefinitionMenuItem.setEnabled(false);

fileMenu.add(processDefinitionMenuItem);

nameListsMenuItem = new JMenuItem("Name lists");

nameListsMenuItem.setMnemonic(KeyEvent.VK_N);

nameListsMenuItem.setEnabled(false);

fileMenu.add(nameListsMenuItem);

fileMenu.addSeparator();

exitMenuItem = new JMenuItem("Exit");

exitMenuItem.setMnemonic(KeyEvent.VK_N);

exitMenuItem.addActionListener(exitHandler);

fileMenu.add(exitMenuItem);

//Build second menu in the menu bar.

editMenu = new JMenu("Edit");

editMenu.setMnemonic(KeyEvent.VK_E);

menuBar.add(fileMenu);

menuBar.add(editMenu);

menuPanel.add(menuBar);

}

contentPane.add(menuPanel, new GridBagConstraints(0, 0, 1, 1, 0.0, 0.0,

GridBagConstraints.WEST, GridBagConstraints.VERTICAL,

new Insets(0, 0, 0, 0), 0, 0));

//======== splitPane1 ========

{

tree_data_display();

}

//======== panel2 ========

{

panel2.setLayout(new GridBagLayout());

((GridBagLayout)panel2.getLayout()).columnWidths = new int[] {517, 0};

((GridBagLayout)panel2.getLayout()).rowHeights = new int[] {493, 0};

((GridBagLayout)panel2.getLayout()).columnWeights = new double[] {0.0, 1.0E-4};

((GridBagLayout)panel2.getLayout()).rowWeights = new double[] {0.0, 1.0E-4};

}

splitPane1.setRightComponent(panel2);

contentPane.add(splitPane1, new GridBagConstraints(0, 1, 1, 1, 0.0, 0.0,

GridBagConstraints.NORTHWEST, GridBagConstraints.NONE,

new Insets(0, 0, 0, 0), 0, 0));

f.setTitle ("WMQ Sample");

f.getContentPane().setLayout(new FlowLayout());

f.setSize(700,550);

f.addWindowListener (Handler);

f.getContentPane().add(contentPane);

f.setVisible(true);

//f.show();

//pack();

//setLocationRelativeTo(getOwner());

}

public static void main (String args[]) throws Exception {

///String lnfName = "com.sun.java.swing.plaf.windows.WindowsLookAndFeel";

//UIManager.setLookAndFeel(lnfName);

try {

UIManager.setLookAndFeel(

UIManager.getSystemLookAndFeelClassName());

} catch (Exception e) {

System.err.println("Couldn't use system look and feel.");

}

//SwingUtilities.updateComponentTreeUI();

new MainFrame ();

}

DefaultMutableTreeNode root;

DefaultTreeModel treeModel;

private JPanel menuPanel;

private JSplitPane splitPane1;

private JPanel panel1;

private JScrollPane scrollPane1;

private JScrollPane scrollPane2;

private JTree tree;

private JPanel panel2;

public Closer1 Handler;

public Closer2 exitHandler;

//public QM QMHandler;

public JFrame f;

public JMenuBar menuBar;

public JMenu menu, submenu;

public JMenu fileMenu;

public JMenu editMenu;

public JMenu viewMenu;

public JMenu toolMenu;

public JMenuItem menuItem;

public JMenu categoriesMenu;

public JMenuItem categoriesMenuItem1;

public JMenuItem categoriesMenuItem2;

public JMenuItem categoriesMenuItem3;

public JMenuItem categoriesMenuItem4;

public JMenuItem queueManagerMenuItem;

public JMenuItem queueMenuItem;

public JMenuItem channelMenuItem;

public JMenuItem clientConnectionMenuItem;

public JMenuItem processDefinitionMenuItem;

public JMenuItem nameListsMenuItem;

public JMenuItem exitMenuItem;

public JMenuItem quitMenuItem;

public Container contentPane;

}

rkrgarlapatia at 2007-7-9 21:37:53 > top of Java-index,Desktop,Core GUI APIs...
# 7
> plz find my code :::: It of nearly 400 linesMaybe you missed it in the link I provided:The first S stands for short.
zadoka at 2007-7-9 21:37:53 > top of Java-index,Desktop,Core GUI APIs...
# 8

To expand on why I am asking for this example: Your code is big and contains a lot of junk that is not related to your problem.You are the best person to pair down your code to the core issue. Otherwise you are wasting the time of everyone here who has to do that when they look at your code.

Also, many times when you create this example you will notice that the problem goes away. That is because when you cut your program down from 400 to 20 lines of code, you cut out the problem. When this happens add, piece by piece till it breaks again. When it does that last piece is the culprit.

If you don't post an example like such, someone still might help you. Both others including myself will ignore your post until you put forth some effort in creating the example.

zadoka at 2007-7-9 21:37:53 > top of Java-index,Desktop,Core GUI APIs...
# 9

Here you go.....i could able to put my problem in a small code. Hope this size of code is good enough......if you say what is the problem in my below code, that could solve my problem......Hooooopfully.....

I assume, the main culprit of the code is my bolded lines below.....but i am unable to rectify it.....plz help me in this regard ::::::::

import java.awt.*;

import javax.swing.*;

class Sample extends JPanel

{

public static void main(String str[])

{

JPanel panel1 = new JPanel();

panel1.setLayout(new GridBagLayout());

((GridBagLayout)panel1.getLayout()).columnWidths = new int[] {151, 0};

((GridBagLayout)panel1.getLayout()).rowHeights = new int[] {496, 0};

((GridBagLayout)panel1.getLayout()).columnWeights = new double[] {0.0, 1.0E-4};

((GridBagLayout)panel1.getLayout()).rowWeights = new double[] {0.0, 1.0E-4};

panel1.add(new Button("sample"), new GridBagConstraints(0, 0, 1, 1, 0.0, 0.0,

GridBagConstraints.CENTER, GridBagConstraints.BOTH,

new Insets(0, 0, 0, 0), 0, 0));

Container cont = new Container();

cont.setLayout(new GridBagLayout());

cont.add(panel1);

JFrame f = new JFrame();

f.setTitle ("WMQ Sample");

f.getContentPane().setLayout(new GridBagLayout());

f.setSize(700,550);

f.getContentPane().add(cont);

f.setVisible(true);

}

}

rkrgarlapatia at 2007-7-9 21:37:53 > top of Java-index,Desktop,Core GUI APIs...
# 10

Ok. I am not sure what you want this panel to do but some suggestions:

You are specifying overrides to the columnwidths and row widths. I am not sure you are using gridBagLayout correct, or at least in the way you want;

I suggest you read over this: http://java.sun.com/docs/books/tutorial/uiswing/layout/gridbag.html

Also, always thing about the layout that this panel is in and its parents.

zadoka at 2007-7-9 21:37:53 > top of Java-index,Desktop,Core GUI APIs...