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.
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.
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.
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.
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.
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
}