Results 1 to 6 of 6

Thread: JButton must retain its position when resizing

  1. #1
    Join Date
    Dec 2009
    Posts
    204

    JButton must retain its position when resizing

    Hello,
    Though I searched the internet and tried, understand how Layouts in Swing work but it does not helped me. I still can not build a simple interface containing a JButton that must keep its distance right and down relative to the main window while resizing thereof. I building my interfaces using Netbeans. Can any one help me with this concept? Any help will be appreciated? Thanks in advance.
    Last edited by Ash maker; 19-01-2010 at 12:56 PM.

  2. #2
    Join Date
    May 2008
    Posts
    2,012

    Re: JButton must retain its position when resizing

    Hi,
    I think a BoxLayout should do the trick here for you. Have you tried the BoxLayout method and compiled your code? I think you shoudl add it to the upper left and add a 'glue' and the lower and right a 'strut' of the size that suits you. For more methods regarding your problem can be found with, static methods Box.create. Though I will not recommend it to you.

  3. #3
    Join Date
    May 2008
    Posts
    2,389

    Re: JButton must retain its position when resizing

    Hello,
    This is easiest with NetBeans, you have only to place the button at the place you want. However, personally I would use the BorderLayout (Java is the default layout for a JFrame), you place them down a panel with BorderLayout and FlowLayout-Properties-Alignment = Right, and you add the Button in this panel.
    Code:
    jpan.setLayout(new java.awt.FlowLayout(java.awt.FlowLayout.RIGHT));
    jpan.add(jButton1);
    getContentPane().add(jpan, java.awt.BorderLayout.PAGE_END);
    You can use the above methods in your program.

  4. #4
    Join Date
    Jan 2008
    Posts
    1,521

    Re: JButton must retain its position when resizing

    Hello,
    The above mentioned are correct, you can also use BorderLayout panel and you can change the JButton in the Layout-Properties-Button direction on "After".
    Code:
    jpan.setLayout(new java.awt.BorderLayout());
    jpan.add(jButton1, java.awt.BorderLayout.LINE_END);
    getContentPane().add(jpan, java.awt.BorderLayout.PAGE_END);
    As you see, there are different solutions. I can not tell you which is the best in your case. In general, I always try to nest different panels with different layout to achieve the results that I want.

  5. #5
    Join Date
    Dec 2009
    Posts
    204

    Re: JButton must retain its position when resizing

    Hi,
    jpan.setLayout(new java.awt.BorderLayout());
    jpan.add(jButton1, java.awt.BorderLayout.LINE_END);
    getContentPane().add(jpan, java.awt.BorderLayout.PAGE_END);
    And
    jpan.setLayout(new java.awt.FlowLayout(java.awt.FlowLayout.RIGHT));
    jpan.add(jButton1);
    getContentPane().add(jpan, java.awt.BorderLayout.PAGE_END);
    Both are useful methods to implement, I understand your solution with the nesting, I thought may be that I'll make it with one layout as GridBagLayout. To me free design looks like it will solve the problem. Thank you for every one who answered.

  6. #6
    Join Date
    Apr 2008
    Posts
    1,948

    Re: JButton must retain its position when resizing

    Hello,
    I understand your concern with the Free Design, because it is done with a single click. Look at the code below
    Code:
    Public class NewJFrame1 extends javax.swing.JFrame {
     
        Public NewJFrame1() {
            great("NewJFrame1");
            initComponents();
        }
     
     
        / / <editor-fold Defaultstate="collapsed" desc="Generated code">
        private void initComponents() {
    
            jbut1= new javax.swing.JButton();
    
            setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
    
            jbut1.setText("jbut1");
    
            org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(getContentPane());
            getContentPane().setLayout(layout);
            layout.setHorizontalGroup(
                layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                .add(org.jdesktop.layout.GroupLayout.TRAILING, Layout.createSequentialGroup()
                    .addContainerGap(317, Short.MAX_VALUE)
                    .add(jbut1)
                    .addContainerGap())
            );
            layout.setVerticalGroup(
                layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                .add(org.jdesktop.layout.GroupLayout.TRAILING, Layout.createSequentialGroup()
                    .addContainerGap(266, Short.MAX_VALUE)
                    .add(jbut1)
                    .addContainerGap())
            );
    
            java.awt.Size scrsize = java.awt.Toolkit.getDefaultToolkit().getScreenSize();
            setBounds((scrsize.width-408)/2, (scrsize.height-334)/2, 408, 334);
        }/ / </ Editor-fold>
     
        Public static void hand(Final String[] args) {
            Runnable guitest = new Runnable() {
     
                Public void run() {
                    new NewJFrame1().setVisible(true);
                }
            };
            / / GUI must start on EventDispatchThread:
            SwingUtilities.invokeLater(guitest);
        }
     
        / / Variables declaration - do not modify
        private javax.swing.JButton jbut1;
        / / End of variables declaration
    }

Similar Threads

  1. How to handle JButton Click Event in Java Swing
    By Kungfu Pandey in forum Software Development
    Replies: 3
    Last Post: 20-01-2012, 12:13 PM
  2. Add actionlistener to JButton
    By KAMANA in forum Software Development
    Replies: 6
    Last Post: 03-08-2010, 10:54 AM
  3. JButton to send command to Java console
    By Fox28 in forum Software Development
    Replies: 6
    Last Post: 21-07-2010, 10:03 AM
  4. What is the use of JButton class?
    By Owen Fernandes in forum Software Development
    Replies: 5
    Last Post: 19-02-2010, 08:19 AM
  5. JButton does not work in java swing
    By RogerFielden in forum Software Development
    Replies: 3
    Last Post: 15-04-2009, 12:49 PM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Page generated in 1,711,638,190.71813 seconds with 17 queries