Reputation: 510
I am using the below code to design my JFrame. But the JTextfield is not getting resized if I try to change the JFrame size.I have used Border Layout
and aligned it to center as per some suggestion for layout to handle the resizing.
public class Parser extends JFrame{
static JFrame frame;
static JLabel lblFile;
static JPanel pHdr;
static JPanel pBody;
static JPanel pFtr;
static JPanel pMainPanel;
static JPanel pOuterMainPanel;
static JLabel lblImage;
public Parser()
{
String startUpPath = System.getProperty("user.dir");
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
frame = new JFrame("Log Parser");
frame.setPreferredSize(new Dimension(700,500));
frame.setLocation((int)screenSize.getWidth()/3,(int)screenSize.getHeight()/4);
frame.setLayout(new BorderLayout());
//initialize the JPanels and add to frames
pOuterMainPanel = new JPanel(new BorderLayout());
pHdr = new JPanel(new BorderLayout(700,100));
pHdr.setPreferredSize(new Dimension(700,100));
pHdr.setOpaque(true);
pHdr.setBackground(Color.WHITE);
pOuterMainPanel.add(pHdr,BorderLayout.NORTH);
pMainPanel = new JPanel(new BorderLayout(700,400));
pBody = new JPanel(new GridBagLayout());
pMainPanel.add(pBody,BorderLayout.CENTER);
pBody.setPreferredSize(new Dimension(700,400));
pBody.setOpaque(true);
pBody.setBackground(Color.WHITE);
pOuterMainPanel.add(pMainPanel,BorderLayout.CENTER);
pFtr = new JPanel(new BorderLayout(700,50));
pFtr.setOpaque(true);
pFtr.setBackground(Color.WHITE);
pFtr.setPreferredSize(new Dimension(700,50));
pOuterMainPanel.add(pFtr,BorderLayout.SOUTH);
//Customize Hdr Panel
lblImage = new JLabel();
ImageIcon imgicon = new ImageIcon(startUpPath + "\\Resources\\IM.png");
lblImage.setIcon(imgicon);
pHdr.add(lblImage,BorderLayout.CENTER);
//Customize Body Panel
GridBagConstraints gc = new GridBagConstraints();
gc.gridx=0;
gc.gridy=0;
gc.anchor = GridBagConstraints.CENTER;
JLabel lblPath = new JLabel("Path ");
lblPath.setForeground(Color.darkGray);
lblPath.setFont(new Font("Times New Roman", Font.BOLD, 15));
pBody.add(lblPath,gc);
gc.gridx++;
final JTextField txtFilePath = new JTextField(30);
txtFilePath.setPreferredSize(new Dimension(30,30));
pBody.add(txtFilePath,gc);
gc.gridx++;
JLabel lblsp1 = new JLabel(" ");
pBody.add(lblsp1,gc);
gc.gridx++;
final JButton btnBrowse = new JButton("Browse");
btnBrowse.setBackground(new Color(224,224,224));
btnBrowse.setPreferredSize(new Dimension(80,30));
pBody.add(btnBrowse,gc);
gc.gridx++;
//Finalize the Frame
frame.add(pOuterMainPanel,BorderLayout.CENTER);
frame.pack();
frame.setVisible(true);
btnBrowse.addMouseListener(new MouseAdapter()
{
public void mouseEntered(MouseEvent evt)
{
btnBrowse.setBackground(new Color(30,91,195));
btnBrowse.setForeground(Color.white);
}
public void mouseExited(MouseEvent evt)
{
btnBrowse.setBackground(new Color(224,224,224));
btnBrowse.setForeground(Color.black);
}
});
FocusListener fl = new FocusListener() {
@Override
public void focusLost(FocusEvent e) {
// TODO Auto-generated method stub
txtFilePath.setBorder(BorderFactory.createLineBorder(Color.GRAY,1));
}
@Override
public void focusGained(FocusEvent e) {
// TODO Auto-generated method stub
txtFilePath.setBorder(BorderFactory.createLineBorder(new Color(34,5,134),2));
}
};
txtFilePath.addFocusListener(fl);
}
public static void main(String[] args) {
// TODO Auto-generated method stub
new Parser();
}
}
Upvotes: 0
Views: 124
Reputation: 4039
You should definitly learn more about how GridBagLayout works.
For solving your problem I want to introduce you to the weight
variables. They are part of the GridBagConstraints
and since you declared your own instance of this they are initialized with value 0
.
So you need to set gc.fill = GridBagConstraints.HORIZONTAL;
to encourage the components to use the available horizontal space.
The Layout does this by using the weight
values to determine how much of the free space will be 'given' to each component regarding their preferredSize
.
So if you want your JTextField
to get all the free space declare gc.weightx = 1.0f;
means 100% of free Space in horizontal direction before adding it. And set it back to 0.0f
before adding the next element.
Upvotes: 1