Click here to Skip to main content
15,791,739 members
Please Sign up or sign in to vote.
2.50/5 (2 votes)
See more:
I have developed a frame in swing containing tabbedpane.
From that frame i move to next frame having JDeskTop Pane. Also i have created object of Socket and ServerSocket and keep it in listening mode.
I want both the frame visible and want to use both.

But whenever i am executing code system is getting hanged.
When i remove listening mode then it executed properly.
please help me..

My code is as follow
Java
import java.awt.*;
import javax.swing.*;

class TabbedPaneExample	extends	JFrame
{
	private	JTabbedPane tabbedPane;

	public TabbedPaneExample()
	{
		Container c=getContentPane();
		Toolkit toolkit =  Toolkit.getDefaultToolkit ();
		Dimension dim = toolkit.getScreenSize();
		setSize(dim.width,dim.height);
		setTitle("Network Monitoring System");
		setBackground(Color.gray );
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

		JPanel topPanel = new JPanel();
		topPanel.setLayout( new BorderLayout() );
		c.add(topPanel );
		
		tabbedPane = new JTabbedPane();
		tabbedPane.addTab( "Monitoring", new Monitor());
		topPanel.add( tabbedPane, BorderLayout.CENTER );

	}

	public static void main( String args[] )
	{
		TabbedPaneExample mainFrame	= new TabbedPaneExample();
		mainFrame.setVisible( true );
	}
}

Monitor Class..
Java
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;

class Monitor extends JPanel implements ActionListener
{
	JLabel welcome;
	JButton start,stop;
	JComboBox ipCombo;

	Monitor()
	{

		setLayout( null );

		Toolkit toolkit =  Toolkit.getDefaultToolkit ();
		Dimension dim = toolkit.getScreenSize();

		Font f=new Font("Verdana",Font.BOLD,20);

		welcome = new JLabel("Welcome To Remote Monotoring System");
		welcome.setBounds( dim.width/4, 30, 550, 80 );
		welcome.setFont(f);
		add(welcome);

		ipCombo=new JComboBox();
		ipCombo.addItem("192.168.1.16");
		ipCombo.addItem("second");
		ipCombo.addItem("third");
		ipCombo.setBounds(100,100,150,20);
		add(ipCombo);

		start=new JButton("Start");
		start.setBounds(100,140,80,30);
		add(start);
		start.addActionListener(this);

		stop=new JButton("Stop");
		stop.setBounds(250,140,80,30);
		add(stop);
		stop.addActionListener(this);

	}

	public void actionPerformed(ActionEvent ae)
	{
		if(ae.getSource()==start)
		{
			//dispose();
			new AddingInternalFramestoaJDesktopPane();
			System.out.println("Start...");
		}
		else if(ae.getSource()==stop)
			System.out.println("Stop...");
	}
}

Adding Internal Frames To Desktop Pane...

Java
import java.awt.BorderLayout;
import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
import javax.swing.JDesktopPane;
import javax.swing.JFrame;
import javax.swing.JInternalFrame;
import javax.swing.JLabel;
import java.awt.*;
import javax.swing.*;

public class AddingInternalFramestoaJDesktopPane {
	JFrame frame;
	JDesktopPane desktop;
	public AddingInternalFramestoaJDesktopPane()
	{
		String port = JOptionPane.showInputDialog("Please enter listening port");
		System.out.println(port);
        drawGUI();
        //new ConnectionInitiator(Integer.parseInt(port),desktop);
        initialize(Integer.parseInt(port));
	}
	void drawGUI()
	{
		frame = new JFrame();
		frame.setTitle("DeskTop Frame");
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

	    desktop = new JDesktopPane();
	    JInternalFrame internalFrame = new JInternalFrame("Can Do All", true, true, true, true);

	    desktop.add(internalFrame);

	    internalFrame.setBounds(25, 25, 200, 100);

	    JLabel label = new JLabel("Mahavir Demo", JLabel.CENTER);
	    internalFrame.add(label, BorderLayout.CENTER);

	    internalFrame.setVisible(true);

	    frame.add(desktop, BorderLayout.CENTER);
		Toolkit toolkit =  Toolkit.getDefaultToolkit ();
		Dimension dim = toolkit.getScreenSize();
		frame.setSize(dim.width,dim.height-30);

	    //frame.setSize(500, 300);
	   frame.setVisible(true);
	}

	public void initialize(int port){
		try {
	            ServerSocket sc = new ServerSocket(port);
	            //Show Server GUI
	            //drawGUI();
	            //Listen to server port and accept clients connections
	            while(true){
	                Socket client = sc.accept();
	                System.out.println("New client Connected to the server");
	                //Per each client create a ClientHandler
	               // new ClientHandler(client,desktop);
	            }
	        } catch (IOException ex) {
	            ex.printStackTrace();
	        }
    }
}
Posted
Updated 10-Sep-11 3:34am
v2
Comments
DaveAuld 10-Sep-11 8:35am    
Edit: fixed your formatting, you hand also managed to drop a set of pre tags into your code which was breaking the bottom section formatting :)
Mahavir Sancheti 10-Sep-11 8:43am    
i have done.
not much familiar about it.
thanks..

Use JDialog instead of JFrame and use setModalityType(Dialog.ModalityType type) [^] to keep it in front.

Other solution: call Window.toFront() when the Frame should appear. This might have blackouts.
 
Share this answer
 
Comments
Mahavir Sancheti 13-Sep-11 10:09am    
i used Window.toFront() but not meeting to my expectation.
about JDialog, i want to show JDesktop pane and main window. so cant use..
TorstenH. 13-Sep-11 12:47pm    
..and now, as a reward for a downvote you want me to help you further more?
I=B4m new to Swing and I made a subclass of JComponent. For now its a class that paints a image and listen to the mouse. My problem is: my class gets painted only when I minimize, maximize or something like that. Which method do I need call from constructor to get my class painted correctly? Thanks. ---

Java
import java.awt.Dimension; 
import java.awt.Graphics; 
import java.awt.Image; 
import java.awt.Toolkit; 
import java.awt.event.MouseEvent; 
import java.awt.event.MouseListener; 
import java.beans.PropertyChangeEvent; 
import java.beans.PropertyChangeListener; 
import javax.swing.JComponent
 
Share this answer
 
v2
Comments
TorstenH. 13-Sep-11 3:36am    
check this:
http://download.oracle.com/javase/tutorial/uiswing/components/jcomponent.html

calling the function layout() from the parental class Container should do the trick:
http://download.oracle.com/javase/1,5.0/docs/api/java/awt/Container.html#layout()

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900