无法在Jtable上显示数据

时间:2018-02-27 04:18:19

标签: java sockets

我正在尝试建立一个客户端服务器架构,客户端将其硬件级别规范发送到服务器。但我无法在GUI上显示数据。客户端信息在服务器端正确接收。

服务器代码

var query = source.OfType<DataModel>().Where(item => item.Value1 == 1);
var query1 = source.OfType<IData>().Where(item => item.Value1 == TypeInEnum.A);

显示客户信息的GUI代码

public class MyServer extends JFrame {
public void Show() throws IOException
{
    ServerSocket ss=new ServerSocket(6666);
    int count=0;
        while(true)
        {
            Socket s=null;
             try{ 
            s=ss.accept();
            SocketThread socketThread=new SocketThread(s,count);
            socketThread.start();

            }catch(Exception e)
            {
                s.close();
            System.out.println(e);
            }
            finally {
                count++;                
            }
        }
}
}
class SocketThread extends Thread
{
      // variables decleration

      public SocketThread(Socket csocket,int count) {
      this.csocket = csocket;
      this.count=count;
      }
      public void run(){
        try { 
            DataInputStream dis=new DataInputStream(csocket.getInputStream());
            // Assigning the value to the variables
            csocket.close();
            dis.close();
            ShowTable st=new ShowTable();
            st.showdata(count,host, ip, os_name, os_arch, os_version, pro_detail, Mac_add, disk_size, max_memory);
            }
            catch(Exception e)
            {
            System.out.println("Server Problem");
            System.out.println(e);
            }
      }
}

但是这个Jframe代码显示一个空白框架所以任何人都可以帮助我,因为它在控制台上显示输出错误。

1 个答案:

答案 0 :(得分:1)

  

谢谢主席先生,如果可能的话,请你提供代码,因为我无法做到这一点

基本思想是,您只希望JFrame的一个实例存在,SocketThread的所有实例都应该引用此单个实例。

虽然有很多方法可以实现这一点,但使用单例模式可能是最简单的方法之一

ShowTable

import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;

public enum ShowTable  {

    INSTANCE;

    private JFrame f = new JFrame();
    private JTable jt = new JTable(new DefaultTableModel());
    private DefaultTableModel model = (DefaultTableModel) jt.getModel();

    private ShowTable() {
        jt.setBounds(30, 40, 200, 300);
        jt.setFocusable(false);
        jt.setRowSelectionAllowed(false);
        JScrollPane sp = new JScrollPane(jt);
        f.add(sp);
        f.setSize(1300, 100);
    }

    public void showdata(int count, String host, String ip, String os_name, String os_arch,
            String os_version, String pro_detail, String Mac_add, float disk_size, float max_memory) {
        //System.out.println("Row  :"+count);
        Object data[] = {host, ip, os_name, os_arch, os_version, pro_detail, Mac_add, disk_size, max_memory};
        if (count == 0) {
            model.addColumn("HOSTNAME");
            model.addColumn("IP ADDRESS");
            model.addColumn("OS NAME");
            model.addColumn("OS ARCHITECTURE");
            model.addColumn("OS VERSION");
            model.addColumn("PROCESSOR DETAIL");
            model.addColumn("MAC ADDRESS");
            model.addColumn("HARD DISK");
            model.addColumn("RAM SIZE");
            model.addRow(data);
        } else {
            model.addRow(data);
        }
        f.setVisible(true);
    }
}

MyServer(和SocketThread

import java.awt.EventQueue;     import java.io.DataInputStream;     import java.io.IOException;     import java.net.ServerSocket;     import java.net.Socket;

public class MyServer {

    public static void main(String[] args) throws IOException {
        new MyServer().Show();
    }

    public void Show() throws IOException {
        ServerSocket ss = new ServerSocket(6666);
        int count = 0;
        while (true) {
            Socket s = null;
            try {
                s = ss.accept();
                SocketThread socketThread = new SocketThread(s, count);
                socketThread.start();

            } catch (Exception e) {
                s.close();
                System.out.println(e);
            } finally {
                count++;
            }
        }
    }

    class SocketThread extends Thread {

        Socket csocket;
        String ip = null;
        String host = null;
        String os_name = null;
        String os_arch = null;
        String os_version = null;
        String pro_detail = null;
        String Mac_add = null;
        float disk_size = 0;
        float max_memory = 0;
        int count;

        public SocketThread(Socket csocket, int count) {
            this.csocket = csocket;
            this.count = count;
        }

        public void run() {
            try {
                DataInputStream dis = new DataInputStream(csocket.getInputStream());
                host = (String) dis.readUTF();
                ip = (String) dis.readUTF();
                os_name = (String) dis.readUTF();
                os_arch = (String) dis.readUTF();
                os_version = (String) dis.readUTF();
//                pro_detail = (String) dis.readUTF();
                Mac_add = (String) dis.readUTF();
                disk_size = (long) (dis.readLong() / (1000000000));
                max_memory = (long) (dis.readLong() / (1000000000));
                csocket.close();
                dis.close();
                EventQueue.invokeLater(new Runnable() {
                    @Override
                    public void run() {
                        ShowTable.INSTANCE.showdata(count, host, ip, os_name, os_arch, os_version, pro_detail, Mac_add, disk_size, max_memory);
                    }
                });
            } catch (Exception e) {
                System.out.println("Server Problem");
                System.out.println(e);
            }
        }
    }
}

MyClient

import java.io.DataOutputStream;     import java.io.File;     import java.lang.management.ManagementFactory;     import java.net.InetAddress;     import java.net.NetworkInterface;     import java.net.Socket;

public class MyClient {

    public static void main(String[] args) {

        InetAddress ip;
        try {
            long diskSize = new File("C:").getTotalSpace() + new File("D:").getTotalSpace() + new File("E:").getTotalSpace();
            long memorySize = ((com.sun.management.OperatingSystemMXBean) ManagementFactory.getOperatingSystemMXBean()).getTotalPhysicalMemorySize();
            ip = InetAddress.getLocalHost();
            Socket s = new Socket("localhost", 6666);
            DataOutputStream dout = new DataOutputStream(s.getOutputStream());
            dout.writeUTF(ip.getHostName());
            dout.writeUTF(ip.getHostAddress());
            dout.writeUTF(System.getProperty("os.name"));
            dout.writeUTF(System.getProperty("os.arch"));
            dout.writeUTF(System.getProperty("os.version"));
//            dout.writeUTF(System.getenv("processor_identifier"));
            NetworkInterface network = NetworkInterface.getByInetAddress(ip);
            StringBuilder sb = new StringBuilder();
            byte[] mac = network.getHardwareAddress();
            for (int i = 0; i < mac.length; i++) {
                sb.append(String.format("%02X%s", mac[i], (i < mac.length - 1) ? "-" : ""));
            }
            dout.writeUTF(sb.toString());
            dout.writeLong(diskSize);
            dout.writeLong(memorySize);
            dout.flush();
            dout.close();
            s.close();
        } catch (Exception ex) {
            System.out.println("Client");
            System.out.println(ex);
        }
    }
}