Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

Welcome to the new platform of Programmer's Heaven! We apologize for the inconvenience caused, if you visited us from a broken link of the previous version. The main reason to move to a new platform is to provide more effective and collaborative experience to you all. Please feel free to experience the new platform and use its exciting features. Contact us for any issue that you need to get clarified. We are more than happy to help you.

Synchonize frame size with screen resolution

GyronMkweboGyronMkwebo Posts: 224Member ✭✭
Hie, i am new to java and want to design a GUI for mySQL database. How do i synchronize the frame size with the current screen resolution? I would need to make sure that the forms (actually frames), come up with the right size to go hand in hand with the current resolution settings on the screen.
Please help.

Comments

  • pahpah Posts: 83Member
    : Hie, i am new to java and want to design a GUI for mySQL database. How do i synchronize the frame size with the current screen resolution? I would need to make sure that the forms (actually frames), come up with the right size to go hand in hand with the current resolution settings on the screen.
    : Please help.
    :
    you can get the screensize with the Methode getScreenSize of the Class
    Toolkit....

    lets say you have a normal frame ....... in the constructor you can
    set the setSize of the frame as follows

    [code]
    public class NewJFrame extends javax.swing.JFrame{

    public NewJFrame(){
    initComponents();
    [red]this.setSize(this.getToolkit().getScreenSize());[/red]
    }

    public static void main(String args[]){
    java.awt.EventQueue.invokeLater(new Runnable(){
    public void run(){
    new JFrame().setVisible(true);
    }
    }};
    }

    private void initComponents(){
    //set teh default close operation etc here....
    //and the layoutmanager.........
    //if you have a good ide it does it all for you ;-)
    }
    }

    [/code]
  • GyronMkweboGyronMkwebo Posts: 224Member ✭✭
    : : Hie, i am new to java and want to design a GUI for mySQL database. How do i synchronize the frame size with the current screen resolution? I would need to make sure that the forms (actually frames), come up with the right size to go hand in hand with the current resolution settings on the screen.
    : : Please help.
    : :
    : you can get the screensize with the Methode getScreenSize of the Class
    : Toolkit....
    :
    : lets say you have a normal frame ....... in the constructor you can
    : set the setSize of the frame as follows
    :
    : [code]
    : public class NewJFrame extends javax.swing.JFrame{
    :
    : public NewJFrame(){
    : initComponents();
    : [red]this.setSize(this.getToolkit().getScreenSize());[/red]
    : }
    :
    : public static void main(String args[]){
    : java.awt.EventQueue.invokeLater(new Runnable(){
    : public void run(){
    : new JFrame().setVisible(true);
    : }
    : }};
    : }
    :
    : private void initComponents(){
    : //set teh default close operation etc here....
    : //and the layoutmanager.........
    : //if you have a good ide it does it all for you ;-)
    : }
    : }
    :
    : [/code]
    : I was trying this
    [code]
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    import javax.swing.JFrame;;

    public class FrmMainForm extends JMenuBar {

    String[] fileItems = new String[] { "New", "Set User Privileges", "Log in as different user", "Exit" };
    String[] editItems = new String[] { "Undo", "Cut", "Copy", "Paste" };
    char[] fileShortcuts = { 'N','O','S','X' };
    char[] editShortcuts = { 'Z','X','C','V' };

    public FrmMainForm() {
    initComponents();

    }

    public void DisplayForm() {
    JFrame frame = new JFrame("Powerspeed Electrical: - Pricing system");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setJMenuBar(new FrmMainForm());
    Dimension screenDim=Toolkit.getDefaultToolkit().getScreenSize();
    frame.setBounds(0, 0, screenDim.height, screenDim.width);

    frame.setVisible(true);
    }
    public void initComponents(){
    JMenu fileMenu = new JMenu("File");
    JMenu editMenu = new JMenu("Edit");



    // Assemble the File menus with mnemonics
    ActionListener printListener = new ActionListener() {
    public void actionPerformed(ActionEvent event) {
    System.out.println("Menu item [" + event.getActionCommand() +
    "] was pressed.");
    if (event.getActionCommand()=="Log in as different user"){
    FrmLogIn new_Log=new FrmLogIn();
    new_Log.DisplayForm();
    }
    }
    };
    for (int i=0; i < fileItems.length; i++) {
    JMenuItem item = new JMenuItem(fileItems[i]);
    item.addActionListener(printListener);
    fileMenu.add(item);
    }

    // Assemble the File menus with keyboard accelerators
    for (int i=0; i < editItems.length; i++) {
    JMenuItem item = new JMenuItem(editItems[i]);
    item.setAccelerator(KeyStroke.getKeyStroke(editShortcuts[i],
    Toolkit.getDefaultToolkit().getMenuShortcutKeyMask(), false));
    item.addActionListener(printListener);
    editMenu.add(item);
    }

    // Insert a separator in the Edit Menu in Position 1 after "Undo"
    editMenu.insertSeparator(1);
    //insert seperators for the file menu
    fileMenu.insertSeparator(1);
    fileMenu.insertSeparator(4);

    // Finally, add all the menus to the menu bar
    add(fileMenu);
    add(editMenu);

    }
    }[/code]
    the public function DispalyForm is invoked from other classes and this brings up the form onto the screen, but it still doesn't fill the screen.
    I tried your idea and the frame came on the screen, even smaller, in fact, so small that i could hardly see it! What do you think is the problem?
  • pahpah Posts: 83Member
    : : I was trying this
    : [code]
    : import java.awt.*;
    : import java.awt.event.*;
    : import javax.swing.*;
    : import javax.swing.JFrame;;
    :
    : public class FrmMainForm extends JMenuBar {
    :
    : String[] fileItems = new String[] { "New", "Set User Privileges", "Log in as different user", "Exit" };
    : String[] editItems = new String[] { "Undo", "Cut", "Copy", "Paste" };
    : char[] fileShortcuts = { 'N','O','S','X' };
    : char[] editShortcuts = { 'Z','X','C','V' };
    :
    : public FrmMainForm() {
    : initComponents();
    :
    : }
    :
    : public void DisplayForm() {
    : JFrame frame = new JFrame("Powerspeed Electrical: - Pricing system");
    : frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    : frame.setJMenuBar(new FrmMainForm());
    : Dimension screenDim=Toolkit.getDefaultToolkit().getScreenSize();
    : frame.setBounds(0, 0, [red]screenDim.height, screenDim.width) you have replaced the two parameters first width and then the height!;[/red]
    [blue] the constuctor is setBounds(int x,int y, int width,int height)[/blue]
    :

    : frame.setVisible(true);
    : }
    : public void initComponents(){
    : JMenu fileMenu = new JMenu("File");
    : JMenu editMenu = new JMenu("Edit");
    :
    :
    :
    : // Assemble the File menus with mnemonics
    : ActionListener printListener = new ActionListener() {
    : public void actionPerformed(ActionEvent event) {
    : System.out.println("Menu item [" + event.getActionCommand() +
    : "] was pressed.");
    : if (event.getActionCommand()=="Log in as different user"){
    : FrmLogIn new_Log=new FrmLogIn();
    : new_Log.DisplayForm();
    : }
    : }
    : };
    : for (int i=0; i < fileItems.length; i++) {
    : JMenuItem item = new JMenuItem(fileItems[i]);
    : item.addActionListener(printListener);
    : fileMenu.add(item);
    : }
    :
    : // Assemble the File menus with keyboard accelerators
    : for (int i=0; i < editItems.length; i++) {
    : JMenuItem item = new JMenuItem(editItems[i]);
    : item.setAccelerator(KeyStroke.getKeyStroke(editShortcuts[i],
    : Toolkit.getDefaultToolkit().getMenuShortcutKeyMask(), false));
    : item.addActionListener(printListener);
    : editMenu.add(item);
    : }
    :
    : // Insert a separator in the Edit Menu in Position 1 after "Undo"
    : editMenu.insertSeparator(1);
    : //insert seperators for the file menu
    : fileMenu.insertSeparator(1);
    : fileMenu.insertSeparator(4);
    :
    : // Finally, add all the menus to the menu bar
    : add(fileMenu);
    : add(editMenu);
    :
    : }
    : }[/code]
    : the public function DispalyForm is invoked from other classes and this brings up the form onto the screen, but it still doesn't fill the screen.
    : I tried your idea and the frame came on the screen, even smaller, in fact, so small that i could hardly see it! What do you think is the problem?
    :

    the first think which i see is the marked codesnap, try it with the the right variables. And the code looks on the first look little confusing for me. Check what your caller does with the frame!.
  • GyronMkweboGyronMkwebo Posts: 224Member ✭✭
    : : : I was trying this
    : : [code]
    : : import java.awt.*;
    : : import java.awt.event.*;
    : : import javax.swing.*;
    : : import javax.swing.JFrame;;
    : :
    : : public class FrmMainForm extends JMenuBar {
    : :
    : : String[] fileItems = new String[] { "New", "Set User Privileges", "Log in as different user", "Exit" };
    : : String[] editItems = new String[] { "Undo", "Cut", "Copy", "Paste" };
    : : char[] fileShortcuts = { 'N','O','S','X' };
    : : char[] editShortcuts = { 'Z','X','C','V' };
    : :
    : : public FrmMainForm() {
    : : initComponents();
    : :
    : : }
    : :
    : : public void DisplayForm() {
    : : JFrame frame = new JFrame("Powerspeed Electrical: - Pricing system");
    : : frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    : : frame.setJMenuBar(new FrmMainForm());
    : : Dimension screenDim=Toolkit.getDefaultToolkit().getScreenSize();
    : : frame.setBounds(0, 0, [red]screenDim.height, screenDim.width) you have replaced the two parameters first width and then the height!;[/red]
    : [blue] the constuctor is setBounds(int x,int y, int width,int height)[/blue]
    : :
    :
    : : frame.setVisible(true);
    : : }
    : : public void initComponents(){
    : : JMenu fileMenu = new JMenu("File");
    : : JMenu editMenu = new JMenu("Edit");
    : :
    : :
    : :
    : : // Assemble the File menus with mnemonics
    : : ActionListener printListener = new ActionListener() {
    : : public void actionPerformed(ActionEvent event) {
    : : System.out.println("Menu item [" + event.getActionCommand() +
    : : "] was pressed.");
    : : if (event.getActionCommand()=="Log in as different user"){
    : : FrmLogIn new_Log=new FrmLogIn();
    : : new_Log.DisplayForm();
    : : }
    : : }
    : : };
    : : for (int i=0; i < fileItems.length; i++) {
    : : JMenuItem item = new JMenuItem(fileItems[i]);
    : : item.addActionListener(printListener);
    : : fileMenu.add(item);
    : : }
    : :
    : : // Assemble the File menus with keyboard accelerators
    : : for (int i=0; i < editItems.length; i++) {
    : : JMenuItem item = new JMenuItem(editItems[i]);
    : : item.setAccelerator(KeyStroke.getKeyStroke(editShortcuts[i],
    : : Toolkit.getDefaultToolkit().getMenuShortcutKeyMask(), false));
    : : item.addActionListener(printListener);
    : : editMenu.add(item);
    : : }
    : :
    : : // Insert a separator in the Edit Menu in Position 1 after "Undo"
    : : editMenu.insertSeparator(1);
    : : //insert seperators for the file menu
    : : fileMenu.insertSeparator(1);
    : : fileMenu.insertSeparator(4);
    : :
    : : // Finally, add all the menus to the menu bar
    : : add(fileMenu);
    : : add(editMenu);
    : :
    : : }
    : : }[/code]
    : : the public function DispalyForm is invoked from other classes and this brings up the form onto the screen, but it still doesn't fill the screen.
    : : I tried your idea and the frame came on the screen, even smaller, in fact, so small that i could hardly see it! What do you think is the problem?
    : :
    :
    : the first think which i see is the marked codesnap, try it with the the right variables. And the code looks on the first look little confusing for me. Check what your caller does with the frame!.
    :

    Yeah, it works, the 'form' gets 'Maximized'. Another question (sorry to nag) is: After i pop up a form (frame) and i want to force users to respond to the form (sort of locking focus onto the form), how would i do that?
  • pahpah Posts: 83Member


    : Yeah, it works, the 'form' gets 'Maximized'. Another question (sorry to nag) is: After i pop up a form (frame) and i want to force users to respond to the form (sort of locking focus onto the form), how would i do that?
    :

    you can use the requestFocus() methode of the frame object should do this.
Sign In or Register to comment.