|
|
Welcome to the Java Forums.
You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community, you will:
- have access to post topics
- communicate privately with other members (PM)
- not see advertisements between posts
- have the possibility to earn one of our surprises if you are an active member
- access many other special features that will be introduced later.
Registration is fast, simple and absolutely free so please, join our community today!
If you have any problems with the registration process or your account login, please contact us.
|
|

01-28-2008, 03:55 PM
|
|
Member
|
|
Join Date: Jan 2008
Posts: 1
|
|
|
Need a idea on GridBagLayout
I need idea on GridBagLayout.
When ever I load dialog with below, without values in JList. It is working properly - http://sureshdevi.co.in/test/example1.jpg
While loading dialog with values in JList, Notes JTextArea getting increased. I want to make Notes as a Fixed Size. I tried with setPreferredSize but no use. http://sureshdevi.co.in/test/example2.jpg
private JPanel createPanel() {
JPanel jpMain = new JPanel(new GridBagLayout());
jpMain.setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10));
GridBagConstraints gbc = new GridBagConstraints();
gbc.gridx = 0;
gbc.gridy = 0;
gbc.gridwidth = 1;
gbc.weightx = 0.0;
gbc.fill = GridBagConstraints.NONE;
gbc.anchor = GridBagConstraints.NORTHWEST;
JLabel jlLookupText = new JLabel("Lookup text:");
jpMain.add(jlLookupText, gbc);
gbc.gridx = 1;
gbc.gridy = 0;
gbc.gridwidth = 8;
gbc.weightx = 1.0;
gbc.insets = new Insets(0, 5, 0, 5);
gbc.fill = GridBagConstraints.HORIZONTAL;
jtfLookupText = new JTextField();
jtfLookupText.setAlignmentX(Component.CENTER_ALIGNMENT);
jpMain.add(jtfLookupText, gbc);
gbc.gridx = 9;
gbc.gridy = 0;
gbc.gridwidth = 1;
gbc.weightx = 0.0;
gbc.insets = new Insets(0, 0, 0, 0);
gbc.fill = GridBagConstraints.NONE;
jbLookup = new JButton("Lookup");
jbLookup.setAlignmentX(Component.CENTER_ALIGNMENT);
jpMain.add(jbLookup, gbc);
gbc.gridx = 0;
gbc.gridy = 1;
gbc.gridwidth = 1;
gbc.weightx = 0.0;
gbc.insets = new Insets(5, 0, 0, 0);
gbc.fill = GridBagConstraints.NONE;
JLabel jlResults = new JLabel("Results:");
jpMain.add(jlResults, gbc);
gbc.gridx = 0;
gbc.gridy = 2;
gbc.gridwidth = 10;
gbc.weightx = 1.0;
gbc.weighty = 1.0;
gbc.gridheight = 5;
gbc.insets = new Insets(5, 0, 5, 0);
gbc.anchor = GridBagConstraints.CENTER;
gbc.fill = GridBagConstraints.BOTH;
jlLookupResults = new JList(new Vector());
jlLookupResults.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
jlLookupResults.setCellRenderer(new MatchElementRenderer());
JScrollPane jspCustomPane = new JScrollPane(jlLookupResults, JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED, JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
jpMain.add(jspCustomPane, gbc);
gbc.gridx = 0;
gbc.gridy = 8;
gbc.gridwidth = 1;
gbc.weightx = 0.0;
gbc.weighty = 0.0;
gbc.gridheight = 1;
gbc.insets = new Insets(0, 0, 0, 0);
gbc.anchor = GridBagConstraints.SOUTHWEST;
gbc.fill = GridBagConstraints.NONE;
JLabel jlNotes = new JLabel("Notes:");
jpMain.add(jlNotes, gbc);
gbc.gridx = 0;
gbc.gridy = 9;
gbc.gridwidth = 10;
gbc.weightx = 1.0;
gbc.weighty = 1.0;
gbc.gridheight = 3;
gbc.insets = new Insets(5, 0, 5, 0);
gbc.anchor = GridBagConstraints.SOUTHWEST;
gbc.fill = GridBagConstraints.BOTH;
jtaNotes = new JTextArea();
jtaNotes.setEditable(false);
jtaNotes.setFont(jtfLookupText.getFont());
jtaNotes.setBackground(getBackground());
JScrollPane jspNotesPane = new JScrollPane(jtaNotes, JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED, JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
jpMain.add(jspNotesPane, gbc);
gbc.gridx = 8;
gbc.gridy = 13;
gbc.gridwidth = 1;
gbc.weightx = 0.0;
gbc.weighty = 0.0;
gbc.gridheight = 1;
gbc.insets = new Insets(5, 0, 0, 5);
gbc.anchor = GridBagConstraints.EAST;
gbc.fill = GridBagConstraints.NONE;
jbSelect = new JButton("Select");
jpMain.add(jbSelect, gbc);
gbc.gridx = 9;
gbc.gridy = 13;
gbc.gridwidth = 1;
gbc.weightx = 0.0;
gbc.weighty = 0.0;
gbc.gridheight = 1;
gbc.insets = new Insets(5, 0, 0, 0);
gbc.anchor = GridBagConstraints.EAST;
gbc.fill = GridBagConstraints.NONE;
jbCancel = new JButton("Cancel");
jpMain.add(jbCancel, gbc);
return jpMain;
}
|
|

01-29-2008, 03:32 AM
|
|
Senior Member
|
|
Join Date: Jul 2007
Posts: 1,222
|
|
import java.awt.*;
import java.util.Vector;
import javax.swing.*;
public class GBLTest {
JList jlLookupResults;
private JTabbedPane getContent() {
JTabbedPane tabbedPane = new JTabbedPane();
tabbedPane.addTab("another approach", tryThis());
tabbedPane.addTab("modified original", createPanel());
return tabbedPane;
}
private JPanel tryThis() {
JPanel panel = new JPanel(new GridBagLayout());
panel.setBorder(BorderFactory.createEmptyBorder(5, 10, 5, 10));
GridBagConstraints gbc = new GridBagConstraints();
gbc.insets = new Insets(0,2,0,2);
gbc.weighty = 1.0;
// row 1
JTextField jtfLookupText = new JTextField();
gbc.weightx = 0;
panel.add(new JLabel("Lookup text:"), gbc);
gbc.weightx = 1.0;
gbc.fill = GridBagConstraints.HORIZONTAL;
panel.add(jtfLookupText, gbc);
gbc.weightx = 0;
gbc.fill = GridBagConstraints.NONE;
panel.add(new JButton("Lookup"), gbc);
// row 2
gbc.gridy = 1;
gbc.weightx = 1.0;
gbc.gridwidth = 3;
gbc.anchor = GridBagConstraints.WEST;
panel.add(new JLabel("Results:"), gbc);
// row 3
jlLookupResults = new JList(new Vector());
jlLookupResults.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
// jlLookupResults.setCellRenderer(new MatchElementRenderer());
JScrollPane jspCustomPane = new JScrollPane(jlLookupResults);
gbc.gridy++;
gbc.anchor = GridBagConstraints.CENTER;
gbc.fill = GridBagConstraints.BOTH;
panel.add(jspCustomPane, gbc);
// row 4
gbc.gridy++;
gbc.anchor = GridBagConstraints.WEST;
gbc.fill = GridBagConstraints.NONE;
panel.add(new JLabel("Notes:"), gbc);
// row 5
gbc.gridy++;
JTextArea jtaNotes = new JTextArea(3,20);
jtaNotes.setEditable(false);
jtaNotes.setFont(jtfLookupText.getFont());
// jtaNotes.setBackground(getBackground());
JScrollPane jspNotesPane = new JScrollPane(jtaNotes,
JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED,
JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
gbc.anchor = GridBagConstraints.CENTER;
gbc.fill = GridBagConstraints.HORIZONTAL;
panel.add(jspNotesPane, gbc);
// row 6
gbc.gridy++;
gbc.anchor = GridBagConstraints.EAST;
gbc.fill = GridBagConstraints.NONE;
gbc.gridwidth = 2;
panel.add(new JButton("Select"), gbc);
gbc.weightx = 0;
gbc.gridwidth = 1;
panel.add(new JButton("Cancel") , gbc);
return panel;
}
private JPanel createPanel() {
JPanel jpMain = new JPanel(new GridBagLayout());
jpMain.setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10));
GridBagConstraints gbc = new GridBagConstraints();
gbc.gridx = 0;
gbc.gridy = 0;
gbc.gridwidth = 1;
gbc.weightx = 0.0;
gbc.fill = GridBagConstraints.NONE;
// Not necessary.
// gbc.anchor = GridBagConstraints.NORTHWEST;
JLabel jlLookupText = new JLabel("Lookup text:");
jpMain.add(jlLookupText, gbc);
gbc.gridx = 1;
gbc.gridy = 0;
// The gridwidth is determined by the row with the maximum
// number of components. For this layout there are three
// columns. So each component in row 1 will have gridwidth = 1.
// gbc.gridwidth = 8;
gbc.weightx = 1.0;
gbc.insets = new Insets(0, 5, 0, 5);
gbc.fill = GridBagConstraints.HORIZONTAL;
JTextField jtfLookupText = new JTextField();
jtfLookupText.setAlignmentX(Component.CENTER_ALIGNMENT);
jpMain.add(jtfLookupText, gbc);
// see note about gridwidth above.
gbc.gridx = 2;
// gbc.gridx = 9;
gbc.gridy = 0;
gbc.gridwidth = 1;
gbc.weightx = 0.0;
gbc.insets = new Insets(0, 0, 0, 0);
gbc.fill = GridBagConstraints.NONE;
JButton jbLookup = new JButton("Lookup");
jbLookup.setAlignmentX(Component.CENTER_ALIGNMENT);
jpMain.add(jbLookup, gbc);
gbc.gridx = 0;
gbc.gridy = 1;
gbc.gridwidth = 1;
gbc.weightx = 0.0;
gbc.insets = new Insets(5, 0, 0, 0);
gbc.fill = GridBagConstraints.NONE;
JLabel jlResults = new JLabel("Results:");
jpMain.add(jlResults, gbc);
gbc.gridx = 0;
gbc.gridy = 2;
// This one component spans across three columns:
gbc.gridwidth = 3;
// gbc.gridwidth = 10;
gbc.weightx = 1.0;
gbc.weighty = 1.0;
// This is for a component spanning multiple rows.
// In this layout each component spans one row so
// the gridheight remains at its default value of one.
// gbc.gridheight = 5;
gbc.insets = new Insets(5, 0, 5, 0);
gbc.anchor = GridBagConstraints.CENTER;
gbc.fill = GridBagConstraints.BOTH;
JList jlLookupResults = new JList(new Vector());
jlLookupResults.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
// jlLookupResults.setCellRenderer(new MatchElementRenderer());
JScrollPane jspCustomPane = new JScrollPane(jlLookupResults,
JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED,
JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
jpMain.add(jspCustomPane, gbc);
gbc.gridx = 0;
// In a way similiar to the way GridBagLayout determines
// the number of columns (above) it figures the number
// of rows by the column with the most components in it.
// For this layout there are 6 rows.
gbc.gridy = 3;
// gbc.gridy = 8;
gbc.gridwidth = 1;
gbc.weightx = 0.0;
gbc.weighty = 0.0;
gbc.gridheight = 1;
gbc.insets = new Insets(0, 0, 0, 0);
// The anchor constraint needs a non-zero weight value(s).
// gbc.anchor = GridBagConstraints.SOUTHWEST;
gbc.fill = GridBagConstraints.NONE;
JLabel jlNotes = new JLabel("Notes:");
jpMain.add(jlNotes, gbc);
gbc.gridx = 0;
gbc.gridy = 4;
// gbc.gridy = 9;
gbc.gridwidth = 3;
// gbc.gridwidth = 10;
gbc.weightx = 1.0;
gbc.weighty = 1.0;
// Component only spans a single row. So gbc.gridheight is still 1.
// gbc.gridheight = 3;
gbc.insets = new Insets(5, 0, 5, 0);
// Not necessary.
// gbc.anchor = GridBagConstraints.SOUTHWEST;
// Better to give a size hint here to avoid squirrely behavior.
// gbc.fill = GridBagConstraints.BOTH;
gbc.fill = GridBagConstraints.HORIZONTAL;
JTextArea jtaNotes = new JTextArea(3, 20); // size hint
jtaNotes.setEditable(false);
jtaNotes.setFont(jtfLookupText.getFont());
// jtaNotes.setBackground(getBackground());
// AS_NEEDED is the default for both JScrollBars in JScrollPane.
JScrollPane jspNotesPane = new JScrollPane(jtaNotes);//,
// JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED,
// JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
jpMain.add(jspNotesPane, gbc);
gbc.gridx = 1; // middle column
// gbc.gridx = 8;
gbc.gridy = 5;
// gbc.gridy = 13;
gbc.gridwidth = 1;
gbc.weightx = 0.0;
gbc.weighty = 0.0;
gbc.gridheight = 1;
gbc.insets = new Insets(5, 0, 0, 5);
gbc.anchor = GridBagConstraints.EAST;
gbc.fill = GridBagConstraints.NONE;
JButton jbSelect = new JButton("Select");
jpMain.add(jbSelect, gbc);
gbc.gridx = 2;
// gbc.gridx = 9;
gbc.gridy = 5;
// gbc.gridy = 13;
gbc.gridwidth = 1;
gbc.weightx = 0.0;
gbc.weighty = 0.0;
gbc.gridheight = 1;
gbc.insets = new Insets(5, 0, 0, 0);
gbc.anchor = GridBagConstraints.EAST;
gbc.fill = GridBagConstraints.NONE;
JButton jbCancel = new JButton("Cancel");
jpMain.add(jbCancel, gbc);
return jpMain;
}
/**
* Wait 3 seconds and then add data to the list
* in the tryThis/"another approach" tab to see
* if the components change size.
*/
private void addToVector() {
new Thread(new Runnable() {
public void run() {
try {
Thread.sleep(3000);
} catch(InterruptedException e) {
System.exit(1);
}
Object[] data = new Object[10];
for(int j = 0; j < data.length; j++)
data[j] = "New data for line " + (j+1);
System.out.println("adding data to list");
jlLookupResults.setListData(data);
}
}).start();
}
public static void main(String[] args) {
GBLTest test = new GBLTest();
JDialog d = new JDialog();
d.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
d.add(test.getContent());
d.setSize(500,400);
d.setLocationRelativeTo(null);
d.setVisible(true);
test.addToVector();
}
}
|
|
| Thread Tools |
|
|
| Display Modes |
Linear Mode
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
|