是否有机会在一个地方创建一个Jtable
和GridBagLayout
的面板?
我想要一个带有过滤按钮的JTable
和GridBagLayout
,但是当我在GridBagLayout
中添加表格和按钮时,这些按钮会显示在表格后面。
这是我的代码:
public class FormConnected extends JPanel {
private JTextField tfId = new JTextField(10);
private JTextField tfName = new JTextField(10);
private JTextField tfSurname = new JTextField(10);
private JTextField tfYear = new JTextField(10);
private JTextField tfAge = new JTextField(10);
private JTextField tfSchoolName = new JTextField(10);
private JTextField tfCity = new JTextField(10);
private JTextField tfDeanName = new JTextField(10);
private JTextField tfYearOfBuild = new JTextField(10);
private JLabel lFrom = new JLabel("From");
private JLabel lTo = new JLabel("To");
private JCheckBox jcbStudentName = new JCheckBox("Student name");
private JCheckBox jcbStudentSurname = new JCheckBox("Student surname");
private JCheckBox jcbSchoolCity = new JCheckBox("School city");
private JCheckBox jcbSchoolName = new JCheckBox("School name");
private JCheckBox jcbStudentAge = new JCheckBox("Student age");
private JList jlStudentName = new JList();
private JList jlStudentSurname = new JList();
private JList jlSchoolName = new JList();
private JList jlSchoolCity = new JList();
private JButton bFiltrate = new JButton("Filtrate");
private JButton bReset = new JButton("Reset");
private JComboBox jcbFrom = new JComboBox();
private JComboBox jcbTo = new JComboBox();
JPanel panelUp;
JPanel panelMedium;
JPanel panelDown;
private ArrayList<Connected> list;
private int idx;
public ArrayList<Connected> getLista() {
return list;
}
public void setLista(ArrayList<Connected> list) {
this.list = list;
}
public int getIdx() {
return idx;
}
public void setIdx(int idx) {
this.idx = idx;
}
public FormConnected()
{
super(new GridBagLayout());
//add(new JScrollPane(new JTable(new MyCustomTableModel(Database.selectInnerJoin()))));
JPanel panelUp = new JPanel(new GridBagLayout());
GridBagConstraints gbcPanelUp = new GridBagConstraints();
JPanel panelMedium = new JPanel(new GridBagLayout());
GridBagConstraints gbcPanelMedium = new GridBagConstraints();
JPanel panelDown = new JPanel(new GridBagLayout());
GridBagConstraints gbcPanelDown = new GridBagConstraints();
gbcPanelUp.gridx = 0;
gbcPanelUp.gridy = 0;
gbcPanelUp.ipadx = 5;
gbcPanelUp.ipady = 5;
gbcPanelUp.insets = new Insets(5, 5, 5, 5);
panelUp.add(jcbStudentName, gbcPanelUp);
gbcPanelUp.gridx = 1;
gbcPanelUp.gridy = 0;
panelUp.add(jcbStudentSurname, gbcPanelUp);
gbcPanelUp.gridx = 2;
gbcPanelUp.gridy = 0;
panelUp.add(jcbSchoolCity, gbcPanelUp);
gbcPanelUp.gridx = 3;
gbcPanelUp.gridy = 0;
panelUp.add(jcbSchoolName, gbcPanelUp);
gbcPanelUp.gridx = 0;
gbcPanelUp.gridy = 1;
panelUp.add(jlStudentName, gbcPanelUp);
gbcPanelUp.gridx = 1;
gbcPanelUp.gridy = 1;
panelUp.add(jlStudentSurname, gbcPanelUp);
gbcPanelUp.gridx = 2;
gbcPanelUp.gridy = 1;
panelUp.add(jlSchoolCity, gbcPanelUp);
gbcPanelUp.gridx = 3;
gbcPanelUp.gridy = 1;
panelUp.add(jlSchoolName, gbcPanelUp);
gbcPanelUp.gridx = 0;
gbcPanelUp.gridy = 2;
panelUp.add(jcbStudentAge, gbcPanelUp);
gbcPanelUp.gridx = 0;
gbcPanelUp.gridy = 3;
panelUp.add(lFrom, gbcPanelUp);
gbcPanelUp.gridx = 1;
gbcPanelUp.gridy = 3;
panelUp.add(jcbFrom, gbcPanelUp);
gbcPanelUp.gridx = 2;
gbcPanelUp.gridy = 3;
panelUp.add(lTo, gbcPanelUp);
gbcPanelUp.gridx = 3;
gbcPanelUp.gridy = 3;
panelUp.add(jcbTo, gbcPanelUp);
gbcPanelDown.gridx = 0;
gbcPanelDown.gridy = 0;
gbcPanelDown.ipadx = 10;
gbcPanelDown.ipady = 10;
gbcPanelDown.insets = new Insets(50, 50, 5, 50);
panelDown.add(bFiltrate, gbcPanelDown);
gbcPanelDown.gridx = 1;
gbcPanelDown.gridy = 0;
gbcPanelDown.insets = new Insets(50, 50, 5, 50);
panelDown.add(bReset, gbcPanelDown);
GridBagConstraints gbcMain = new GridBagConstraints();
gbcMain.gridx = 0;
gbcMain.gridy = 0;
add(panelUp, gbcMain);
gbcMain.gridx = 0;
gbcMain.gridy = 1;
add(panelDown, gbcMain);
list = Database.selectInnerJoin();
if (list != null)
{
idx = 0;
fillFormConnected();
}
}
public void fillFormConnected()
{
if(list.isEmpty() == false)
{
tfId.setText(list.get(idx).getId()+ "");
tfName.setText(list.get(idx).getName()+ "");
tfSurname.setText(list.get(idx).getSurname()+ "");
tfYear.setText(list.get(idx).getYear()+"");
tfAge.setText(list.get(idx).getAge()+"");
tfSchoolName.setText(list.get(idx).getSchoolName()+"");
tfCity.setText(list.get(idx).getCity()+"");
tfDeanName.setText(list.get(idx).getDeanName()+"");
tfYearOfBuild.setText(list.get(idx).getYearOfBuild()+"");
}
}
}