gridBagLayout() hell

Hello,

I have almost completed the GUI for my RAT client although there are layout problems i.e. some elements are not where they should be.

This is what the layout should be like:

-

/ label/ /

/- / /

/textField/ JEditorPane /

/--/ /

/ textField/ /

/- / /

/textField/ /

/-

Obviously there are more labels and textFields as shown in the code below. I have tried to edit the properties for each label, textField etc, I have also tried to relate it to the tutorials on Java swing but with no luck. Please help.

import java.net.*;

import javax.swing.text.html.*;

import javax.swing.text.*;

import javax.swing.event.*;

import javax.swing.*;

import java.io.*;

import java.awt.*;

import java.awt.event.*;

import java.util.*;

import java.applet.Applet;

/**

* Requires RatServer to be running

* set path="C:\Program Files\Java\jdk1.5.0_08\bin"

*/

publicclass RatClientimplements ActionListener

{

JTextField host =null;

JTextField port =null;

JTextField cmd =null;

JTextField file =null;

JEditorPane jep =null;

BufferedReader in =null;

JScrollPane scroll =null;

public Component createComponents()

{

JPanel pane =new JPanel(new GridBagLayout());

GridBagConstraints c =new GridBagConstraints();

c.fill = GridBagConstraints.HORIZONTAL;

jep =new JEditorPane();

c.gridx = 2;

c.gridy = 0;

c.ipadx = 40;

c.ipady = 40;

c.weightx = 0.0;

c.gridwidth = 3;

c.gridheight = -14;

pane.add(jep, c);

scroll =new JScrollPane(jep);

c.gridx = 2;

c.gridy = 0;

c.ipadx = 40;

c.ipady = 40;

c.weightx = 0.0;

c.gridwidth = 3;

pane.add(scroll, c);

JLabel hostLabel =new JLabel("Enter host: ");

c.ipadx = 10;

c.ipady = 10;

c.gridx = 0;

c.gridy = 0;

pane.add(hostLabel, c);

host =new JTextField("192.168.0.4");

c.gridx = 0;

c.gridy = 1;

pane.add(host, c);

JLabel portLabel =new JLabel("Enter port: ");

c.gridx = 0;

c.gridy = 2;

pane.add(portLabel, c);

port =new JTextField("80");

c.gridx = 0;

c.gridy = 3;

pane.add(port, c);

JLabel cmdLabel =new JLabel("Send a command: ");

c.gridx = 0;

c.gridy = 4;

pane.add(cmdLabel, c);

cmd =new JTextField("GetInfo");

c.gridx = 0;

c.gridy = 5;

pane.add(cmd, c);

JLabel fileLabel =new JLabel("Send a file: ");

c.gridx = 0;

c.gridy = 6;

pane.add(fileLabel, c);

file =new JTextField("C:\\misc.txt");

c.gridx = 0;

c.gridy = 7;

pane.add(file, c);

JButton button =new JButton("Connect");

c.gridx = 0;

c.gridy = 8;

button.addActionListener(this);

pane.add(button, c);

return pane;

privatestaticvoid Gui()

{

//Create and set up the window.

JFrame frame =new JFrame("RatClient");

frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

RatClient app =new RatClient();

Component contents = app.createComponents();

frame.getContentPane().add(contents);

//Display the window.

frame.pack();

frame.setVisible(true);

}

}

[5291 byte] By [John4938a] at [2007-10-3 4:35:37]
# 1
That diagram did not turn out right. Basicly the labels and textFields should be to the left of the JEditorPane.Cheers anyone
John4938a at 2007-7-14 22:39:20 > top of Java-index,Desktop,Core GUI APIs...
# 2

> That diagram did not turn out right.

Which is why the forum has a "Preview" button. So you can make sure the question is presentable. Then you could have used the "Code" tags on your diagram since they preserve spaces. After over 100 postings you should know this by now.

> Please help.

The code doesn't compile so we can't see what your current problem is.

> gridBagLayout() hell

Then don't use GridBayLayout. Nobody says you are forced to use a single Layout Manager for the entire GUI.

> Basicly the labels and textFields should be to the left of the JEditorPane

Then create a separate panel and add the components to the panel using the relevant LayoutManager. Then add the panel to the West of the BorderLayout. Then add your JEditorPane to the Center of the BorderLayout.

If you need further help then you need to create a [url http://homepage1.nifty.com/algafield/sscce.html]Short, Self Contained, Compilable and Executable, Example Program[/url] (SSCCE) that demonstrates the incorrect behaviour, because I can't guess exactly what you are doing based on the information provided.

And don't forget to use the [url http://forum.java.sun.com/help.jspa?sec=formatting]Code Formatting Tags[/url] so the code retains its original formatting.

camickra at 2007-7-14 22:39:20 > top of Java-index,Desktop,Core GUI APIs...
# 3

Ok heres an SSCCE that shows the incorrect layout.

Ive tried using another panel also but to no avail.

At the moment its not showing the JEditorPane at all.

Please help again.

Cheers

import java.net.*;

import javax.swing.text.html.*;

import javax.swing.text.*;

import javax.swing.event.*;

import javax.swing.*;

import java.io.*;

import java.awt.*;

import java.awt.event.*;

import java.util.*;

import java.applet.Applet;

/**

* Requires RatServer to be running

* set path="C:\Program Files\Java\jdk1.5.0_08\bin"

*/

public class RatClient2 implements ActionListener

{

JTextField host = null;

JTextField port = null;

JTextField cmd = null;

JTextField file = null;

JEditorPane jep = null;

BufferedReader in = null;

JScrollPane scroll = null;

public Component createComponents()

{

JPanel paneForJep = new JPanel(new GridBagLayout());

GridBagConstraints gbc = new GridBagConstraints();

jep = new JEditorPane();

gbc.gridx = 2;

gbc.gridy = 0;

gbc.ipadx = 40;

gbc.ipady = 40;

paneForJep.add(jep, gbc);

scroll = new JScrollPane(jep);

gbc.gridx = 2;

gbc.gridy = 0;

gbc.ipadx = 40;

gbc.ipady = 40;

paneForJep.add(scroll, gbc);

JPanel pane = new JPanel(new GridBagLayout());

GridBagConstraints c = new GridBagConstraints();

c.fill = GridBagConstraints.HORIZONTAL;

JLabel hostLabel = new JLabel("Enter host: ");

c.ipadx = 10;

c.ipady = 10;

c.gridx = 0;

c.gridy = 0;

pane.add(hostLabel, c);

host = new JTextField("192.168.0.4");

c.gridx = 0;

c.gridy = 1;

pane.add(host, c);

JLabel portLabel = new JLabel("Enter port: ");

c.gridx = 0;

c.gridy = 2;

pane.add(portLabel, c);

port = new JTextField("80");

c.gridx = 0;

c.gridy = 3;

pane.add(port, c);

JLabel cmdLabel = new JLabel("Send a command: ");

c.gridx = 0;

c.gridy = 4;

pane.add(cmdLabel, c);

cmd = new JTextField("GetInfo");

c.gridx = 0;

c.gridy = 5;

pane.add(cmd, c);

JLabel fileLabel = new JLabel("Send a file: ");

c.gridx = 0;

c.gridy = 6;

pane.add(fileLabel, c);

file = new JTextField("C:\\a.jpg");

c.gridx = 0;

c.gridy = 7;

pane.add(file, c);

JButton button = new JButton("Connect");

c.gridx = 0;

c.gridy = 8;

button.addActionListener(this);

pane.add(button, c);

return pane;

}

public void actionPerformed(ActionEvent e)

{

String host2 = host.getText();

String port2 = port.getText();

String command = cmd.getText();

String file2 = file.getText();

}

private static void Gui()

{

//Create and set up the window.

JFrame frame = new JFrame("RatClient2");

frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

RatClient2 app = new RatClient2();

Component contents = app.createComponents();

frame.getContentPane().add(contents);

//Display the window.

frame.pack();

frame.setVisible(true);

}

public static void main(String[] args)

{

javax.swing.SwingUtilities.invokeLater(new Runnable()

{

public void run()

{

Gui();

}

});

}

}

I hope this is okay i.e. I have tried my best to conform to the forum rules.

John4938a at 2007-7-14 22:39:20 > top of Java-index,Desktop,Core GUI APIs...
# 4

> Ok heres an SSCCE that shows the incorrect layout.

Better example. But it could be simplified even further. Your question is about laying out components. The code related to the ActionListener is not required. Keep the code as simple as possible.

> Ive tried using another panel also but to no avail.

I suggested using a separate panel with a different LayoutManager because I find the GridBagLayout too confusing and layouts, when planned in advance can generally be done with the other simpler LayoutManagers.

> I have tried my best to conform to the forum rules.

There are no forum rules about how to post code. These are my personal suggestions which I hope make sense. If you can't solve the problem by looking at executable code, don't expect us to be able to solve the problem just by looking at the code. The more information we have in front of us the better the chance we can help.

> At the moment its not showing the JEditorPane at all.

As I said I don't like using GridBagLayout so I rewrote the code completely showing the concept of using different panels with differenct layout managers. Whether you decide to use the code as is or not, hopefully I demonstrated how to break down the layout into smaller pieces, which each piece using its own layout manager. Read the Swing tutorial on [url http://java.sun.com/docs/books/tutorial/uiswing/layout/visual.html]How to Use Layout Managers[/url] to find the strengths and limitations of each layout manager. Then be creative.

public Component createComponents()

{

JPanel pane = new JPanel( new BorderLayout() );

JPanel west = new JPanel( new GridLayout(0, 1) );

//JPanel west = new JPanel();

//west.setLayout(new BoxLayout(west, BoxLayout.Y_AXIS));

JLabel hostLabel = new JLabel("Enter host: ");

west.add(hostLabel);

host = new JTextField("192.168.0.4");

west.add(host);

JLabel portLabel = new JLabel("Enter port: ");

west.add(portLabel);

port = new JTextField("80");

west.add(port);

JLabel cmdLabel = new JLabel("Send a command: ");

west.add(cmdLabel);

cmd = new JTextField("GetInfo");

west.add(cmd);

JLabel fileLabel = new JLabel("Send a file: ");

west.add(fileLabel);

file = new JTextField("C:\\a.jpg");

west.add(file);

JButton button = new JButton("Connect");

west.add(button);

jep = new JEditorPane();

scroll = new JScrollPane(jep);

scroll.setPreferredSize( new Dimension(300, 300) );

pane.add(west, BorderLayout.WEST);

pane.add(scroll, BorderLayout.CENTER);

return pane;

}

camickra at 2007-7-14 22:39:20 > top of Java-index,Desktop,Core GUI APIs...
# 5
Well thanks for the comprehesive help, new layout looks good.The only problem is the program no longer works.The button is used to submit the text in the textFields, but this button no longer works with the new layout.Please tell me why this is happening.
John4938a at 2007-7-14 22:39:20 > top of Java-index,Desktop,Core GUI APIs...
# 6
Dont worry just found out what it was:button.addActionListener(this);Cheers
John4938a at 2007-7-14 22:39:20 > top of Java-index,Desktop,Core GUI APIs...