-1

I am trying to run my application in full screen size. It contains a frame fully covered by panel. I have tried this code

Toolkit tk=this.getDefaultToolkit();
this.setSize(tk.getScreenSize.getWidth(),tk.getScreenSize.getHeight());

I also tried MAXIMIZED_BOTH in setExtendedState. That too doesn't work. I think the problem is with jPanel but not sure. Its only half visible in small screen sizes but the Frame is acquiring full size in all screen sizes. I have enabled autoresizing for the panel. Here the images of application in larger screen and in smaller screen biggerscreen

and smaller screen

Previnkumar
  • 107
  • 3
  • 13
  • Possible duplicate of [How to make a JFrame really fullscreen?](https://stackoverflow.com/questions/10055436/how-to-make-a-jframe-really-fullscreen) – ItamarG3 Jun 27 '17 at 06:03
  • The core is is going to centre around how the components preferred size is been calculated and what layout managers you are using. `this.setSize(tk.getScreenSize.getWidth(),tk.getScreenSize.getHeight());` is defiantly NOT the right path, as it does not take into consideration things like the task bar – MadProgrammer Jun 27 '17 at 06:09
  • yeah Thanks for reply @ItamarGreen . In all screen sizes frame acquires full screen. But the panel(blue in picture) is not adopting the frame size. – Previnkumar Jun 27 '17 at 06:10
  • @MadProgrammer Thanks. I m using netbeans. its free design layout. I have also tried border layout. Can u please suggest me how should i solve this? – Previnkumar Jun 27 '17 at 06:13
  • Use a combination of layouts. Right now, I just see `BorderLayout`, `GridLayout` and `GridBagLayout` been used – MadProgrammer Jun 27 '17 at 06:23
  • Sry, i didn't use grid layout and grid bag layout. Instead i tried setBound(0,0,w,h). That too doesn't shown up @MadProgrammer – Previnkumar Jun 27 '17 at 06:27
  • Layout managers provide a greater flexibility and ability to respond to difference in graphical environments (far beyond "manual" layouts), which is why, as a general rule, they are so highly recommended. I would suggest you have a look at [Laying Out Components Within a Container](https://docs.oracle.com/javase/tutorial/uiswing/layout/index.html) for more details – MadProgrammer Jun 27 '17 at 06:30
  • Thanks @MadProgrammer . Will using layouts allow autoResizing of Components?. In Grid bag layout components are set in fixed size and aligned to centre. None resized or extended to fit screen size – Previnkumar Jun 27 '17 at 06:54
  • @previn They are if you set it up right – MadProgrammer Jun 27 '17 at 06:59
  • Sry for another comment @MadProgrammer . Gone through the [Laying out components within a container](https://docs.oracle.com/javase/tutorial/uiswing/layout/index.html) , Still not get the answer. Will u please tell what layout should i use and how to use it – Previnkumar Jun 27 '17 at 08:00
  • I am tempted to downvote. Your question is not clear at all. What does "full screen" means? Usually it means "I only want to see my application's content and nothing else". I understand that this isn't what you want. Second, provide some _relevant_ code. What have you done to layout the components especially the buttons that aren't visible? – Stelios Adamantidis Jun 27 '17 at 12:50
  • @SteliosAdamantidis Please sorry about that. To the buttons in the top(home, view all, db settings and quit) i have enabled `autoresizing` horizontally. And to the buttons in left(add students, students list, semester, manage groups, settings, help and quit) i have given `autoresizing` vertically. For `panel`(blue color) and the center `panel`(i.e in card layout) i have set `autoresizing` both horizontally and vertically. – Previnkumar Jun 27 '17 at 13:30
  • No need to be sorry about anything, you might though want to consider editing your question to add some of the code that you have written. – Stelios Adamantidis Jun 27 '17 at 13:34
  • yeah @SteliosAdamantidis but code for these buttons and panels will be too large and also i am using netbeans. Here mostly it is given in properties – Previnkumar Jun 27 '17 at 13:35

2 Answers2

1

The proper use of a variety of layout managers can greatly increase the ability for a UI to be dynamically sized

Based on the screen shots in your question, I see a BorderLayout, GridLayout and possibly a GridBagLayout as the primary choices

The window "packed"

Packed

The window "maximised"

maximised

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.GridLayout;
import java.awt.Insets;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextField;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import javax.swing.border.LineBorder;

public class JavaApplication170 {

    public static void main(String[] args) {
        new JavaApplication170();
    }

    public JavaApplication170() {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
                    ex.printStackTrace();
                }

                JFrame frame = new JFrame("Testing");
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.add(new TopPane(), BorderLayout.NORTH);
                frame.add(new LeftPane(), BorderLayout.WEST);
                frame.add(new JScrollPane(new FieldsPane()));
                frame.pack();
                //frame.setExtendedState(JFrame.MAXIMIZED_BOTH);
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }
        });
    }

    public class TopPane extends JPanel {

        public TopPane() {
            setLayout(new GridLayout(1, 0));
            add(new JLabel("Some title"));
            add(new JButton("One"));
            add(new JButton("Two"));
            add(new JButton("Three"));
            add(new JButton("Four"));
        }

    }

    public class LeftPane extends JPanel {

        public LeftPane() {
            setLayout(new GridLayout(0, 1));
            add(new JButton("One"));
            add(new JButton("Two"));
            add(new JButton("Three"));
            add(new JButton("Four"));
            add(new JButton("Five"));
            add(new JButton("Six"));
            add(new JButton("Seven"));
        }

    }

    public class FieldsPane extends JPanel {

        public FieldsPane() {
            setLayout(new GridBagLayout());
            addField("Field one", 0);
            addField("Field two", 1);
            addField("Field three", 2);
            addField("Field four", 3);
            addField("Field five", 4);
            addField("Field six", 5);
            addField("Field seven", 6);
            addField("Field eight", 7);
            addField("Field nine", 8);
            addField("Field ten", 9);
            addField("Field eleven", 10);
            addField("Field tweleve", 11);

            GridBagConstraints gbc = new GridBagConstraints();
            gbc.gridheight = 3;
            gbc.gridx = 2;
            gbc.gridy = 1;
            //gbc.weightx = 1;
            add(makeProfileLabel(), gbc);

            gbc.gridheight = 1;
            gbc.gridy += 3;
            add(new JButton("Some button"), gbc);
        }

        protected void addField(String text, int row) {
            GridBagConstraints gbc = new GridBagConstraints();
            gbc.gridx = 0;
            gbc.gridy = row;
            gbc.insets = new Insets(4, 4, 4, 4);
            add(new JLabel(text), gbc);
            gbc.gridx++;
            add(new JTextField(10), gbc);
        }

        protected JLabel makeProfileLabel() {
            JLabel label = new JLabel();
            label.setPreferredSize(new Dimension(50, 50));
            label.setBorder(new LineBorder(Color.BLACK));
            return label;
        }

    }

}

Now, this is a relatively simple example intended to demonstrate the concept of using multiple layouts.

For example, I might be tempted to use another GridBagLayout to layout the "main" layout, which would give me more control over the "some title" label

Layouts can be a bit of a (black magic) art form, but if you start with the "base" requirements, look at the functionality the UI is trying to produce and break those requirements down, it will be easier then trying to dump all the content into a single component

MadProgrammer
  • 343,457
  • 22
  • 230
  • 366
0

I'm guessing that this is a JFrame. At some point you add your main panel to the frame.

this.add(panel);

Instead, wrap the panel in a JScrollPane and add() that to the frame.

JScrollPane scrollPane = new JScrollPane(panel);
this.add(scrollPane);
this.pack();
this.setExtendedState(getExtendedState() | MAXIMIZED_BOTH);
this.setVisible(true);

It would be better if we resized the components to fit screen size rather than using a scrollPane.

Here's some other ideas:

  • Use a size variant where it's possible.

  • Decrease any gaps in the registration panel, e.g. setHgap() and setVgap() for GridLayout.

  • Make just the registration panel Scrollable.

Catalina Island
  • 7,027
  • 2
  • 23
  • 42
  • yeah Thanks. But that won't be nice. It would be better if we resized the components to fit screen size rather than using a `scrollpane` – Previnkumar Jun 27 '17 at 11:46
  • OK. I added some other ideas above. Update your question so other people will know that you don't want a `JScrollPane`. – Catalina Island Jun 27 '17 at 16:41