i wrote a Chat program and my drop down menu doesnt appear in the front...i used toolkit and Flow layouts to drow the GUI.. need help badly
thanks!!!!!!!
Cerberus 17 Junior Poster
If you're using Swing i don't think the layout matters because you apply the menu bar to the frame
e.g. frame.setJMenuBar(myMenu)
If that doesn't help perhaps you could post some of your code.
qwest21 0 Newbie Poster
If you're using Swing i don't think the layout matters because you apply the menu bar to the frame
e.g. frame.setJMenuBar(myMenu)
If that doesn't help perhaps you could post some of your code.
thanks for the help but that didnt work so here is my code fot the Client
import java.io.*;
import java.net.*;
import java.awt.*;
import java.awt.image.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.border.*;
import javax.swing.event.*;
//ChatClient code:
public class ChatClient implements Runnable, WindowListener, ActionListener //, ListSelectionListener
{
protected String host;
protected int port;
public TextArea output;
protected TextField input;
protected TextArea temp;
SketchFrame window;
String yourname;
static ChatHandler hobj;
GraphicsEnvironment e = GraphicsEnvironment.getLocalGraphicsEnvironment ();
public ChatClient ()
{
}
public ChatClient (String host, int port, SketchFrame window)
{
this.host = host;
this.port = port;
this.yourname = JOptionPane.showInputDialog ("Enter Login name:");
window.setSize (100, 500);
window.setBackground (Color.cyan);
output = new TextArea (15, 60);
output.setEditable (false);
temp = new TextArea (15, 60);
temp.setVisible (false);
input = new TextField (60);
input.setForeground (Color.blue);
output.setForeground (Color.blue);
input.addActionListener (this);
GridBagConstraints constraints = new GridBagConstraints ();
window.getContentPane ().setLayout (new FlowLayout (FlowLayout.CENTER));
//window.getContentPane().setLayout(new FlowLayout(FlowLayout.CENTER,10,10));
window.getContentPane ().setLayout (new BorderLayout (10, 10));
window.getContentPane ().add (temp, BorderLayout.SOUTH);
constraints.insets = new Insets (25, 25, 25, 25);
window.getContentPane ().add (output, BorderLayout.CENTER);
window.getContentPane ().add (input, BorderLayout.SOUTH);
window.pack ();
}
/*
public String Fun()
{
return (yourname);
}
*/
public TextArea func ()
{
return (output);
}
public Insets getInsets ()
{
return new Insets (25, 25, 25, 25);
}
protected DataInputStream dataIn;
protected DataOutputStream dataOut;
protected Thread listener;
public synchronized void start () throws IOException
{
if (listener == null)
{
Socket socket = new Socket (host, port);
try
{
dataIn = new DataInputStream
(new BufferedInputStream (socket.getInputStream ()));
dataOut = new DataOutputStream
(new BufferedOutputStream (socket.getOutputStream ()));
dataOut.writeUTF (yourname + " has logged on\n ");
//dataOut.flush ();
}
catch (IOException ex)
{
socket.close ();
throw ex;
}
listener = new Thread (this);
listener.start ();
window.setVisible (true);
}
}
public synchronized void stop () throws IOException
{
//window.setVisible (false);
if (listener != null)
{
listener.interrupt ();
listener = null;
dataOut.close ();
}
}
public void run ()
{
try
{
while (!Thread.interrupted ())
{
String line = dataIn.readUTF ();
output.append (line + "\n");
}
}
catch (IOException ex)
{
handleIOException (ex);
}
}
protected synchronized void handleIOException (IOException ex)
{
if (listener != null)
{
output.append (ex + "\n");
input.setVisible (false);
window.validate ();
if (listener != Thread.currentThread ())
listener.interrupt ();
listener = null;
try
{
dataOut.close ();
}
catch (IOException ignored)
{
}
}
}
public void windowOpened (WindowEvent event)
{
input.requestFocus ();
}
public void windowClosing (WindowEvent event)
{
try
{
stop ();
}
catch (IOException ex)
{
ex.printStackTrace ();
}
}
public void windowClosed (WindowEvent event)
{
}
public void windowIconified (WindowEvent event)
{
}
public void windowDeiconified (WindowEvent event)
{
}
public void windowActivated (WindowEvent event)
{
}
public void windowDeactivated (WindowEvent event)
{
}
public void actionPerformed (ActionEvent event)
{
try
{
input.selectAll ();
dataOut.writeUTF (yourname + " says:\n " + event.getActionCommand ());
dataOut.flush ();
}
catch (IOException ex)
{
handleIOException (ex);
}
}
} //End of chatClient code
here i also have another class that draws the GUI
import javax.swing.*;
import java.io.*;
import java.io.Writer.*;
import java.awt.*;
import java.awt.event.*;
import java.net.*;
import java.util.*;
import java.lang.*;
public class SketchFrame extends JFrame //implements ActionListener
{
ChatClient chatObj;
String logins="My nick";
String chat;
//Constructor
public SketchFrame(String title)
{
setTitle(title);
setDefaultCloseOperation(EXIT_ON_CLOSE);
setJMenuBar(menuBar);
//Main menu:
JMenu fileMenu = new JMenu("File");
exitAction = new FileAction("Exit", KeyStroke.getKeyStroke('E',Event.CTRL_MASK));
fileMenu.addSeparator();
addMenuItem(fileMenu,exitAction);
fileMenu.addSeparator();
//Initialize menu items:
menuBar.add(fileMenu);
enableEvents(AWTEvent.WINDOW_EVENT_MASK);
}
protected void processWindowEvent(WindowEvent e)
{
if (e.getID() == WindowEvent.WINDOW_CLOSING)
{
dispose();
System.exit(0);
}
super.processWindowEvent(e);
}
private JMenuItem addMenuItem(JMenu menu,Action action)
{
JMenuItem item = menu.add(action);
KeyStroke keyStroke = (KeyStroke)action.getValue("juh");//action.ACCELERATOR_KEY);
if(keyStroke != null)
item.setAccelerator(keyStroke);
return item;
}
//Main menu init:
private JMenuBar menuBar = new JMenuBar();
//File Menu Action:
class FileAction extends AbstractAction
{
public FileAction(String NAME,KeyStroke keyStroke)
{
super(NAME);
}
public void actionPerformed(ActionEvent e) //throws IOException
{
String name= (String)getValue(NAME);
if(name.equals(newAction.getValue(NAME)))
{
SketchFrame window2;
window2 = new SketchFrame("Instant Broadcast Messenger");
Toolkit theKit = window2.getToolkit();
Dimension wndSize = theKit.getScreenSize();
window2.setBounds(wndSize.width/4,wndSize.height/4,wndSize.width/2,wndSize.height/2);
window2.setVisible(true);
String newHost = JOptionPane.showInputDialog("Enter HostName:");
String newPort=JOptionPane.showInputDialog("Enter PortName:");
}
else
if(name.equals(saveAction.getValue(NAME)))
{
String file =JOptionPane.showInputDialog("Enter FileName:");
file=file+".txt";
}
else
if(name.equals(exitAction.getValue(NAME)))
{
dispose();
System.exit(0);
}
else
if(name.equals(loginAction.getValue(NAME)))
{
String logins = JOptionPane.showInputDialog("Enter Login name:");
ChatHandler handle=new ChatHandler(logins);
//handle.start();
}
}
}
//Background color Action
//Inner Class definition
class ColorAction extends AbstractAction
{
public ColorAction(String name,Color color)
{
super(name);
this.color=color;
}
public void actionPerformed(ActionEvent e)
{
elementColor = color;
getContentPane().setBackground(color);
}
private Color color;
}
//Font color Action
class FontColorAction extends AbstractAction
{
public FontColorAction(String name,Color color)
{
super(name);
this.color=color;
}
public void actionPerformed(ActionEvent e)
{
elementColor = color;
}
private Color color;
}
//Color Action object
private FileAction newAction, saveAction, exitAction,loginAction;
private Color elementColor;
private Font font;
void FontFunc(TextArea output)
{
Font f=new Font("SansSerif",Font.BOLD,18);
output.setFont(f);
// output.setForeground(Color.red);
}
}
//End of class SketchFrame
and this Class is usedmainly to call the previous class to the client class and connect to the server
import java.io.*;
import java.net.*;
import java.awt.*;
import java.awt.event.*;
public class Sketcher
{
static SketchFrame window;
public static void main (String[] args) throws IOException
{
//SketchFrame code:
window = new SketchFrame ("Instant Broadcast Messenger");
Toolkit theKit = window.getToolkit ();
Dimension wndSize = theKit.getScreenSize ();
window.setBounds (wndSize.width / 4, wndSize.height / 4, wndSize.width / 2, wndSize.height / 2);
window.setVisible (true);
if ((args.length != 1) || (args [0].indexOf (':') < 0))
throw new IllegalArgumentException ("192.168.5.40:99");
int idx = args [0].indexOf (':');
String host = args [0].substring (0, idx);
int port = Integer.parseInt (args [0].substring (idx + 1));
Sketcher sObj = new Sketcher ();
sObj.fun (host, port, window);
}
void fun (String host, int port, SketchFrame window) throws IOException
{
ChatClient client = new ChatClient (host, port, window);
client.start ();
}
} //End of Sketcher code
Edited by Reverend Jim because: Fixed formatting
Be a part of the DaniWeb community
We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.