Go Back   TechArena Community > Software > Software Development
Become a Member!
Forgot your username/password?
Register Tags Active Topics RSS Search Mark Forums Read

Sponsored Links



JButton must retain its position when resizing

Software Development


Reply
 
Thread Tools Search this Thread
  #1  
Old 19-01-2010
Member
 
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 11:56 AM.
Reply With Quote
  #2  
Old 19-01-2010
Member
 
Join Date: May 2008
Posts: 2,005
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.
Reply With Quote
  #3  
Old 19-01-2010
Member
 
Join Date: May 2008
Posts: 2,380
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.
__________________
The FIFA Manager 2009 PC Game
Reply With Quote
  #4  
Old 19-01-2010
Member
 
Join Date: Jan 2008
Posts: 1,513
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.
Reply With Quote
  #5  
Old 19-01-2010
Member
 
Join Date: Dec 2009
Posts: 204
Re: JButton must retain its position when resizing

Hi,
Quote:
jpan.setLayout(new java.awt.BorderLayout());
jpan.add(jButton1, java.awt.BorderLayout.LINE_END);
getContentPane().add(jpan, java.awt.BorderLayout.PAGE_END);
And
Quote:
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.
Reply With Quote
  #6  
Old 19-01-2010
Member
 
Join Date: Apr 2008
Posts: 1,946
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
}
Reply With Quote
Reply

  TechArena Community > Software > Software Development
Tags: , , , , ,



Thread Tools Search this Thread
Search this Thread:

Advanced Search


Similar Threads for: "JButton must retain its position when resizing"
Thread Thread Starter Forum Replies Last Post
How to handle JButton Click Event in Java Swing Kungfu Pandey Software Development 3 20-01-2012 11:13 AM
Add actionlistener to JButton KAMANA Software Development 6 03-08-2010 10:54 AM
JButton to send command to Java console Fox28 Software Development 6 21-07-2010 10:03 AM
What is the use of JButton class? Owen Fernandes Software Development 5 19-02-2010 07:19 AM
JButton does not work in java swing RogerFielden Software Development 3 15-04-2009 12:49 PM


All times are GMT +5.5. The time now is 08:00 AM.