JFrame Netbeans & resizing

So, I am using Netbeans to create a simple game, I am not familiar with coding layouts by hand which is where I am having a problem. The first level of the game has a grid of cards with dimensions 4 x 4. Level two has a grid of cards with dimensions 4 X 8. There is a container on the right side of the screen that holds information like how many moves the user has made etc. My question is what do I need to research in order to make the screen as compact as possible in each level. For example in level one with the 4x4 grid there is a lot of wasted room between the grid of cards and the container of info on the right. I would like to remove the wasted space, but in level two the space is needed to hold the 4x8 card grid so I would like it to expand to fit it.
A pointer in the right direction would be more than enough. I have been reading a lot about layouts etc. and cannot find anything to solve this. Maybe it is more complex than I thought it would be.
Thanks,
chris

Swing related questions should be posted in the Swing forum.
For example in level one with the 4x4 grid there is a lot of wasted room between the grid of cards and the container of info on the rightmake sure you pack() the frame.
If you need further help then you need to create a [Short, Self Contained, Compilable and Executable, Example Program (SSCCE)|http://homepage1.nifty.com/algafield/sscce.html], that demonstrates the incorrect behaviour.
Don't forget to use the [Code Formatting Tags|http://forum.java.sun.com/help.jspa?sec=formatting], so the posted code retains its original formatting.
Well, you could always set the layout to null and set the bounds yourself. Sure and you could always rewrite all the JDK classes yourself, but that wouldn't be very efficient would it? Why reinvent the wheel which is what you are doing buy using a null layout.

Similar Messages

  • Jframe undecorated resize

    hi..
    i am trying to create a custom JFrame kind of look, by setting it to undercorated and creating my own Title bar, status bar, borders, and close/minimize/maximize buttons.
    The problem that i am having is once you set the jframe to undercorated, the mouse events for resizing JFrame are gone. Is there a way to get it to resize again? (maybe by registering new mouse listeners and somehow redirecting them back to the jframe listeners)
    Thnx in advance.

    yea i suppose i could use a JWindow but i would still have the same resize problem.
    i tried making my own resize functions for the customized jframe dynamically resizing it while the mouse is dragged, but the whole frame flashes while resizing.
    The following code is an example of a test customised JFrame which shows the problem i am having (flashing while resizing)
    I only added the resize from the north (bottom) of the frame so i can make the code easier to read.
    I dont know if this is a good way or if there is an easier way to do it?
    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.*;
    public class TestFrame extends JFrame implements MouseMotionListener, MouseListener
         Point sp;
         int     compStartHeight;
         int minHeight = 100;
         JPanel frameContent = new JPanel();
         public TestFrame()
              super("testing frame");
              setSize(600, 600);
              setContentPane(frameContent);
              frameContent.setBackground(Color.black);
              frameContent.setLayout(new BoxLayout(frameContent, BoxLayout.Y_AXIS));     
              setUndecorated(true);
              addMouseMotionListener(this);
              addMouseListener(this);
              JButton testButton = new JButton("TEST");
              JButton testButton2 = new JButton ("TEST2");
              frameContent.add(testButton);
              frameContent.add(Box.createVerticalGlue());
              frameContent.add(testButton2);
              setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
              setVisible(true);
         public void mouseMoved(MouseEvent e)
              Point p = e.getPoint();
              if (p.y > e.getComponent().getSize().height - 5)
                   setCursor( Cursor.getPredefinedCursor( Cursor.N_RESIZE_CURSOR ));
              else
                   setCursor( Cursor.getPredefinedCursor( Cursor.DEFAULT_CURSOR));
         public void mouseDragged(MouseEvent e)
              Point p = e.getPoint();
              int compWidth = getSize().width;
              if (getCursor().getType() == Cursor.N_RESIZE_CURSOR)
                   int nextHeight = compStartHeight+p.y-sp.y;
                   if (nextHeight > minHeight)
                        setSize(compWidth,nextHeight);
                        setVisible(true);     
              else
                   int x = getX()+p.x-sp.x;     
                   int y = getY()+p.y-sp.y;     
                   setLocation(x,y);
         public void mousePressed(MouseEvent e)
              sp = e.getPoint();
              compStartHeight = getSize().height;
         public void mouseEntered(MouseEvent e)
        public void mouseExited(MouseEvent e)
              if (sp == null)
                   setCursor( Cursor.getPredefinedCursor( Cursor.DEFAULT_CURSOR));
        public void mouseClicked(MouseEvent e)
        public void mouseReleased(MouseEvent e)
              sp = null;
         public static void main(String[] args)
         new TestFrame();
    }

  • JFrame titleBar resize ???

    hi,i got 2 file :
    satu.java
    import java.awt.BorderLayout;
    import java.awt.event.WindowAdapter;
    import java.awt.event.WindowEvent;
    import javax.swing.JFrame;
    public class satu {
    * @param args
    public static void main(String[] args) {
    // TODO Auto-generated method stub
    JFrame.setDefaultLookAndFeelDecorated(true);
    final JFrame appp = new JFrame("TEST ");
    appp.setSize(150,180);
    appp.setResizable(false);
    dua m = new dua(appp);
            m.init();
    appp.add(m,BorderLayout.CENTER);   
        appp.setVisible(true);
        //appp.setLocationRelativeTo(null);
    appp.addWindowListener(new WindowAdapter() {
       public void windowClosing(WindowEvent evt) {
                appp.setVisible(false);
                appp.dispose();
    }dua.java
    import java.applet.Applet;
    import java.awt.Dimension;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import javax.swing.JButton;
    import javax.swing.JFrame;
    public class dua extends Applet implements ActionListener {
    * @param args
    JFrame myframe;
    public dua(JFrame sat){
    myframe = sat;
    public void init(){
    JButton x = new JButton("Big");
    x.addActionListener(this);
    add(x);
    public void actionPerformed(ActionEvent arg0) {
    // TODO Auto-generated method stub
    int newx = myframe.getWidth()+10;
    int newy = myframe.getHeight()+10;
    myframe.setTitle("X = "+newx+" Y = "+newy);
    myframe.setSize(newx,newy);
    the problem is when i clicked the button,the titlebar doesnt resize...what should i add to resize the title bar too ?
    thanks.

    BACK !!! You have to validate parent frame. This works fine:
    import java.awt.BorderLayout;
    import java.awt.event.WindowAdapter;
    import java.awt.event.WindowEvent;
    import java.applet.Applet;
    import java.awt.Dimension;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import javax.swing.JButton;
    import javax.swing.JFrame;
    public class satu {
        public static void main(String[] args) {
            JFrame.setDefaultLookAndFeelDecorated(true);
            final JFrame appp = new JFrame("TEST ");
            appp.setSize(150,180);
            appp.setResizable(false);
            dua m = new dua(appp);
            m.init();
            appp.add(m,BorderLayout.CENTER);
            appp.setVisible(true);
            appp.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
    class dua extends Applet implements ActionListener {
        JFrame myframe;
        public dua(JFrame sat){
            myframe = sat;
        public void init(){
            JButton x = new JButton("Big");
            x.addActionListener(this);
            add(x);
        public void actionPerformed(ActionEvent arg0) {
            int newx = myframe.getWidth()+10;
            int newy = myframe.getHeight()+10;
            myframe.setTitle("X = "+newx+" Y = "+newy);
            myframe.setSize(newx,newy);
            myframe.validate();
    }

  • How to resize the components in a JFrame when the JFrame Resizes?

    Hi all,
    i have some problems with my app. I have set the JFrame resizable and that works fine for the JFrame but not for the components in the JFrame.
    So my question is how can i set the components in a JFrame to resize automatically when the JFrame resizes?
    Here are the important things from my code,...if you need more, just let me know, its my first post in a forum .
    Its a JFrame with a JTabbedPane and on the Tabs are Panels with buttons and Tables.
    Thank you in advance!
    public class MainMonitor extends JFrame {
    public MainMonitor() {
              super();
              initialize();
              setVisible(true);
         private void initialize() {
              this.setSize(1145, 785);
              this.setIconImage(Toolkit.getDefaultToolkit().getImage("Images/c.gif"));
              this.setContentPane(getJContentPane());
              this.setTitle("Company Manager");
              this.setResizable(true);
              this.setLocationRelativeTo(null);
         private JPanel getJContentPane() {
              if (jContentPane == null) {
                   jContentPane = new JPanel();
                   jContentPane.setLayout(null);
                   jContentPane.setOpaque(true);
                   jContentPane.add(getJTabbedPane(), null);
                   jContentPane.add(getJPanel11(), null);
              return jContentPane;
         private JTabbedPane getJTabbedPane() {
              if (jTabbedPane == null) {
                   jTabbedPane = new JTabbedPane();
                   jTabbedPane.setLocation(new Point(6, 69));
                   jTabbedPane.setSize(new Dimension(1120, 684));
                   jTabbedPane.addTab("P", null, getJPanel(), null);
                   jTabbedPane.addTab("K", null, getJPanel1(), null);
                   jTabbedPane.addTab("B", null, getJPanel2(), null);
              return jTabbedPane;
         

    Giasaste wrote:
    Thanks a lot, i didnt knew that the Layout Manager is a must.It's not a must, but it is the way to allow your app to be resized nicely and makes it much easier to maintain and enhance your program. Imagine creating a complex gui then later decide that you want another radiobutton and to remove a jlabel / jtextfield pair. with layout managers a chore becomes easy.

  • Is there an easy way to programatically resize a JFrame?

    I'm having layout problems. When my app starts up, some things are not laid out correctly. If I resize the frame by one pixel even, the entire thing redraws and it's correct.
    There must be a way to force resizing of a JFrame programmatically that would achieve the same effect. However, there must also be a programatic way to say, "redo all the layouts". I've tried the obvious ones, like doLayout(), to no effect. What actually happens when a JFrame is resized? I basically want all that to happen, minus the actual size change.

    Yes typically caused by adding components to your frame after you have set it to be visible. However you can call frame.repaint(); to get the frame to repaint itself. When you resize a frame by even 1 pixel, frame.repaint() is called automatically to redraw all the contents with the new size you specify. So if you bypass the resize and call repaint you will get the same effect.
    Hope that works.
    Jason Barraclough.

  • Dynamic resize of JButton and JFrame in response to Font

    Im supposed to increase the font size of the text with JButton by 1 within each click.
    Eventually the text becomes shorter and less visible like WORD becomes WOR... then WO... etc
    How to make the button always resize with text so that the text is fully visible and JFrame always resize with button so that the button doesn't change its position within the Frame ?
    edit:
    I managed to make buttons resize with increasing font
    by making
    JButton b = new JButton("button");
    b.setHorizontalTextPosition(CENTER);
    b.setVerticalTextPosition(CENTER);However the window size doesnt increase with components ;[
    any help would be appretiated.
    Edited by: pimpcane on Dec 11, 2007 12:16 PM

    Ok I managed to get it working moreless by
    adding pack() to the actionPerformed(...) function
    public void actionPerformed(ActionEvent e)
                 int index = Integer.parseInt(e.getActionCommand());
              int size = buttons[index].getFont().getSize();
              size++;
              buttons[index].setFont(new Font(name, style, size));
              pack();
    }The problem is in the task im given it is forbidden to use pack();
    Is there any other method to obtain the same result of JFrame resizing dynamically in response to components resize ?
    Edited by: pimpcane on Dec 12, 2007 12:13 PM
    Edited by: pimpcane on Dec 12, 2007 12:15 PM

  • Increasing font size when resizing JFrame

    Hello,
    I am writing a calculator program, and therefore have a JLabel to display the numbers and multiple buttons. The JLabel and JButton components are added to a panel with a GridBagLayout, which is then added to the content pane of a JFrame. When the JFrame is resized, the size of the JButton and JLabel are increased, but the font in both of them stays the same. Is there anyway to increase the font size proportional to the size of the JButton and JLabel?

    Note: This thread was originally posted in the [Java Programming|http://forums.sun.com/forum.jspa?forumID=31] forum, but moved to this forum for closer topic alignment.

  • Question to Swing Pros: Resizing of JFrame only with visible Rectangular  ?

    Hello folks,
    quite below is the quoted text message is from Java Substance Forum: http://www.nabble.com/Removing-top-left-substance-menu-and-deactivating-Background-painting-while-Resizing-a-Frame--to17775101.html
    Is it somehow possible to use substance or another LooknFeel with custom titelbar and still keeping a hidden JFrame background resizing? So if i resize a JFrame only a rectangular should be visible not the content of the window should dynamically resize.
    That would be a general Swing question to ask in Swing forums on java.net or on java.sun.com
    Thanks
    Kirill
    ----- Original Message ----
    From: PEL <liquidc4d@...>
    To: users@...
    Sent: Wednesday, June 11, 2008 9:07:36 AM
    Subject: Re: Removing top left substance menu and deactivating Background painting while Resizing a Frame?
    2. You would notice in your screenshots that you're running Substance
    under decorated mode, but Metal (and Windows) under undecorated mode
    (title pane coming from OS). Try running your app under decorated mode in
    Metal and see if the dynamic resizing is the same.
    Ok with decorated(false) the resizing of the frame shows only a border not
    the whole background of the frame, but then i have no stunning
    Glas3DDecoration painter :D
    Is it somehow possible to use substance with 3dglass titelbar and a hidden
    jframe background resizing?

    I've never seen a way to do that in Swing normally.
    Of course, if you could run something allowing transparent windows (some newer version of Java will support it, or Skin L&F, etc), I can imagine how you might fake it by using your own decorations, and on resize, create a transparent window with a border that is your resize window, and use that to resize, and then fix the final size to the real window when done.

  • Making a JFrame only Vertically Resizable

    How can i make a JFrame vertically resizable but ONLY vertically? This seems to be a hard thing to find information on. I would appreciate any help.

    Well, ideally, i dont want the user to be able to click the side of the window and drag to the right at all, i want the curser to be stuck at the border of the window when resizing but still be able to stretch the bottom. I dont want the window to stretch and then stretch back horizontally, it seems poorly programmed. If somebody knows a "correct" way to do this, it would be nice to know. Thanks for your help.

  • Auto resizing JInternalFrame while resizing its parent JFrame

    Scenario: I have a JFrame and JInternalFrame. Here JInternalFrame is the child window of JFrame.
    Problem: When I drag the JFrame using mouse the JFrame is resizing automatically but the JInternalFrame is not automatically resizing. I want the JInternalFrame should automatically resize while dragging JFrame.
    Any help will be highly appriciated.

    Add a [Component Listener|http://java.sun.com/docs/books/tutorial/uiswing/events/componentlistener.html] to the JFrame and resize your JInternalFrame according to the JFrame size.

  • JFrame Not Painting Until Resize

    I have a couple of components in my JFrame. At first, it looks like the JFrame's not painting. I tried calling repaint() before and after I made the JFrame visible. I also moved the JFrame, which should repaint it. However, if I try to resize the JFrame or just click while the cursor is in "resize mode," everything draws and acts fine. Unfortunately, I want to make the JFrame non-resizable. What's the problem?

    try calling the validate method before repainting

  • Resize JTable in JTabbedPane

    Given the FileTableDemo from the Java Foundation Class in a Nutshell book.(O'Reilly) Chapter 3 page 51 in my addition.
    I have modified the example to place the JTable in a JTabbedPane. Now when the window (JFrame) is resized by the user the JTable no longer resizes. How do I passing the "resizing" into the component (JTable) placed in the JTabbedPane?
    Here is the code: My deviation from the example is enclosed in "///////////KBS" and of course the line frame.getContentPane().add(myTP, "Center"); where "pane" has been replaced with "myTP"
    Thanks Brad
    import javax.swing.*;
    import javax.swing.table.*;
    import java.io.File;
    import java.util.Date;
    public class FileTableDemo
    public static void main(String[] args)
    // Figure out what directory to display
    File dir;
    if (args.length > 0)
    dir = new File(args[0]);
    else
    dir = new File(System.getProperty("user.home"));
    // Create a TableModel object to represent the contents of the directory
    FileTableModel model = new FileTableModel(dir);
    // Create a JTable and tell it to display our model
    JTable table = new JTable(model);
    JScrollPane pane = new JScrollPane(table);
    pane.setBorder(BorderFactory.createEmptyBorder(60,20,20,20));
    ////////KBS
    JTabbedPane myTP = new JTabbedPane();
    JPanel myOne = new JPanel();
    JPanel myTwo = new JPanel();
    myOne.add(pane);
    myTP.add("One", myOne);
    myTP.add("Two", myTwo);
    ////////KBS
    // Display it all in a scrolling window and make the window appear
    JFrame frame = new JFrame("FileTableDemo");
    frame.getContentPane().add(myTP, "Center");
    frame.setSize(600, 400);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.pack();
    frame.setVisible(true);
    // The methods in this class allow the JTable component to get
    // and display data about the files in a specified directory.
    // It represents a table with six columns: filename, size, modification date,
    // plus three columns for flags: direcotry, readable, writable
    class FileTableModel extends AbstractTableModel
    protected File dir;
    protected String[] filenames;
    protected String[] columnNames = new String[] {
    "name",
    "size",
    "last modified",
    "directory?",
    "readable?",
    "writable?"
    protected Class[] columnClasses = new Class[] {
    String.class,
    Long.class,
    Date.class,
    Boolean.class,
    Boolean.class,
    Boolean.class
    // This table model works for any one given directory
    public FileTableModel(File dir)
    this.dir = dir;
    this.filenames = dir.list();
    // These are easy methods
    public int getColumnCount()
    return 6;
    public int getRowCount()
    return filenames.length;
    // Information about each column
    public String getColumnName(int col)
    return columnNames[col];
    public Class getColumnClass(int col)
    return columnClasses[col];
    // The method that must actually return the value of each cell
    public Object getValueAt(int row, int col)
    File f = new File(dir, filenames[row]);
    switch(col)
    case 0: return filenames[row];
    case 1: return new Long(f.length());
    case 2: return new Date(f.lastModified());
    case 3: return f.isDirectory() ? Boolean.TRUE : Boolean.FALSE;
    case 4: return f.canRead() ? Boolean.TRUE : Boolean.FALSE;
    case 5: return f.canWrite() ? Boolean.TRUE : Boolean.FALSE;
    default: return null;
    }

    Well, I didn't find an answer but I did find a solution.
    The JPanel is the problem. I use it to help with layout which is not obvious in this example.
    As you can see in the code the JTable is placed in a JScrollPane which is place in a JPanel which is placed in a JTabbedPane.
    If I use Box instead of JPanel the JTable in the JScrollPane in the Box in the TabbedPane in the JFrame resizes correclty.
    So although JPanel is resizable with setSize it does not get resized when it's JFrame gets resized.
    I would still like to know why?
    ////////KBS
    JTabbedPane myTP = new JTabbedPane();
    Box myOne = Box.createHorizontalBox();
    Box myTwo = Box.createHorizontalBox();
    myOne.add(pane);
    myTP.add("One", myOne);
    myTP.add("Two", myTwo);
    ////////KBS

  • JTable in JPanel last Column resize clips JTable doesnt repaint JPanel

    I have a JTable in a JPanel, when I drag the last column of the JTableHeader (u can see the column resizing as the text of the Table HeaderColumn moves with the drag) the JTable is outside the JPanel. Only when u click on another component and then click back on the JTableHeader does it repaint.
    I have tried jPanel.revalidate(); in the mouseDragged event of MouseMotionListener on the JTableHeader and all manner or repaints and fireTableStructureChanged. But I dont understand the order of events.
    The last bug to fix!!!
    NB I dont want to use scrollpane as its not necessary and as many tables are synchronized with one table it makes repaints less smooth
    Thanks Paul

    Well, I didn't find an answer but I did find a solution.
    The JPanel is the problem. I use it to help with layout which is not obvious in this example.
    As you can see in the code the JTable is placed in a JScrollPane which is place in a JPanel which is placed in a JTabbedPane.
    If I use Box instead of JPanel the JTable in the JScrollPane in the Box in the TabbedPane in the JFrame resizes correclty.
    So although JPanel is resizable with setSize it does not get resized when it's JFrame gets resized.
    I would still like to know why?
    ////////KBS
    JTabbedPane myTP = new JTabbedPane();
    Box myOne = Box.createHorizontalBox();
    Box myTwo = Box.createHorizontalBox();
    myOne.add(pane);
    myTP.add("One", myOne);
    myTP.add("Two", myTwo);
    ////////KBS

  • JFrame drag problem

    Hi
    I have simple app that has a number of JFrames popping up etc, when i drag a popped up JFrame over my main JFrame it flickers and creates a trail until the dragging is stopped.
    Anyone know how to stop this ?!
    thx
    joj

    Add a [Component Listener|http://java.sun.com/docs/books/tutorial/uiswing/events/componentlistener.html] to the JFrame and resize your JInternalFrame according to the JFrame size.

  • Rescale GUI-Components on resizeing frame?

    Hello,
    how do I have to implement rescaling GUI-components when a frame (JFrame) is resized by a user on runtime?
    Should I have to use the observer-observable-pattern?
    thanx

    If you've got a layout manager then its automatic otherwise add a WindowListener to capture the resize event then resize them yourself.
    Rob.

Maybe you are looking for

  • How do i get my security questions if i forgot mine

    i cant get my answers right

  • IDOC in status 2

    Hi, I am trying to post an IDOC through R/3 to XI system which finally converts into a file and goes to 3rd party. When I am posting IDOC in R/3 I am getting 02 status and error as: Communication error when sending with HTTP and Status as Error Passi

  • Third party access to airport extreme

    HELP! We have the aiport extreme which operates my iBook G4 laptop wirelessly. Now I like to connect our work computer to the wireless connection. The work computer is an "Acer" which has 802.11b/g wireless LAN capability. It picks up our wireless fe

  • My X61 can't connect wireless when i use battery!!!

    TYPE NO.:  7673-AN6 i confused why it can not connect wireless when i use battery?! the problem drive me crazy nearly, it means i must take adapter every time i help every one can help me! cheers,

  • Opening an Illustrator CS5 document in CS4

    I currently have Illustrator CS4. I am taking an online class and the lessons that were created for us were created in CS5. When I go to open the lessons in CS4, I get a message saying that the file was generated by a newer version of Illustrator and