Reputation: 16428
I am just trying Java BorderLayout
and GridLayout
with some GUI components.
I am trying for right-align of JLabel
s and left-align the JTextField
s so that it can be much of better in look.
I found some answers in stackoverflow itself, with the help of setAlignmentX
and setHorizontalAlignment
. Both didn't work.
Basically, I have 3 components in each BorderLayout
.
Then, these components are being added to a GridLayout
on each row.
Code
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.GridLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
/**
*
* @author dinesh
*/
public class MySettings {
public static void main(String[] args) {
JFrame jf = new JFrame("hi");
//demo Info Panel start
// <editor-fold defaultstate="collapsed" desc="demo info panel">
JLabel lbldemoExecutablePath = new JLabel("demo Executable Path :");
// lbldemoExecutablePath.setAlignmentX(JLabel.LEADING);
// lbldemoExecutablePath.setAlignmentY(JLabel.LEADING);
// lbldemoExecutablePath.setHorizontalAlignment(SwingConstants.CENTER);
JTextField txtdemoInstallationPath = new JTextField(10);
txtdemoInstallationPath.setEditable(false);
txtdemoInstallationPath.setBackground(Color.WHITE);
txtdemoInstallationPath.setToolTipText("getdemoPath()");
txtdemoInstallationPath.setAlignmentX(JLabel.LEADING);
txtdemoInstallationPath.setAlignmentY(JLabel.LEADING);
JButton btndemoBrowse = new JButton("Browse");
btndemoBrowse.addActionListener(new java.awt.event.ActionListener() {
@Override
public void actionPerformed(java.awt.event.ActionEvent evt) {
System.out.println("demo browse button clicked");
}
});
JPanel pnldemoInfo = new JPanel(new BorderLayout(10, 10));
pnldemoInfo.add(lbldemoExecutablePath, BorderLayout.LINE_START);
pnldemoInfo.add(txtdemoInstallationPath, BorderLayout.CENTER);
pnldemoInfo.add(btndemoBrowse, BorderLayout.LINE_END);
// </editor-fold>
//demo info panel end
//sample Info Panel start
// <editor-fold defaultstate="collapsed" desc="sample info panel">
JLabel lblsampleExecutablePath = new JLabel("sample Executable Path :");
// lblsampleExecutablePath.setAlignmentX(JLabel.LEADING);
// lblsampleExecutablePath.setAlignmentY(JLabel.LEADING);
JTextField txtsampleInstallationPath = new JTextField(10);
txtsampleInstallationPath.setEditable(false);
txtsampleInstallationPath.setBackground(Color.WHITE);
txtsampleInstallationPath.setToolTipText("getsamplePath()");
JButton btnsampleBrowse = new JButton("Browse");
btnsampleBrowse.addActionListener(new java.awt.event.ActionListener() {
@Override
public void actionPerformed(java.awt.event.ActionEvent evt) {
System.out.println("demo browse button clicked");
}
});
JPanel pnlsampleInfo = new JPanel(new BorderLayout(10, 10));
pnlsampleInfo.add(lblsampleExecutablePath, BorderLayout.LINE_START);
pnlsampleInfo.add(txtsampleInstallationPath, BorderLayout.CENTER);
pnlsampleInfo.add(btnsampleBrowse, BorderLayout.LINE_END);
// </editor-fold>
//sample info panel end
//app Info Panel start
// <editor-fold defaultstate="collapsed" desc="app info panel">
JLabel lblappExecutablePath = new JLabel("app Executable Path :");
// lblappExecutablePath.setAlignmentX(JLabel.LEADING);
JTextField txtappExecutablePath = new JTextField(10);
txtappExecutablePath.setEditable(false);
txtappExecutablePath.setBackground(Color.WHITE);
txtappExecutablePath.setToolTipText("gettxtappExecutablePath()");
JButton btnappBrowse = new JButton("Browse");
btnappBrowse.addActionListener(new java.awt.event.ActionListener() {
@Override
public void actionPerformed(java.awt.event.ActionEvent evt) {
System.out.println("appBrowse browse button clicked");
}
});
JPanel pnlappInfo = new JPanel(new BorderLayout(10, 10));
pnlappInfo.add(lblappExecutablePath, BorderLayout.LINE_START);
pnlappInfo.add(txtappExecutablePath, BorderLayout.CENTER);
pnlappInfo.add(btnappBrowse, BorderLayout.LINE_END);
// </editor-fold>
//app info panel end
//logfile Info Panel start
// <editor-fold defaultstate="collapsed" desc="logfile info panel">
JLabel lblLogFilePath = new JLabel("Log File Path :");
// lblLogFilePath.setAlignmentX(JLabel.LEADING);
JTextField txtLogFilePath = new JTextField(10);
txtLogFilePath.setEditable(false);
txtLogFilePath.setBackground(Color.WHITE);
txtLogFilePath.setToolTipText("gettxtLogFilePath()");
JButton btnLogFileBrowse = new JButton("Browse");
btnLogFileBrowse.addActionListener(new java.awt.event.ActionListener() {
@Override
public void actionPerformed(java.awt.event.ActionEvent evt) {
System.out.println("appBrowse browse button clicked");
}
});
JPanel pnlLogFileInfo = new JPanel(new BorderLayout(10, 10));
pnlLogFileInfo.add(lblLogFilePath, BorderLayout.LINE_START);
pnlLogFileInfo.add(txtLogFilePath, BorderLayout.CENTER);
pnlLogFileInfo.add(btnLogFileBrowse, BorderLayout.LINE_END);
// </editor-fold>
//logfile info panel end
JPanel pnlGereal = new JPanel(new GridLayout(0, 1));
pnlGereal.add(pnldemoInfo);
pnlGereal.add(pnlsampleInfo);
pnlGereal.add(pnlappInfo);
pnlGereal.add(pnlLogFileInfo);
jf.add(pnlGereal);
jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//jf.setSize(500, 500);
jf.pack();
jf.setVisible(true);
}
}
Where am I making the mistake ? Where I exactly set the alignment ?
Upvotes: 4
Views: 2087
Reputation: 2335
Alignment doesn't work here because BorderLayout
is using sizes of JLabels
and dynamically adjust width to it.
You could try to see that alignment works by using this code:
JLabel lbldemoExecutablePath = new JLabel( "demo Executable Path :" );
lbldemoExecutablePath.setPreferredSize( new Dimension(200,100) );
lbldemoExecutablePath.setHorizontalAlignment( SwingConstants.RIGHT );
You can see here that your JLabel( "demo Executable Path :" ) is at right. However you shouldn't use setPreferredSize() method in your code I post it only to show you that alignment works.
Instead of BorderLayout
you could use FormLayout
from JGoodies http://www.jgoodies.com/freeware/libraries/forms/.
Upvotes: 3
Reputation: 2701
There are many ways to solve this. You can break the whole frame up in 3 panels, one for each JLabel
, JTextField
and JButton
. You can then set the size of each individual JPanel
, without having to worry about the individual components contained in the panel.
+----------------+----------------+----------------+
| panel1 | panel2 | panel3 |
+----------------+----------------+----------------+
| label1 | field1 | button1 |
| label2 | field2 | button2 |
| label3 | field3 | button3 |
| label4 | field4 | button4 |
+----------------+----------------+----------------+
Or you can also just give each JLabel
the same width. Like this:
final int LABEL_WIDTH = 200;
...
lbldemoExecutablePath.setPreferredSize(new Dimension(LABEL_WIDTH, lbldemoExecutablePath.getHeight()));
...
lblsampleExecutablePath.setPreferredSize(new Dimension(LABEL_WIDTH, lblsampleExecutablePath.getHeight()));
...
lblappExecutablePath.setPreferredSize(new Dimension(LABEL_WIDTH, lblappExecutablePath.getHeight()));
...
lblLogFilePath.setPreferredSize(new Dimension(LABEL_WIDTH, lblLogFilePath.getHeight()));
Upvotes: 3