0

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);        

    }
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
miatech
  • 2,150
  • 8
  • 41
  • 78
  • *according to oracle docs when using absolute positioning* - you can't use absolute positioning. Components don't change size as the size of the frame changes. This is why you MUST use layout managers. We can't tell you what layout manager to use because don't know what your final layout should look like. *that link doesn't give you too much information* - the link gives all kinds of information. It contains working examples of each layout manager you can download and test. You can also next panels with different layout man agers to achieve you desired effect. – camickr Oct 07 '19 at 21:03

1 Answers1

0

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.

George Z.
  • 6,643
  • 4
  • 27
  • 47
  • that link doesn't give you too much information. I removed setLayout(null) and it can't render my gui when I run the app. – miatech Oct 07 '19 at 15:38
  • @miatech The link gives you basic information about each `LayoutManager`. In order to learn more about each one of them, you will have to visit its "sub-link". Of course you cannot render your GUI since the `setBounds` are ignored (LayoutManager will take care of them). – George Z. Oct 07 '19 at 15:42
  • according to oracle docs when using absolute positioning (absolutelayout) should be null – miatech Oct 07 '19 at 15:43
  • @miatech Plus it will probably need to use combinations of `LayoutManager`s and not just one. (GridBagLayout) can handle it. But as a beginner to it, i would not recommend it. Start with nested JPanels. (always a personal opinion) – George Z. Oct 07 '19 at 15:43
  • @miatech https://docs.oracle.com/javase/tutorial/uiswing/layout/none.html – George Z. Oct 07 '19 at 15:44