aspose file tools*
The moose likes Swing / AWT / SWT and the fly likes Help!!!!!! GridBag Layout constraints Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Help!!!!!! GridBag Layout constraints" Watch "Help!!!!!! GridBag Layout constraints" New topic
Author

Help!!!!!! GridBag Layout constraints

Benny Finlay
Greenhorn

Joined: Apr 16, 2004
Posts: 1
I'm recieving an error of "Java.lang.IllegalArgumentException: cannot add to layout: constraints must be a GridBagConstraint" when trying to run a client/server style program, which is called by a method within a login class (which is a GUI interface). My issue is when the client is opened it gives me this error. and the client screen goes grey (the colour of the login screen, because i'm using a gridbaglayout for the initial login screen it seems to interfere with the client one.) the server screen on the other machine claims it's connected. but you can't see anything.
Also the intial client method is called as client("") but for this code to work fully i belive that this would need to have a replacable variable/ array or something in hear for further messages to be sent.
as you'll notice i'm quite new to Java.
any help would be much appreciated.
Heres the code:- these first two classes are within the same app on one machine
[CODE- Login/Client side - start]
//Client
import java.io.*;
import java.net.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.JOptionPane;
import java.util.*;
public class Client extends JFrame {
private JTextField enterField;
private JTextArea displayArea;
private ObjectOutputStream output;
private ObjectInputStream input;
private String message = "";
private String chatServer;
private Socket client;
public Client(String host) {
super("Network Monitoring Application - Client");
chatServer = host;
Container container = getContentPane();
enterField = new JTextField();
enterField.setEnabled(false);
enterField.addActionListener(
new ActionListener() {
public void actionPerformed(ActionEvent event) {
sendData(event.getActionCommand());
}
}
);
container.add(enterField, BorderLayout.NORTH);
displayArea = new JTextArea();
container.add(new JScrollPane(displayArea), BorderLayout.CENTER);
setSize(500, 400);
setVisible(true);
}
public void runClient() {
try {
connectToServer();
getStreams();
processConnection();
closeConnection();
}
catch (EOFException eofException) {
System.out.println("Server Terminated Connection");
}
catch (IOException ioException) {
ioException.printStackTrace();
}
}
private void getStreams() throws IOException {
output = new ObjectOutputStream(client.getOutputStream());
output.flush();
input = new ObjectInputStream(client.getInputStream());
displayArea.append("\n Got I/O streams\n");
}
private void connectToServer() throws IOException
{
displayArea.setText("Attempting to connect\n");
client = new Socket(InetAddress.getByName(chatServer), 6999);
displayArea.append("Connected to" + client.getInetAddress().getHostName());
}

private void processConnection() throws IOException
{
enterField.setEnabled(true);
do{
try {
message = (String) input.readObject();
displayArea.append("\n" + message);
displayArea.setCaretPosition(displayArea.getText().length());
}
catch (ClassNotFoundException classNotFoundException) {
displayArea.append("\nUnknown Object type received");
}
}while (!message.equals("SERVER terminate"));
}
private void closeConnection() throws IOException
{
displayArea.append("\nClosing Connection");
output.close();
input.close();
client.close();
}
private void sendData(String message){
try {
output.writeObject("CLIENT" + message);
output.flush();
displayArea.append("\nCLIENT" + message);
}
catch(IOException ioException) {
displayArea.append("\nError writing object");
}
}
}

//Login
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.io.*;
import java.net.*;
public class Login extends JFrame implements ActionListener {
private Container container;
private GridBagLayout layout;
private GridBagConstraints constraints;
private JLabel label1, label2, label3, label4;
private JPasswordField passwordField;
private JPasswordField passwordField2;
private static String OK = "ok";
private boolean clickmemode =true;
JButton jButton1 = new JButton();
public Login() { //Creating the GUI *****************
super("Network Monitoring Application");// this is writing on top of screen
container = getContentPane();
layout = new GridBagLayout();
container.setBackground(Color.lightGray);
container.setLayout(layout);
constraints = new GridBagConstraints();
//Constructing Label 1
label1 = new JLabel("label with text");
label1.setText("STOCKHOLM OPEN");
label1.setForeground(Color.blue);
label1.setFont(new Font("Times new Roman", Font.BOLD, 36));
//Constructing Label2
label2 = new JLabel("label with text");
label2.setText("USERNAME");
label2.setFont(new Font("Times new Roman", Font.BOLD, 30));
label2.setForeground(Color.black);
//Constructing Label3
label3 = new JLabel("label with text");
label3.setText("PASSWORD");
label3.setFont(new Font("Times new Roman", Font.BOLD, 30));
label3.setForeground(Color.black);
//Constructing Label4
label4 = new JLabel("label with text");
label4.setForeground(Color.blue);
label4.setText("Please Enter Username and Password above");
label4.setFont(new Font("Times new Roman", Font.PLAIN, 18));
//Constructing PasswordField
passwordField = new JPasswordField(20);
passwordField.setToolTipText("Enter Password");
passwordField.setEchoChar('*');
passwordField.setActionCommand(OK);
passwordField.addActionListener(this);
passwordField2 = new JPasswordField(20);
passwordField2.setToolTipText("Enter Username");
passwordField2.setEchoChar('*');
passwordField2.setActionCommand(OK);
passwordField2.addActionListener(this);
//Constructing The Submit Button
ButtonHandler Handler = new ButtonHandler();
JButton submitButton = new JButton("Submit");
submitButton.setActionCommand(OK);
submitButton.setFont(new Font("Times new Roman", Font.BOLD, 24));
submitButton.setBackground(Color.white);
submitButton.setForeground(Color.black);
submitButton.addActionListener(this);
submitButton.setToolTipText("Submit entry");

constraints.weightx = 1;
constraints.weighty = 1;
constraints.gridwidth = GridBagConstraints.REMAINDER;
addComponent(label1);
constraints.gridwidth = 1;
addComponent(label2);
constraints.gridwidth = GridBagConstraints.REMAINDER;
addComponent(passwordField2 );
constraints.gridwidth = GridBagConstraints.RELATIVE;
addComponent(label3);
constraints.gridwidth = GridBagConstraints.REMAINDER;
addComponent(passwordField);
constraints.gridwidth = GridBagConstraints.REMAINDER;
addComponent(submitButton);
constraints.gridwidth = GridBagConstraints.REMAINDER;
addComponent(label4);
setSize(1024, 768); //This sets the size of the GUI Itself
setVisible(true);//Making the GUI screen visibe
try {
jbInit();
}
catch(Exception e) {
e.printStackTrace();
}
}
private void addComponent(Component component) { //adds the components
layout.setConstraints(component, constraints);
container.add(component);
}
public void actionPerformed(ActionEvent e) {
String cmd = e.getActionCommand();

if (OK.equals(cmd)) { //Process the password.
char[] input1 = passwordField.getPassword();
char[] input2 = passwordField2.getPassword();
if (isPasswordCorrect(input1) && isUserNameCorrect(input2)) {
JOptionPane.showMessageDialog(null,"Login Accepted");
{ Client("");}
//THIS IS WHERE ALL THE METHODS FOR CHANGEOVER BETWEEN 2 DIFFERENT SCREENS ARE IMPLEMENTED ??? something needs doing here
}
else {
JOptionPane.showMessageDialog(null, "Invalid Username or Password\nMake sure caps lock is off",
"Error Message",JOptionPane.ERROR_MESSAGE);
}
//Zero out the possible password, for security.
for (int i = 0; i < input1.length; i++) {
input1[i] = 0;
}
passwordField.selectAll();
resetFocus();
}
else {
JOptionPane.showMessageDialog(null,
"Enter valid name\n");
}
}

public void Client(String host){
Client application01;

Client application;
if(host.length() == 0)
application = new Client("192.168.0.2");
else
application =new Client(host);
application.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
application.runClient(); }
private static boolean isUserNameCorrect(char[] input) {
boolean isCorrect = true;
char[] correctUserName = {
'a', 'd', 'm', 'i', 'n'};
if (input.length != correctUserName.length) {
isCorrect = false;
}
else {
for (int i = 0; i < input.length; i++) {
if (input[i] != correctUserName [i]) {
isCorrect = false;
}
}
}
//Zero out the password.
for (int i = 0; i < correctUserName.length; i++) {
correctUserName [i] = 0;
}
return isCorrect;
}
private static boolean isPasswordCorrect(char[] input) {
boolean isCorrect = true;
char[] correctPassword = {
'j', 'a', 'v', 'a'};
if (input.length != correctPassword.length) {
isCorrect = false;
}
else {
for (int i = 0; i < input.length; i++) {
if (input[i] != correctPassword[i]) {
isCorrect = false;
}
}
}
//Zero out the password.
for (int i = 0; i < correctPassword.length; i++) {
correctPassword[i] = 0;
}
return isCorrect;
}
protected void resetFocus() {
passwordField.requestFocusInWindow();
}
private class ButtonHandler implements ActionListener{//handling the button event
public void actionPerformed(ActionEvent event){
//JOptionPane.showMessageDialog(null, "Submit what\n" +event.getActionCommand());
}
}//end of class button handler

public static void main(String[] args)//main
{
Login application = new Login();
application.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
private void jbInit() throws Exception {
jButton1.setText("jButton1");
this.getContentPane().add(jButton1, BorderLayout.CENTER);
}///end of main
}//end of class
[End of Client/Login Code]

[Code for Server]
/**
* <p>Title: </p>
* <p>Description: </p>
* <p>Copyright: Copyright (c) 2004</p>
* <p>Company: </p>
* @author not attributable
* @version 1.0
*/
import java.io.*;
import java.net.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class Server extends JFrame {
private JTextField enterField;
private JTextArea displayArea;
private ObjectOutputStream output;
private ObjectInputStream input;
private ServerSocket server;
private Socket connection;
private int counter =1;
public Server()//creating the text area
{
super("Network Monitoring Application - Server");
Container container = getContentPane();
enterField = new JTextField();
enterField.setEnabled(false);
enterField.addActionListener(
new ActionListener() {
public void actionPerformed(ActionEvent event) {
sendData(event.getActionCommand());
}
}
);
container.add(enterField, BorderLayout.NORTH);
displayArea = new JTextArea();
container.add(new JScrollPane(displayArea),BorderLayout.CENTER);
setSize(500,400);
setVisible(true);
}
public void runServer()
{
try{
server = new ServerSocket(6999,100);
while(true){
waitForConnection();
getStreams();
processConnection();
closeConnection();
++counter;
}
}
catch(EOFException eofException){
System.out.println("Client Terminated Connection");
}
catch(IOException ioException){
ioException.printStackTrace();
}
}

private void waitForConnection() throws IOException
{
displayArea.setText("waiting for connection\n");
connection = server.accept();
displayArea.append("Connection" + counter + "received from" +
connection.getInetAddress().getHostName());
}
private void getStreams() throws IOException
{
output = new ObjectOutputStream(connection.getOutputStream());
output.flush();
input = new ObjectInputStream(connection.getInputStream());
displayArea.append("\n Got I/O streams\n");
}
private void processConnection() throws IOException
{
String message = "SERVER>>> Connection successful";
output.writeObject(message);
output.flush();
enterField.setEnabled(true);
do{
try {
message = (String) input.readObject();
displayArea.append("\n" + message);
displayArea.setCaretPosition(displayArea.getText().length());
}
catch (ClassNotFoundException classNotFoundException) {
displayArea.append("\nUnknown Object type received");
}
}while (!message.equals("client terminate"));
}
private void closeConnection() throws IOException
{
displayArea.append("\nUser terminated Connection");
enterField.setEnabled(false);
output.close();
input.close();
connection.close();
}
private void sendData(String message){
try {
output.writeObject("SERVER>>>" + message);
output.flush();
displayArea.append("\nSERVER>>>" + message);
}
catch(IOException ioException) {
displayArea.append("\nError writing object");
}
}

public static void main(String[] args) {
Server application = new Server();
application .setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
application.runServer();
}
}
[End of Server Code]
Any ideas
Nathaniel Stoddard
Ranch Hand

Joined: May 29, 2003
Posts: 1258
Benny,
You may want to edit your post using the CODE tags so people can copy it and help you out more readily. Also, how about a stack trace that gives all the details for your exception?
One thing I do notice is that you never set the gridx and gridy values for your constraints object.
[ April 16, 2004: Message edited by: Nathaniel Stoddard ]

Nathaniel Stodard<br />SCJP, SCJD, SCWCD, SCBCD, SCDJWS, ICAD, ICSD, ICED
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Help!!!!!! GridBag Layout constraints
 
Similar Threads
can't get server to send file to client
client and server network
revised - client server problem
URGENT!! chat applet
can't get server to send file to client