So I was doing a small Java Punnett Square Project ( asked a question that gives more details as to what it does exactly). After asking the last question, I decided to start rewriting the program since it was very confusing and jumbled. Here is the formatting that I want:
Here is a screenshot of my output:
The text fields are not to the right of the Labels, and my calculate button is not displaying my table.
Here is the code:
//imports
public class GeneticsGUI
{
public static void main(String[] args)
{
JFrame window = new JFrame("Genetics");
JPanel panel = new JPanel(new GridLayout(25,2));
JLabel o = new JLabel("Options:");
panel.add(o);
String[] options = new String[] {"2x2 Punnett Square", "4x4 Punnett Square"};
JComboBox<String> list = new JComboBox<>(options);
panel.add(list);
String selected = (String) list.getSelectedItem();
JLabel p1 = new JLabel("Parent 1:");
JTextField par1 = new JTextField();
JLabel p2 = new JLabel("Parent 1:");
JTextField par2 = new JTextField();
panel.add(p1,BorderLayout.WEST);
panel.add(par1,BorderLayout.CENTER);
panel.add(p2,BorderLayout.WEST);
panel.add(par2,BorderLayout.CENTER);
JButton submit = new JButton("Calculate");
submit.addActionListener( new ActionListener()
{
@Override
public void actionPerformed(ActionEvent e)
{
if(e.getSource()==submit)
{
String p1 = par1.getText();
String p2 = par2.getText();
if(selected == "2x2 Punnett Square")
{
String[][] mono = Genetics.monohybridPunett(p1, p2);
for(int row = 0; row<2 ; row++)
{
for(int col = 0; col<2; col++)
{
mono[row][col] = " "+ mono[row][col];
}
}
JTable table = new JTable(mono.length,mono.length);
table.setShowGrid(true);
for(int i = 0; i<mono.length; i++)
for(int j = 0; j<mono.length; j++)
table.setValueAt(mono[i][j], i, j);
DefaultTableCellRenderer t = new DefaultTableCellRenderer();
t.setHorizontalTextPosition(DefaultTableCellRenderer.CENTER);
t.setVerticalTextPosition(DefaultTableCellRenderer.CENTER);
table.setSize(300, 300);
table.isCellEditable(0,0);
table.setRowHeight(100);
panel.add(table,BorderLayout.EAST);;
panel.revalidate();
}
else if(selected == "4x4 Punnett Square")
{
String[][] di = Genetics.dihybridPunett(p1, p2);
JTable table = new JTable(di.length,di.length);
table.setShowGrid(true);
DefaultTableModel model = new DefaultTableModel(di,new Object[]{"AaBb","AaBb","",""});
table.isCellEditable(0, 0);
table.setModel(model);
table.setRowHeight(100);
table.getColumnModel().getColumn(0).setPreferredWidth(100);
table.getColumnModel().getColumn(1).setPreferredWidth(100);
table.getColumnModel().getColumn(2).setPreferredWidth(100);
table.getColumnModel().getColumn(3).setPreferredWidth(100);
panel.add(table,BorderLayout.CENTER);
panel.revalidate();
}
}
}
});
panel.add(submit);
window.add(panel);
window.setSize(800, 800);
window.setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE);
window.setVisible(true);
}
}
Again, I am not really an expert in GUI or Swing, so was wondering what I would need to do to fix this formatting problem.
EDIT:
Here is my current code for my GridBagLayout implementation.
JFrame window = new JFrame("Genetics");
JPanel panel = new JPanel();
panel.setLayout(new GridBagLayout());
GridBagConstraints c = new GridBagConstraints();
JLabel o = new JLabel("Options:");
c.fill = GridBagConstraints.HORIZONTAL;
c.gridx = 0;
c.gridy = 0;
c.weightx = 0;
panel.add(o, c);
String[] options = new String[] {"2x2 Punnett Square", "4x4 Punnett Square"};
JComboBox<String> list = new JComboBox<>(options);
c.fill = GridBagConstraints.HORIZONTAL;
c.weightx = 1;
c.gridx = 1;
c.gridy = 0;
panel.add(list, c);
String selected = (String) list.getSelectedItem();
JLabel p1 = new JLabel("Parent 1:");
c.fill = GridBagConstraints.HORIZONTAL;
c.weightx = 0.5;
c.weighty = 1;
c.insets = new Insets(40,0,0,0);
c.gridx = 0;
c.gridy = 1;
panel.add(p1, c);
JTextField par1 = new JTextField();
c.fill = GridBagConstraints.HORIZONTAL;
c.weightx = 0.5;
c.gridx = 1;
c.gridy = 1;
panel.add(par1, c);
JLabel p2 = new JLabel("Parent 2:");
c.fill = GridBagConstraints.HORIZONTAL;
c.insets = new Insets(0,0,0,0);
c.weightx = 0.5;
c.gridx = 0;
c.gridy = 2;
panel.add(p2, c);
JTextField par2 = new JTextField();
c.fill = GridBagConstraints.HORIZONTAL;
c.weightx = 0.5;
c.gridx = 1;
c.gridy = 2;
panel.add(par2, c);
JButton submit = new JButton("Calculate");
c.fill = GridBagConstraints.HORIZONTAL;
c.weightx = 0.5;
c.gridx = 0;
c.gridy = 3;
c.anchor = GridBagConstraints.LINE_START;
panel.add(submit, c);