简体   繁体   中英

How to Make JTable Resize Automatically When Window Maximize in Eclipse

working on a gui app in Eclipse using windowsbuilder plugin. The app works just fine. but the jTable won't resize when I drag the edges of the window. or when I maximize the app. There are other components on the window which I would like to automatically resize; however, the jtable is the one that is a must for me. when I maximize the window the table stays the same size. I'm using Absolute Layout for the layout manager. so positioning the elements on the window was just a matter of drag and drop. I have looked for the properties that allow me to set resize = true or something similar and I found autoRisizeMode. so I set it to true. that didn't work. any suggestions? Here's the code for my Gui initialize() method

private void initialize()  {        
        frame = new JFrame();
        frame.setAlwaysOnTop(true);
        frame.getContentPane().setBackground(new Color(240, 240, 240));
        frame.setBounds(100, 100, 882, 577);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.getContentPane().setLayout(null);

        JLabel lblIpRange = new JLabel("Ip Range");
        lblIpRange.setFont(new Font("Tahoma", Font.PLAIN, 14));
        lblIpRange.setBounds(10, 59, 55, 14);
        frame.getContentPane().add(lblIpRange);

        startIpLabel = new JTextField();
        startIpLabel.setBounds(75, 58, 100, 20);
        frame.getContentPane().add(startIpLabel);
        startIpLabel.setColumns(10);

        endIpLabel = new JTextField();
        endIpLabel.setBounds(198, 58, 100, 20);
        frame.getContentPane().add(endIpLabel);
        endIpLabel.setColumns(10);      
        String[] columns = {"Number", "Ip Address", "Hostname", "Mac"};             

        JButton scanButton = new JButton("Scan");
        scanButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {    
                try {
                    ipScan = new IpScanMain(startIpLabel.getText());
                    ipScan.startIpScanning();
                } catch (IOException e1) {
                    // TODO Auto-generated catch block
                    e1.printStackTrace();
                }
                allNodes = ipScan.getAllNodes();
                int rows = allNodes.size();
                data = new Object[rows][COLUMNS];
                int index = 0;
                for(Node node : allNodes) {
                    data[index][0] = index;
                    data[index][1] = node.getIp();
                    data[index][2] = node.getHostName();
                    data[index][3] = node.getMac();
                    index++;
                }
                table.setModel(new DefaultTableModel(data, columns));
            }


        });

        scanButton.setBounds(323, 57, 89, 23);
        frame.getContentPane().add(scanButton);

        JSeparator separator = new JSeparator();
        separator.setBounds(11, 106, 845, 2);
        frame.getContentPane().add(separator);

        JScrollPane scrollPane = new JScrollPane();
        scrollPane.setViewportBorder(new LineBorder(new Color(0, 0, 0)));
        scrollPane.setBounds(10, 119, 846, 410);
        frame.getContentPane().add(scrollPane);

        table = new JTable();
        table.setAutoResizeMode(JTable.AUTO_RESIZE_LAST_COLUMN);
        table.setFont(new Font("Tahoma", Font.PLAIN, 14));
        table.setBackground(Color.WHITE);
        table.setBorder(new LineBorder(new Color(0, 0, 0), 0));
        table.setCellSelectionEnabled(true);
        table.setFillsViewportHeight(true);
        table.setColumnSelectionAllowed(true);
        table.setModel(new DefaultTableModel(
            new Object[][] { },
            new String[] {
                "Number", "Ip Address", "Hostname", "Mac", "Ports"
            }
        ));
        table.getColumnModel().getColumn(0).setPreferredWidth(30);
        table.getColumnModel().getColumn(0).setMaxWidth(2147483619);
        table.getColumnModel().getColumn(3).setPreferredWidth(80);
        scrollPane.setColumnHeaderView(table);
        scrollPane.setViewportView(table);

        JMenuBar menuBar = new JMenuBar();
        menuBar.setBackground(new Color(240, 240, 240));
        menuBar.setBounds(10, 0, 849, 26);
        frame.getContentPane().add(menuBar);

        JMenu mnFile = new JMenu("File");
        menuBar.add(mnFile);

        JMenuItem mntmClose = new JMenuItem("Open File");
        mnFile.add(mntmClose);

        JSeparator separator_1 = new JSeparator();
        mnFile.add(separator_1);

        JMenuItem mntmSave = new JMenuItem("Save");
        mnFile.add(mntmSave);

        JMenuItem mntmSaveAs = new JMenuItem("Save As");
        mnFile.add(mntmSaveAs);

        JSeparator separator_2 = new JSeparator();
        mnFile.add(separator_2);

        JMenuItem mntmPrint = new JMenuItem("Print...");
        mnFile.add(mntmPrint);

        JSeparator separator_3 = new JSeparator();
        mnFile.add(separator_3);

        JMenuItem mntmProperties = new JMenuItem("Settings");
        mnFile.add(mntmProperties);

        JSeparator separator_4 = new JSeparator();
        mnFile.add(separator_4);

        JMenuItem mntmClose_1 = new JMenuItem("Exit");
        mnFile.add(mntmClose_1);

        JMenu mnEdit = new JMenu("Edit");
        menuBar.add(mnEdit);

        JMenuItem mntmCopy = new JMenuItem("Copy");
        mnEdit.add(mntmCopy);

        JMenuItem mntmPaste = new JMenuItem("Paste");
        mnEdit.add(mntmPaste);

        JMenu mnHelp = new JMenu("Help");
        menuBar.add(mnHelp);

        JMenuItem mntmGuide = new JMenuItem("Guide");
        mnHelp.add(mntmGuide);

        JMenuItem mntmAbout = new JMenuItem("About");
        mnHelp.add(mntmAbout);

        JPanel panel = new JPanel();
        panel.setBorder(new LineBorder(new Color(0, 0, 0)));
        panel.setBounds(574, 37, 281, 58);
        frame.getContentPane().add(panel);
        panel.setLayout(null);

        JLabel lblNewLabel = new JLabel("IPv4");
        lblNewLabel.setFont(new Font("Tahoma", Font.BOLD, 14));
        lblNewLabel.setBounds(10, 11, 46, 14);
        panel.add(lblNewLabel);

        JLabel lblNewLabel_1 = new JLabel("IPv6");
        lblNewLabel_1.setFont(new Font("Tahoma", Font.BOLD, 14));
        lblNewLabel_1.setBounds(10, 36, 46, 14);
        panel.add(lblNewLabel_1);

        JLabel ipv4Label = new JLabel("192.168.1.10/24");
        ipv4Label.setFont(new Font("Tahoma", Font.PLAIN, 14));
        ipv4Label.setBounds(53, 11, 123, 14);
        panel.add(ipv4Label);

        JLabel ipv6Label = new JLabel("ff80:ee3d:ff35:abdd:3dce:335d");
        ipv6Label.setFont(new Font("Tahoma", Font.PLAIN, 14));
        ipv6Label.setBounds(53, 36, 196, 14);
        panel.add(ipv6Label);

        JComboBox comboBox = new JComboBox();       
        comboBox.setBounds(446, 57, 100, 22);
        frame.getContentPane().add(comboBox);

        System.out.println(Thread.currentThread().getName());

        //combobox network interface stuff
        NetInterface netint = null;
        try {
            netint = new NetInterface(frame, comboBox);
        } catch (SocketException | UnknownHostException e1) {
            e1.printStackTrace();
        }
        EventQueue.invokeLater(netint);        

    }

The reason your JTable is not resized after frame gets resized, is because you getContentPane.setLayout(null) . You should use the proper LayoutManager and let it do the work for you. Setting absolute layout ( null ) will give you hard time since you have to set the coordinates and the dimensions of each component manually. Plus, as you are already facing, JFrame cannot be resizable or work in a screen with other resolution, which is totally unfriendly.

Another post where it explains more and why absolute positioning is bad in Swing is this one.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM