Syed Muhammad Mubashir
Syed Muhammad Mubashir

Reputation: 1016

How to ReSize All Controls Of Application With Different Resolutions

I am using Swing Application. I have build Many forms in the Resolution (1280*1024). When i am deploying the application in the resolution the forms are clipping in the resolution (1024*768) and other smaller resolutions. I have tried the MIGLayOutManager! and GridBagLayout! as these are preferable if you are developing the application from scratch but they do not work for me. I want to resize my child controls of the frame using the Example!

Below is the code which i used for GridBagLayOut on the edit of Answer by @Guillaume Polet

        try {
    Utility util = new Utility(null);
    Component[] com = null;
    java.util.List<Component> jComp = new ArrayList<Component>();
    if (obj instanceof JPanel) {
        JPanel panel = (JPanel) obj;
        com = panel.getComponents();
    } else if (obj instanceof JFrame) {
        JFrame JFrm = (JFrame) obj;

        jComp = util.harvestAllComp(JFrm);

    } else if (obj instanceof JFrame) {

        JFrame frm = (JFrame) obj;
        //  frm.cp = cp;
        String dbName = "GoldNew";
        util = new Utility(dbName);
        DBEngine db = new DBEngine(dbName);

        for (int a = 0; a < jComp.size(); a++) {
            if (jComp.get(a) instanceof JLabel) {

                JLabel label = (JLabel) com[a];
            } else if (jComp.get(a) instanceof JTextField) {
                JTextField jtxt = (JTextField) jComp.get(a);
                jtxt.setEditable(boolEnable);

                // Do Nothing 
            } else if (jComp.get(a) instanceof JTextArea) {
                JTextArea jtxt = (JTextArea) jComp.get(a);
                jtxt.setEditable(boolEnable);

            } else if (jComp.get(a) instanceof JXDatePicker) {
                JXDatePicker jdate = (JXDatePicker) jComp.get(a);
                jdate.setEditable(boolEnable);
                jdate.getEditor().setEditable(false);

                // Do Nothing 
            } else if (jComp.get(a) instanceof JTabbedPane) {
                JTabbedPane tabPane = (JTabbedPane) jComp.get(a);
                Component[] comTab = tabPane.getComponents();
                tabPane.removeAll();
                GridBagLayout tabLayOut = new GridBagLayout();
                tabPane.setLayout(tabLayOut);
                addRowOfComponents(tabPane, comTab);

            } 
            if (jComp.get(a) instanceof JPanel) {
                JPanel panel2 = (JPanel) jComp.get(a);
                Component[] comTab = panel2.getComponents();
                panel2.removeAll();
                GridBagLayout tabLayOut = new GridBagLayout();
                panel2.setLayout(tabLayOut);
                addRowOfComponents(panel2, comTab);

            } else if (jComp.get(a) instanceof JTable) {

                final JTable tbl = (JTable) jComp.get(a);
                tbl.getTableHeader().setReorderingAllowed(false);
            } else if ((!(jComp.get(a) instanceof JTextField)) && (!(jComp.get(a) instanceof JXDatePicker))
                    && (!(jComp.get(a) instanceof JTextArea))
                    && (!(jComp.get(a) instanceof JLabel))) {
                // jComp.get(a).setEnabled(boolEnable);
                jComp.get(a).setEnabled(boolEnable);

            }
        }
    }
} catch (Exception ex) {
    ex.printStackTrace();
}

I want to use Component Size and Screen Size To Determine Aspect Ratio And Scale Component using Aspect Ration as in Example! using information below. I will be really thankful if any body guide how to achieve the task.

       Dimension dim = Toolkit.getDefaultToolkit().getScreenSize();
        frm.setBounds(0, 0, dim.width - 100, dim.height - 100);
        int w = frm.getSize().width;//Size Of Form
        int h = frm.getSize().height;
        int x = (dim.width - w) / 2;
        int y = (dim.height - h) / 2;

Upvotes: 0

Views: 1402

Answers (1)

Guillaume Polet
Guillaume Polet

Reputation: 47617

I think that you are incorrectly using LayoutManagers. Instead of setting them on the container, you set them on its content (although this is allowed, this is not done very often).

  1. Use a LayoutManager on a Container
  2. Optionally set constraints on its children component.

Here is a completely dumb example of using GridBagLayout which increases or decreases the size of all components according to the size of the frame:

import java.awt.Container;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.net.MalformedURLException;

import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTabbedPane;
import javax.swing.JTable;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.SwingUtilities;

public class TestGridBagLayout {

    protected void initUI() throws MalformedURLException {
        final JFrame frame = new JFrame();
        frame.setTitle(TestGridBagLayout.class.getSimpleName());
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        JPanel mainPanel = new JPanel(new GridBagLayout());
        JTable table = new JTable(new Object[][] { new Object[] { "Cell 1" }, new Object[] { "Cell 2" }, new Object[] { "Cell 3" } },
                new Object[] { "Header 1" });
        JList list = new JList(new Object[] { "Element 1", "Element 2", "Element 3", "Element 4" });
        JTextArea textArea = new JTextArea(8, 30);
        JTabbedPane tabbedPane = new JTabbedPane();
        tabbedPane.addTab("Tab 1", new JLabel("Tab 1"));
        tabbedPane.addTab("Tab 2", new JLabel("Tab 2"));
        tabbedPane.addTab("Tab 3", new JLabel("Tab 3"));
        addRowOfComponents(mainPanel, new JLabel("A label"), new JButton("A button"), new JTextField("A textfield", 24), new JComboBox(
                new Object[] { "Item 1", "Item 2", "Item 3", "Item 4", "Item 5" }));
        addRowOfComponents(mainPanel, new JScrollPane(table), new JScrollPane(list), new JScrollPane(textArea), tabbedPane);
        frame.add(mainPanel);
        frame.setSize(800, 500);
        frame.setVisible(true);
    }

    private void addRowOfComponents(Container parent, JComponent... children) {
        GridBagConstraints gbc = new GridBagConstraints();
        gbc.weightx = 1.0;
        gbc.weighty = 1.0;
        gbc.fill = GridBagConstraints.BOTH;
        gbc.insets = new Insets(3, 3, 3, 3);
        for (int i = 0; i < children.length; i++) {
            JComponent jComponent = children[i];
            if (i + 1 == children.length) {
                gbc.gridwidth = GridBagConstraints.REMAINDER;
            }
            parent.add(jComponent, gbc);
        }
        parent.revalidate();
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    new TestGridBagLayout().initUI();
                } catch (MalformedURLException e) {
                    e.printStackTrace();
                }
            }
        });
    }

}

Upvotes: 1

Related Questions