JScrollPane wont go into a JPanel with gridbag - java

my JScroll pane contains a Jlist, and is parented by a JPanel that has the gridbag layour, but it doesn't want to fit right it ends up really small and acts like its gridx is -1 and y is zero, when it should have a x of 0 and y of 1
code:
public class StartGui
{
private static JFrame frame = new JFrame("");
private static JPanel panel = new JPanel();
private static JMenuBar menu = new JMenuBar();
private static DefaultListModel<String> listModel = new DefaultListModel<String>();
private static JList<String> Targets = new JList<String>(listModel);
private static JButton Start = new JButton("Start");
private static JButton Stop = new JButton("Stop");
private static JButton Configure = new JButton("Configure");
private static JButton AddRecipiants = new JButton("Add Recipiants");
private static JProgressBar Progress = new JProgressBar();
private static JLabel RLable = new JLabel("Recipiants:");
private static JScrollPane lsp;
private static GridBagLayout gbl = new GridBagLayout();
private static GridBagConstraints gbc = new GridBagConstraints();
private static JFrame emails = new JFrame();
private static JPanel panel2 = new JPanel();
private static JTextArea emailA = new JTextArea("type email here");
private static JButton done = new JButton("OK");
public static void main(String[] args)
{
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setJMenuBar(menu);
frame.setResizable(false);
frame.setBounds(0, 0, 500, 400);
panel.setLayout(gbl);
listModel.addElement("TestEmailAddress#fake.com");
lsp = new JScrollPane(Targets);
emails.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
emails.setResizable(false);
emails.setBounds(0, 0, 200, 300);
panel2.setLayout(gbl);
emails.add(panel2);
gbc.gridx = 0;
gbc.gridy = 17;
gbc.gridwidth = 20;
gbc.gridheight = 3;
gbc.fill = GridBagConstraints.BOTH;
gbc.weightx = 1;
gbc.weighty = 0;
gbc.anchor = GridBagConstraints.NORTH;
gbc.insets = new Insets(5,70,5,70);
gbl.setConstraints(done, gbc);
panel2.add(done);
gbc.gridx = 0;
gbc.gridy = 0;
gbc.gridwidth = 20;
gbc.gridheight = 17;
gbc.fill = GridBagConstraints.BOTH;
gbc.weightx = 1;
gbc.weighty = 1;
gbc.anchor = GridBagConstraints.NORTH;
gbc.insets = new Insets(10,10,0,10);
gbl.setConstraints(emailA, gbc);
panel2.add(emailA);
emails.add(panel2);
gbc.gridx = 0;
gbc.gridy = 1;
gbc.gridwidth = 10;
gbc.gridheight = 19;
gbc.fill = GridBagConstraints.BOTH;
gbc.weightx = 1;
gbc.weighty = 1;
gbc.anchor = GridBagConstraints.WEST;
gbc.insets = new Insets(5,5,5,5);
gbl.setConstraints(Targets, gbc);
panel.add(lsp);
gbc.gridx = 10;
gbc.gridy = 11;
gbc.gridwidth = 5;
gbc.gridheight = 5;
gbc.fill = GridBagConstraints.BOTH;
gbc.weightx = 1;
gbc.weighty = 1;
gbc.anchor = GridBagConstraints.CENTER;
gbc.insets = new Insets(25,5,25,5);
gbl.setConstraints(Start, gbc);
panel.add(Start);
gbc.gridx = 15;
gbc.gridy = 11;
gbc.gridwidth = 5;
gbc.gridheight = 5;
gbc.fill = GridBagConstraints.BOTH;
gbc.weightx = 1;
gbc.weighty = 1;
gbc.anchor = GridBagConstraints.CENTER;
gbc.insets = new Insets(25,5,25,5);
gbl.setConstraints(Stop, gbc);
panel.add(Stop);
gbc.gridx = 10;
gbc.gridy = 4;
gbc.gridwidth = 10;
gbc.gridheight = 7;
gbc.fill = GridBagConstraints.BOTH;
gbc.weightx = 1;
gbc.weighty = 1;
gbc.anchor = GridBagConstraints.CENTER;
gbc.insets = new Insets(35,5,40,5);
gbl.setConstraints(Configure, gbc);
panel.add(Configure);
gbc.gridx = 10;
gbc.gridy = 0;
gbc.gridwidth = 10;
gbc.gridheight = 4;
gbc.fill = GridBagConstraints.BOTH;
gbc.weightx = 1;
gbc.weighty = 1;
gbc.anchor = GridBagConstraints.CENTER;
gbc.insets = new Insets(25,5,20,5);
gbl.setConstraints(AddRecipiants, gbc);
panel.add(AddRecipiants);
Progress.setMaximum(100);
Progress.setString("0%");
Progress.setValue(0);
gbc.gridx = 10;
gbc.gridy = 16;
gbc.gridwidth = 10;
gbc.gridheight = 4;
gbc.fill = GridBagConstraints.BOTH;
gbc.weightx = 1;
gbc.weighty = 1;
gbc.anchor = GridBagConstraints.CENTER;
gbc.insets = new Insets(25,5,15,5);
gbl.setConstraints(Progress, gbc);
panel.add(Progress);
gbc.gridx = 0;
gbc.gridy = 0;
gbc.gridwidth = 10;
gbc.gridheight = 1;
gbc.fill = GridBagConstraints.BOTH;
gbc.weightx = 1;
gbc.weighty = 1;
gbc.anchor = GridBagConstraints.CENTER;
gbc.insets = new Insets(5,45,0,45);
gbl.setConstraints(RLable, gbc);
panel.add(RLable);
frame.add(panel);
frame.setVisible(true);
}

The JScrollPane preferred size isn't typically very large, and is generally determine from the Scrollable interface (and other factors)
You can affect the JList's default size using the following methods...
JList#setVisibleRowCount
JList#setPrototypeCellValue
The first will effect the number of rows that are visible in the view port and the second will effect the height and width of that row.
You are also NOT passing in constraints to the layout manager for the scroll pane (you are trying to use the JList) which isn't going to work...
You should be doing this...
panel.add(lsp, gbc);
And I would greatly discourage this...
gbl.setConstraints(Targets, gbc);
(I should add, I would discourage the above generally, and recommend using add(Component, Object) - But that's me)
Updated
My suggestions, vs your default code...
public class BadLayout10 {
public static void main(String[] args) {
new BadLayout10();
}
public BadLayout10() {
EventQueue.invokeLater(new Runnable() {
#Override
public void run() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (Exception ex) {
}
new StartGui();
}
});
}
public class StartGui {
private JFrame frame = new JFrame("");
private JPanel panel = new JPanel();
private JMenuBar menu = new JMenuBar();
private DefaultListModel<String> listModel = new DefaultListModel<String>();
private JList<String> Targets = new JList<String>(listModel);
private JButton Start = new JButton("Start");
private JButton Stop = new JButton("Stop");
private JButton Configure = new JButton("Configure");
private JButton AddRecipiants = new JButton("Add Recipiants");
private JProgressBar Progress = new JProgressBar();
private JLabel RLable = new JLabel("Recipiants:");
private JScrollPane lsp;
private GridBagLayout gbl = new GridBagLayout();
private GridBagConstraints gbc = new GridBagConstraints();
private JFrame emails = new JFrame();
private JPanel panel2 = new JPanel();
private JTextArea emailA = new JTextArea("type email here");
private JButton done = new JButton("OK");
public StartGui() {
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setJMenuBar(menu);
frame.setResizable(false);
frame.setBounds(0, 0, 500, 400);
panel.setLayout(gbl);
listModel.addElement("TestEmailAddress#fake.com");
Targets.setVisibleRowCount(10);
Targets.setPrototypeCellValue("This is a really long test string");
lsp = new JScrollPane(Targets);
emails.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
emails.setResizable(false);
emails.setBounds(0, 0, 200, 300);
panel2.setLayout(gbl);
emails.add(panel2);
gbc.gridx = 0;
gbc.gridy = 17;
gbc.gridwidth = 20;
gbc.gridheight = 3;
gbc.fill = GridBagConstraints.BOTH;
gbc.weightx = 1;
gbc.weighty = 0;
gbc.anchor = GridBagConstraints.NORTH;
gbc.insets = new Insets(5, 70, 5, 70);
gbl.setConstraints(done, gbc);
panel2.add(done);
gbc.gridx = 0;
gbc.gridy = 0;
gbc.gridwidth = 20;
gbc.gridheight = 17;
gbc.fill = GridBagConstraints.BOTH;
gbc.weightx = 1;
gbc.weighty = 1;
gbc.anchor = GridBagConstraints.NORTH;
gbc.insets = new Insets(10, 10, 0, 10);
gbl.setConstraints(emailA, gbc);
panel2.add(emailA);
emails.add(panel2);
gbc.gridx = 0;
gbc.gridy = 1;
gbc.gridwidth = 10;
gbc.gridheight = 19;
gbc.fill = GridBagConstraints.BOTH;
gbc.weightx = 1;
gbc.weighty = 1;
gbc.anchor = GridBagConstraints.WEST;
gbc.insets = new Insets(5, 5, 5, 5);
// gbl.setConstraints(Targets, gbc);
panel.add(lsp, gbc);
gbc.gridx = 10;
gbc.gridy = 11;
gbc.gridwidth = 5;
gbc.gridheight = 5;
gbc.fill = GridBagConstraints.BOTH;
gbc.weightx = 1;
gbc.weighty = 1;
gbc.anchor = GridBagConstraints.CENTER;
gbc.insets = new Insets(25, 5, 25, 5);
gbl.setConstraints(Start, gbc);
panel.add(Start);
gbc.gridx = 15;
gbc.gridy = 11;
gbc.gridwidth = 5;
gbc.gridheight = 5;
gbc.fill = GridBagConstraints.BOTH;
gbc.weightx = 1;
gbc.weighty = 1;
gbc.anchor = GridBagConstraints.CENTER;
gbc.insets = new Insets(25, 5, 25, 5);
gbl.setConstraints(Stop, gbc);
panel.add(Stop);
gbc.gridx = 10;
gbc.gridy = 4;
gbc.gridwidth = 10;
gbc.gridheight = 7;
gbc.fill = GridBagConstraints.BOTH;
gbc.weightx = 1;
gbc.weighty = 1;
gbc.anchor = GridBagConstraints.CENTER;
gbc.insets = new Insets(35, 5, 40, 5);
gbl.setConstraints(Configure, gbc);
panel.add(Configure);
gbc.gridx = 10;
gbc.gridy = 0;
gbc.gridwidth = 10;
gbc.gridheight = 4;
gbc.fill = GridBagConstraints.BOTH;
gbc.weightx = 1;
gbc.weighty = 1;
gbc.anchor = GridBagConstraints.CENTER;
gbc.insets = new Insets(25, 5, 20, 5);
gbl.setConstraints(AddRecipiants, gbc);
panel.add(AddRecipiants);
Progress.setMaximum(100);
Progress.setString("0%");
Progress.setValue(0);
gbc.gridx = 10;
gbc.gridy = 16;
gbc.gridwidth = 10;
gbc.gridheight = 4;
gbc.fill = GridBagConstraints.BOTH;
gbc.weightx = 1;
gbc.weighty = 1;
gbc.anchor = GridBagConstraints.CENTER;
gbc.insets = new Insets(25, 5, 15, 5);
gbl.setConstraints(Progress, gbc);
panel.add(Progress);
gbc.gridx = 0;
gbc.gridy = 0;
gbc.gridwidth = 10;
gbc.gridheight = 1;
gbc.fill = GridBagConstraints.BOTH;
gbc.weightx = 1;
gbc.weighty = 1;
gbc.anchor = GridBagConstraints.CENTER;
gbc.insets = new Insets(5, 45, 0, 45);
gbl.setConstraints(RLable, gbc);
panel.add(RLable);
frame.add(panel);
frame.setVisible(true);
}
}
}

Related

Turn on design mode

I have old Java NetBenas project that contains UI classes derived from JPanel. Code editor does not contain Design button in these classes. Design button appears only in newly created JPanel fom. Is it possible somehow tell NetBeans to open old forms in Design mode?
Form that can't be opened in Design mode:
public class About extends javax.swing.JPanel /*JFrame*/ {
private ClassLoader cl = getClass().getClassLoader();
public ImageIcon logo = new ImageIcon(
cl.getResource("icons/logo.gif"));
public ImageIcon ball = new ImageIcon(
cl.getResource("icons/ball.gif"));
private JPanel centerPanel;
private JPanel topPanel;
private JLabel productName = new JLabel("<html><font face=\"verdana\" size=10>"+
"SocketTest",logo,JLabel.CENTER);
private JTextArea readme = new JTextArea();
private JScrollPane jsp;
String html="<html><font face=\"verdana\" size=\"2\">";
private JLabel versionText = new JLabel(html+"Version",ball,JLabel.LEFT);
private JLabel version = new JLabel(html+": 3.0.0", JLabel.LEFT);
private JLabel licenceText = new JLabel(html+"Licence",ball,JLabel.LEFT);
private JLabel licence = new JLabel(html+": GNU Lesser General Public License", JLabel.LEFT);
private JLabel authorText = new JLabel(html+"Author",ball,JLabel.LEFT);
private JLabel author = new JLabel(html+": Akshathkumar Shetty", JLabel.LEFT);
private JLabel copyrightText = new JLabel(html+"Copyright © 2003-2008 Akshathkumar Shetty",ball,JLabel.LEFT);
private JLabel websiteText = new JLabel(html+"Website",ball,JLabel.LEFT);
private JLabel website = new JLabel(html+": http://sockettest.sourceforge.net/", JLabel.LEFT);
private JLabel readmeText = new JLabel(html+"ReadMe",ball,JLabel.LEFT);
private GridBagConstraints gbc = new GridBagConstraints();
/** Creates a new instance of About */
public About() {
//Container cp = getContentPane();
Container cp = this;
topPanel = new JPanel();
topPanel.setLayout(new GridBagLayout());
gbc.insets = new Insets( 2, 2, 2, 2 );
gbc.weighty = 0.0;
gbc.weightx = 0.0;
gbc.gridx = 0;
gbc.gridy = 0;
gbc.gridheight = 1;
gbc.gridwidth = 3;
gbc.anchor = GridBagConstraints.CENTER;
gbc.fill = GridBagConstraints.NONE;
topPanel.add(productName, gbc);
gbc.anchor = GridBagConstraints.WEST;
gbc.fill = GridBagConstraints.HORIZONTAL;
gbc.gridy = 1;
gbc.gridx = 0;
gbc.weightx = 0.0;
topPanel.add(versionText, gbc);
gbc.gridx = 1;
topPanel.add(version, gbc);
gbc.gridx = 2;
gbc.weightx = 1.0;
gbc.fill = GridBagConstraints.HORIZONTAL;
topPanel.add(Box.createHorizontalGlue(), gbc);
gbc.gridy = 2;
gbc.gridx = 0;
gbc.weightx = 0.0;
topPanel.add(licenceText, gbc);
gbc.gridx = 1;
topPanel.add(licence, gbc);
gbc.gridx = 2;
gbc.weightx = 1.0;//1.0
gbc.fill = GridBagConstraints.HORIZONTAL;
topPanel.add(new JLabel(), gbc);
gbc.gridy = 3;
gbc.gridx = 0;
gbc.weightx = 0.0;
topPanel.add(authorText, gbc);
gbc.gridx = 1;
topPanel.add(author, gbc);
gbc.gridx = 2;
gbc.weightx = 1.0;
gbc.fill = GridBagConstraints.HORIZONTAL;
topPanel.add(Box.createHorizontalGlue(), gbc);
gbc.gridy = 4;
gbc.gridx = 0;
gbc.weightx = 0.0;
topPanel.add(websiteText, gbc);
gbc.gridx = 1;
topPanel.add(website, gbc);
gbc.gridx = 2;
gbc.weightx = 1.0;
gbc.fill = GridBagConstraints.HORIZONTAL;
topPanel.add(Box.createHorizontalGlue(), gbc);
gbc.gridy = 5;
gbc.gridx = 0;
gbc.weightx = 0.0;
gbc.gridwidth = 2;
topPanel.add(copyrightText, gbc);
gbc.gridwidth = 1;
gbc.gridx = 2;
gbc.weightx = 1.0;
gbc.fill = GridBagConstraints.HORIZONTAL;
topPanel.add(Box.createHorizontalGlue(), gbc);
gbc.gridy = 6;
gbc.gridx = 0;
gbc.weightx = 0.0;
gbc.gridwidth = 1;
topPanel.add(readmeText, gbc);
gbc.gridx = 1;
topPanel.add(new JLabel(" "), gbc);
gbc.gridx = 2;
gbc.weightx = 1.0;
gbc.fill = GridBagConstraints.HORIZONTAL;
topPanel.add(Box.createHorizontalGlue(), gbc);
centerPanel = new JPanel();
readme.setText("Loading... readme");
try {
String cont = Util.readFile("readme.txt",(Object)About.this);
readme.setText(cont);
} catch (IOException e){
System.err.println("Error reading readme.txt "+e);
readme.append("\r\nFailed : "+e.getMessage());
}
readme.setEditable(false);
readme.setLineWrap(true);
readme.setWrapStyleWord(true);
jsp = new JScrollPane(readme);
centerPanel.setLayout(new BorderLayout());
centerPanel.add(jsp);
centerPanel.setBorder(BorderFactory.createEmptyBorder(0,9,0,9));
cp.setLayout(new BorderLayout(0,10));
cp.add(topPanel,BorderLayout.NORTH);
cp.add(centerPanel,BorderLayout.CENTER);
setBorder(BorderFactory.createEmptyBorder(10,10,10,10));
}
}

Component's Sizes in GridBagLayout

I'm having major issues aligning the components in GridBagLayout, yes I've read the tutorial by Oracle and tried changing the weightx.
This is how it looks like currently :
Basically what I need to achieve is:
JTextFields "Nome" and "Filiação" to stretch all the way to the left, just like "Idade" and "Turma"
Bottom JButtons to be the same size, aligned in the middle.
I hope someone can point what I'm missing here.
Here's a sorta SSCCE:
package test1;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
public class Test1 {
public static void main(String[] args) {
JFrame jf = new JFrame("Test");
JPanel jp = new JPanel(new GridBagLayout());
GridBagConstraints gbc = new GridBagConstraints();
gbc.insets = new Insets(10, 10, 10, 10);
JLabel labelNome = new JLabel("Nome:");
gbc.gridx = 0;
gbc.gridy = 0;
gbc.fill = GridBagConstraints.BOTH;
jp.add(labelNome, gbc);
JTextField tfNome = new JTextField();
gbc.gridx = 1;
gbc.ipadx = 50;
gbc.fill = GridBagConstraints.HORIZONTAL;
jp.add(tfNome, gbc);
JLabel labelIdade = new JLabel("Idade :");
gbc.ipadx = 0;
gbc.gridx = 2;
gbc.fill = GridBagConstraints.NONE;
jp.add(labelIdade, gbc);
JTextField tfIdade = new JTextField();
gbc.gridx = 3;
gbc.ipadx = 50;
gbc.fill = GridBagConstraints.HORIZONTAL;
jp.add(tfIdade, gbc);
JLabel labelEndereco = new JLabel("Endereço :");
gbc.ipadx = 0;
gbc.gridx = 0;
gbc.gridy = 1;
gbc.fill = GridBagConstraints.BOTH;
jp.add(labelEndereco, gbc);
JTextField tfEndereco = new JTextField();
gbc.ipadx = 50;
gbc.gridx = 1;
gbc.gridwidth = 3;
gbc.fill = GridBagConstraints.HORIZONTAL;
jp.add(tfEndereco, gbc);
JLabel labelFiliacao = new JLabel("Filiação :");
gbc.ipadx = 0;
gbc.gridwidth = 1;
gbc.gridx = 0;
gbc.gridy = 2;
gbc.fill = GridBagConstraints.BOTH;
jp.add(labelFiliacao, gbc);
JTextField tfFiliacao = new JTextField();
gbc.gridx = 1;
gbc.ipadx = 50;
gbc.fill = GridBagConstraints.HORIZONTAL;
jp.add(tfFiliacao, gbc);
JLabel labelTurma = new JLabel("Turma :");
gbc.ipadx = 0;
gbc.gridx = 2;
gbc.fill = GridBagConstraints.BOTH;
jp.add(labelTurma, gbc);
JTextField tfTurma = new JTextField();
gbc.gridx = 3;
gbc.ipadx = 50;
gbc.fill = GridBagConstraints.HORIZONTAL;
jp.add(tfTurma, gbc);
JLabel labelDisciplina = new JLabel("Disciplina :");
gbc.ipadx = 0;
gbc.gridx = 0;
gbc.gridy = 3;
gbc.fill = GridBagConstraints.BOTH;
jp.add(labelDisciplina, gbc);
JTextField tfDisciplina = new JTextField();
gbc.ipadx = 50;
gbc.ipady = 0;
gbc.gridx = 1;
gbc.gridwidth = 3;
gbc.fill = GridBagConstraints.HORIZONTAL;
jp.add(tfDisciplina, gbc);
JButton adicionaDisciplina = new JButton("Adicionar disciplina");
gbc.ipadx = 0;
gbc.gridwidth = 2;
gbc.gridx = 0;
gbc.gridy = 4;
jp.add(adicionaDisciplina, gbc);
JButton limparDisciplina = new JButton("Limpar lista de disciplinas");
gbc.gridx = 2;
jp.add(limparDisciplina, gbc);
JButton botaoSalvar = new JButton("Salvar");
gbc.gridx = 0;
gbc.gridy = 5;
jp.add(botaoSalvar, gbc);
JButton botaoCancelar = new JButton("Cancelar");
gbc.gridx = 2;
jp.add(botaoCancelar, gbc);
jf.setSize(500, 550);
jf.add(jp);
jf.setVisible(true);
}
}
JTextFields "Nome" and "Filiação" to stretch all the way to the left, just like "Idade" and "Turma"
Don't know what that means. They are aligned to the left just like the other text fields in that second column.
They may not appear as far left as the other text fields because the "Endereco" label length is longer, so the second column can only start where that ends.
If you want the text fields to have the same size then you need to specify the size of your text field and the GridBagLayout will respect the size. You do this by specifying the columns in the text field when you create the text field:
JTextField tfNome = new JTextField(10);

GridBagLayout fail to work on gridy

I have a problem with getting the GridBagLayout work as expected.
The gridy of the GridBagConstraints didn't seem to function as expected at all for atomIndexLabel (JLabel), atomIndexExample (JLabel) and atomIndexAddField (JTextField). I want to align them to approximately the middle of the table, but when running, they stick to the top, just like gridy is set to 0, 1 and 2.
I tried to add an emptyElement, but it didn't work. My code is below. Any suggestions?
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.border.*;
import javax.swing.table.DefaultTableModel;
public class Test extends JPanel {
private static final long serialVersionUID = -7810531559762481489L;
private JLabel atomIndexLabel, atomIndexExample, atomIndexNotification;
private JTextField atomIndexAddField;
private JButton atomIndexAddButton, atomIndexRemoveButton;
private AtomIndexButtonListener atomIndexButtonListener;
private JTable atomIndexTable;
private JScrollPane atomIndexTableScrollPane;
private DefaultTableModel atomIndexTableModel;
public Test() {
this.atomIndexLabel = new JLabel("Input one or multiple atom indexs:");
this.atomIndexExample = new JLabel("Example: 1 2 3 4 5");
this.atomIndexAddField = new JTextField(20);
this.atomIndexAddButton = new JButton("Add Atom Index");
this.atomIndexTable = new JTable(1, 1);
this.atomIndexTableModel = new DefaultTableModel() {
private static final long serialVersionUID = -6458638313466319330L;
#Override
public boolean isCellEditable(int row, int column) {
return false;
}
};
this.atomIndexTable.setModel(atomIndexTableModel);
this.atomIndexTableModel.addColumn("Atom_Index");
this.atomIndexTableScrollPane = new JScrollPane(atomIndexTable);
this.atomIndexTableScrollPane.setPreferredSize(new Dimension(100, 170));
this.atomIndexRemoveButton = new JButton("Remove Selected");
this.atomIndexNotification = new JLabel("No input atom index");
this.atomIndexNotification.setHorizontalAlignment(JLabel.CENTER);
setupLayout();
this.atomIndexButtonListener = new AtomIndexButtonListener();
this.atomIndexAddButton.addActionListener(atomIndexButtonListener);
this.atomIndexRemoveButton.addActionListener(atomIndexButtonListener);
this.setBorder(new TitledBorder("Atom Index Input"));
}
private void setupLayout() {
this.setLayout(new GridBagLayout());
GridBagConstraints gbc = new GridBagConstraints();
/*JComponent emptyElement = new JLabel("");
gbc.gridx = 0;
gbc.gridy = 0;
gbc.gridwidth = 1;
gbc.gridheight = 6;
gbc.anchor = GridBagConstraints.LAST_LINE_START;
this.add(emptyElement, gbc);*/
gbc.gridx = 0;
gbc.gridy = 6;
gbc.gridwidth = 4;
gbc.gridheight = 1;
gbc.anchor = GridBagConstraints.FIRST_LINE_START;
this.add(atomIndexLabel, gbc);
gbc.gridx = 0;
gbc.gridy = 7;
gbc.gridwidth = 2;
gbc.gridheight = 1;
gbc.anchor = GridBagConstraints.FIRST_LINE_START;
this.add(atomIndexExample, gbc);
gbc.gridx = 0;
gbc.gridy = 8;
gbc.gridwidth = 4;
gbc.gridheight = 1;
gbc.anchor = GridBagConstraints.FIRST_LINE_START;
this.add(atomIndexAddField, gbc);
gbc.gridx = 0;
gbc.gridy = 11;
gbc.gridwidth = 4;
gbc.gridheight = 1;
gbc.anchor = GridBagConstraints.CENTER;
gbc.insets = new Insets(10, 0, 0, 0);
this.add(atomIndexAddButton, gbc);
gbc.gridx = 5;
gbc.gridy = 0;
gbc.gridwidth = 6;
gbc.gridheight = 12;
gbc.insets = new Insets(10, 10, 0, 0);
this.add(atomIndexTableScrollPane, gbc);
gbc.gridx = 5;
gbc.gridy = 12;
gbc.gridwidth = 6;
gbc.gridheight = 1;
gbc.insets = new Insets(10, 10, 0, 0);
this.add(atomIndexRemoveButton, gbc);
gbc.gridx = 0;
gbc.gridy = 13;
gbc.gridwidth = 10;
gbc.gridheight = 1;
gbc.insets = new Insets(20, 0, 0, 0);
this.add(atomIndexNotification, gbc);
}
private class AtomIndexButtonListener implements ActionListener {
#Override
public void actionPerformed(ActionEvent e) {
System.out.println("For Test only");
}
}
public static void main(String[] args) {
JFrame frame = new JFrame();
frame.add(new Test());
frame.setSize(1000, 900);
frame.setResizable(false);
frame.setLocationRelativeTo(null);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().setLayout(new FlowLayout());
frame.setVisible(true);
}
}

How to tell GridBagLayout not to resize my components

I have JPanel inside vertical JSplitPane. JPanel contains jlabels and jtextfields. When I shrink height of JPanel by moving JSplitPane's divider All components in jpanel are resized themselves. How to tell them not to resize when height of parent jpanel shrinks.
I cannot size minSize for JPanel because it makes impossible for JSplitPane to move divider.
JSplitPane splitPane = new JSplitPane(JSplitPane.VERTICAL_SPLIT);
splitPane.setDividerSize(10);
splitPane.setOneTouchExpandable(true);
JPanel searchPanel = createSearchPanel();
splitPane.setDividerLocation(searchPanel.getPreferredSize().height + 5);
splitPane.setTopComponent(searchPanel);
As you see there is searchPanel. Let's see it:
JPanel searchPanel = new JPanel(new GridBagLayout()) {
Dimension minSize = new Dimension(200, 0);
#Override
public Dimension getMinimumSize() {
return minSize;
}
};
searchPanel.setBorder(BorderFactory.createTitledBorder("Search query"));
GridBagConstraints gbc = new GridBagConstraints(0, 0, 1, 1, 0, 0,
GridBagConstraints.LINE_START, GridBagConstraints.HORIZONTAL,
new Insets(5, 2, 5, 3), 0, 0);
searchPanel.add(eventLabel, gbc);
gbc.gridx = 1;
gbc.gridwidth = 3;
searchPanel.add(eventLabelField, gbc);
gbc.gridx = 0;
gbc.gridy = 1;
gbc.gridwidth = 1;
searchPanel.add(timestampLabel, gbc);
gbc.gridx = 1;
searchPanel.add(timestampStartField, gbc);
gbc.gridx = 2;
searchPanel.add(timestammpToLabel, gbc);
gbc.gridx = 3;
searchPanel.add(timestampEndField, gbc);
gbc.gridx = 0;
gbc.gridy = 2;
searchPanel.add(locationLabel, gbc);
gbc.gridx = 1;
gbc.gridy = 2;
gbc.gridwidth = 3;
searchPanel.add(locationField, gbc);
gbc.gridx = 0;
gbc.gridy = 3;
gbc.gridwidth = 1;
searchPanel.add(durationMagnitudeLabel, gbc);
gbc.gridx = 1;
gbc.gridy = 3;
searchPanel.add(durationMagnitudeMinField, gbc);
gbc.gridx = 2;
searchPanel.add(durationToLabel, gbc);
gbc.gridx = 3;
searchPanel.add(durationMagnitudeMaxField, gbc);
gbc.gridx = 1;
gbc.gridy = 4;
gbc.gridwidth = 2;
gbc.anchor = GridBagConstraints.PAGE_END;
gbc.weighty = 1.0;
gbc.ipadx = 2;
gbc.ipady = 2;
gbc.insets = new Insets(15, 0, 0, 0);
searchPanel.add(searchButton, gbc);
When I move up vertical divider the height of searchPanel shrinks and components look like:
You see that jtextFields got much smaller than they were after I moved divider up
Please, help me here.
I have changed your code. Use weightx property of GridBagConstraint with GridBagConstraints.HORIZONTAL fill, it helps components to fill their cell properly.
Example code:
JPanel searchPanel = new JPanel(new GridBagLayout()) {
Dimension minSize = new Dimension(200, 0);
#Override
public Dimension getMinimumSize() {
return minSize;
}
};
searchPanel.setBorder(BorderFactory.createTitledBorder("Search query"));
GridBagConstraints gbc = new GridBagConstraints(0, 0, 1, 1, 0, 0,
GridBagConstraints.LINE_START, GridBagConstraints.HORIZONTAL,
new Insets(5, 2, 5, 3), 0, 0);
searchPanel.add(new JLabel("1"), gbc);
gbc.gridx = 1;
gbc.gridwidth = 3;
gbc.weightx = 1;
searchPanel.add(new JTextField(), gbc);
gbc.gridx = 0;
gbc.gridy = 1;
gbc.gridwidth = 1;
gbc.weightx = 0;
searchPanel.add(new JLabel("1"), gbc);
gbc.gridx = 1;
gbc.weightx = 1;
searchPanel.add(new JTextField(), gbc);
gbc.gridx = 2;
gbc.weightx = 0;
searchPanel.add(new JLabel("1"), gbc);
gbc.gridx = 3;
gbc.weightx = 1;
searchPanel.add(new JTextField(), gbc);
gbc.gridx = 0;
gbc.gridy = 2;
gbc.weightx = 0;
searchPanel.add(new JLabel("1"), gbc);
gbc.gridx = 1;
gbc.gridy = 2;
gbc.gridwidth = 3;
gbc.weightx = 1;
searchPanel.add(new JTextField(), gbc);
gbc.gridx = 0;
gbc.gridy = 3;
gbc.gridwidth = 1;
gbc.weightx = 0;
searchPanel.add(new JLabel("1"), gbc);
gbc.gridx = 1;
gbc.gridy = 3;
gbc.weightx = 1;
searchPanel.add(new JTextField(), gbc);
gbc.gridx = 2;
gbc.weightx = 0;
searchPanel.add(new JLabel("1"), gbc);
gbc.gridx = 3;
gbc.weightx = 1;
searchPanel.add(new JTextField(), gbc);
gbc.gridx = 1;
gbc.gridy = 4;
gbc.gridwidth = 2;
gbc.anchor = GridBagConstraints.PAGE_END;
gbc.weighty = 1.0;
gbc.ipadx = 2;
gbc.ipady = 2;
gbc.insets = new Insets(15, 0, 0, 0);
gbc.weightx = 0;
searchPanel.add(new JButton("b"), gbc);
return searchPanel;
Watch docs How to use GridBagLayout
Results:
Edit: Sorry, I'm using awful names for labels)
Add a dummy JPanel to the bottom and set weightY=1 for the JPanel. ALl the rest component should have weightY=0. Thus on height increasing all the additional pixels are targeted to the dummy JPanel.

How can I make a component span multiple cells in a GridBagLayout

I have to make this for school:
This is the code I have so far:
import javax.swing.*;
import java.awt.*;
public class AddressBookGui1 extends JFrame {
public AddressBookGui1(){
GridBagLayout gbl = new GridBagLayout();
GridBagConstraints gbc = new GridBagConstraints();
setLayout(gbl);
JLabel label;
JButton button;
JTextField textField;
JTextArea textArea = new JTextArea(10, 20);
gbc.weightx = 1;
label = new JLabel("text");
gbc.fill = GridBagConstraints.HORIZONTAL;
gbc.gridx = 0;
gbc.gridy = 0;
add(label ,gbc);
textField = new JTextField();
gbc.weightx = 1;
gbc.fill = GridBagConstraints.HORIZONTAL;
gbc.gridx = 1;
gbc.gridy = 0;
add(textField ,gbc);
label = new JLabel("text");
gbc.weightx = 1;
gbc.fill = GridBagConstraints.HORIZONTAL;
gbc.gridx = 0;
gbc.gridy = 1;
gbc.gridwidth = 1;
add(label ,gbc);
textField = new JTextField();
gbc.weightx = 1;
gbc.fill = GridBagConstraints.HORIZONTAL;
gbc.gridx = 1;
gbc.gridy = 1;
gbc.gridwidth = 2;
add(textField, gbc);
label = new JLabel("text");
gbc.weightx = 1;
gbc.fill = GridBagConstraints.HORIZONTAL;
gbc.gridx = 0;
gbc.gridy = 2;
gbc.gridwidth = 1;
add(label ,gbc);
textField = new JTextField();
gbc.weightx = 1;
gbc.fill = GridBagConstraints.HORIZONTAL;
gbc.gridx = 1;
gbc.gridy = 2;
gbc.gridwidth = 2;
add(textField, gbc);
label = new JLabel("text");
gbc.weightx = 1;
gbc.anchor = GridBagConstraints.FIRST_LINE_START;
gbc.fill = GridBagConstraints.HORIZONTAL;
gbc.gridx = 0;
gbc.gridy = 3;
gbc.gridwidth = 1;
add(label ,gbc);
gbc.weightx = 1;
gbc.fill = GridBagConstraints.HORIZONTAL;
gbc.anchor = GridBagConstraints.CENTER;
gbc.gridwidth = 2;
gbc.gridx = 1;
gbc.gridy = 3;
add(textArea, gbc);
gbc.weightx = 1;
button = new JButton("text");
gbc.fill = GridBagConstraints.HORIZONTAL;
gbc.gridwidth = 1;
gbc.gridx = 0;
gbc.gridy = 4;
add(button ,gbc);
gbc.weightx = 1;
button = new JButton("text");
gbc.fill = GridBagConstraints.HORIZONTAL;
gbc.gridx = 3;
gbc.gridy = 4;
add(button ,gbc);
}
public static void main(String[] args){
AddressBookGui1 frame = new AddressBookGui1();
frame.setTitle("Address Book");
frame.setSize(400, 300);
frame.setLocationRelativeTo(null);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
}
(I still need to deal with padding and insets. I've gotten those to work in a much simpler program so I think I have a handle on that stuff)
I've tried the GridBagLayout Oracle tutorial and I'm not sure what I'm doing wrong. Can someone help me make it look more like it is supposed to? Specifically to make the text fields and text area span over 2 cells.
Few things I noticed about your code.
Do not use setSize() of JFrame. This will cause abnormal behavior.
Instead, let the frame size itself according to the size of its
components. If you want the frame to be bigger, adjust not the size
of the frame but the components inside it. You can either
setpreferredSize or override the getpreferredsize of the component if
you really want to adjust is size since GridBagLayout is one of those layout managers that respects the
preferredSize of the component. Use pack() to remove the unecessary
space.
Do not extend a JFrame, make your UI class to have a main panel and
add all the components there. provide a getter for that panel (e.g.
getUI()) for extracting the UI of that class.
Always reinstantiate the GridBagConstraints object whenever it is
going to be applied to another component. This way it is more
readable.
Use insets to put padding around the component.
Do not reuse same reference to different components;
Use Initial Thread
This is not standard but it I find it really helpful when working
with GridBagLayout, in setting the constraints of gbc, make it in
alphabetical order.
To solve your problem, here is the modified code with the things I pointed out about applied.
public class AddressBook {
private JPanel pnlMain;
public AddressBook() {
pnlMain = new JPanel();
pnlMain.setLayout(new GridBagLayout());
GridBagConstraints gbc = new GridBagConstraints();
JLabel lblName = new JLabel("Name");
gbc.fill = GridBagConstraints.HORIZONTAL;
gbc.gridx = 0;
gbc.gridy = 0;
gbc.insets = new Insets(0, 10, 0, 0);
gbc.weightx = 1;
pnlMain.add(lblName, gbc);
JTextField txtName = new JTextField();
gbc = new GridBagConstraints();
gbc.fill = GridBagConstraints.HORIZONTAL;
gbc.gridwidth = 3;
gbc.gridx = 1;
gbc.gridy = 0;
gbc.insets = new Insets(5, 0, 0, 10);
gbc.weightx = 1;
pnlMain.add(txtName, gbc);
JLabel lblPhone = new JLabel("Phone");
gbc = new GridBagConstraints();
gbc.fill = GridBagConstraints.HORIZONTAL;
gbc.gridwidth = 1;
gbc.gridx = 0;
gbc.gridy = 1;
gbc.insets = new Insets(0, 10, 0, 0);
gbc.weightx = 1;
pnlMain.add(lblPhone, gbc);
JTextField txtPhone = new JTextField();
gbc = new GridBagConstraints();
gbc.fill = GridBagConstraints.HORIZONTAL;
gbc.gridwidth = 3;
gbc.gridx = 1;
gbc.gridy = 1;
gbc.insets = new Insets(5, 0, 0, 10);
gbc.weightx = 1;
pnlMain.add(txtPhone, gbc);
JLabel lblEmail = new JLabel("Email");
gbc = new GridBagConstraints();
gbc.fill = GridBagConstraints.HORIZONTAL;
gbc.gridwidth = 1;
gbc.gridx = 0;
gbc.gridy = 2;
gbc.insets = new Insets(0, 10, 0, 0);
gbc.weightx = 1;
pnlMain.add(lblEmail, gbc);
JTextField txtEmail = new JTextField();
gbc = new GridBagConstraints();
gbc.fill = GridBagConstraints.HORIZONTAL;
gbc.gridwidth = 3;
gbc.gridx = 1;
gbc.gridy = 2;
gbc.weightx = 1;
gbc.insets = new Insets(5, 0, 0, 10);
pnlMain.add(txtEmail, gbc);
JLabel lblAddress = new JLabel("Address");
gbc = new GridBagConstraints();
gbc.fill = GridBagConstraints.HORIZONTAL;
gbc.gridwidth = 1;
gbc.gridx = 0;
gbc.gridy = 3;
gbc.insets = new Insets(0, 10, 0, 0);
gbc.weightx = 1;
pnlMain.add(lblAddress, gbc);
JTextArea txtAreaAddress = new JTextArea(10, 20);
JScrollPane pane = new JScrollPane(txtAreaAddress);
gbc = new GridBagConstraints();
gbc.anchor = GridBagConstraints.NORTH;
gbc.fill = GridBagConstraints.BOTH;
gbc.gridwidth = 3;
gbc.gridx = 1;
gbc.gridy = 3;
gbc.insets = new Insets(5, 0, 0, 10);
gbc.weightx = 1;
pnlMain.add(pane, gbc);
JButton btnSave = new JButton("Save");
gbc = new GridBagConstraints();
gbc.anchor = GridBagConstraints.WEST;
gbc.fill = GridBagConstraints.NONE;
gbc.gridwidth = 1;
gbc.gridx = 0;
gbc.gridy = 4;
gbc.insets = new Insets(10, 10, 10, 0);
gbc.weightx = 1;
pnlMain.add(btnSave, gbc);
JButton btnCancel = new JButton("Cancel");
gbc = new GridBagConstraints();
gbc.anchor = GridBagConstraints.EAST;
gbc.gridwidth = 1;
gbc.gridx = 3;
gbc.gridy = 4;
gbc.insets = new Insets(10, 0, 10, 10);
gbc.weightx = 1;
pnlMain.add(btnCancel, gbc);
}
public JPanel getUI(){
return pnlMain;
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
#Override
public void run() {
JFrame frame = new JFrame("Address Book");
frame.getContentPane().add(new AddressBook().getUI());
frame.setLocationRelativeTo(null);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.pack();
frame.setVisible(true);
}
});
}
}
gbc.gridwidth is the parameter that allows a component to span more than one column. For example, if you have 3 columns and 4 rows and you want a label to occupy the complete top row, then you need to assign the first cell for the label. and set gbc.gridwidth = 3;

Categories