tags:

views:

56

answers:

2
+1  Q: 

borders in java

Hi

I want to set single title border to group of textfields how can i do this in java / swing.

i have tried below code but text fields are compressing inside panel

// Create panel and add some components to it.
JPanel pnl = new JPanel(new FlowLayout(FlowLayout.LEFT));

pnl.add(new JLabel("Name"));
pnl.add(new JTextField());

// Add titled border to panel, which will therefore surround
// all child components placed on the panel.
pnl.setBorder(BorderFactory.createTitledBorder("It's Friday!"));
+3  A: 

Here's an example using Swing:

// Create panel and add some components to it.
JPanel pnl = new JPanel(new FlowLayout(FlowLayout.LEFT));

pnl.add(new JLabel("Name"));
pnl.add(new JTextField());

// Add titled border to panel, which will therefore surround
// all child components placed on the panel.
pnl.setBorder(BorderFactory.createTitledBorder("It's Friday!"));
Adamski
i have tried this but text fields are compressing inside panel
ganga
The text fields "compressing" is not a result of applying the border, but rather because you haven't set a preferred size. For `JTextField` you can do this using `setPreferredSize` or create the text field with a specified number of columns.
Adamski
+1  A: 

That is because the text fields have no size set yet. It's quickest to set a size by using the setColumns(int) method. You could also use the setPreferredSize(Dimension).

willcodejavaforfood