GUI问题 - 布局行为怪异

时间:2013-11-21 12:03:38

标签: java swing user-interface checkbox

我有这个问题。当我尝试在我的面板中更改我的GUI时,没有任何反应。它保持不变。如果你看看我的MouseListener我的“membersTable”它应该向右添加一个面板但是当我尝试使用GridBagLayout时没有任何反应或者它添加了我想要的信息而不是复选框。我究竟做错了什么?我真的需要在继续之前解决这个问题。

package gui;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.GridLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.util.ArrayList;
import java.util.List;

import javax.swing.BorderFactory;
import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JEditorPane;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JSeparator;
import javax.swing.JTable;
import javax.swing.JTextPane;
import javax.swing.ListSelectionModel;
import javax.swing.SwingConstants;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import javax.swing.border.Border;
import javax.swing.table.AbstractTableModel;
import javax.swing.text.SimpleAttributeSet;
import javax.swing.text.StyleConstants;
import javax.swing.text.StyledDocument;

import mappers.MemberMapper;
import domains.Member;

public class AddContingentGUI {
    private JFrame addContingentFrame;
    private JTable membersTable;
    private JScrollPane tableScrollPane;
    private JPanel tablePanel, panelRight, panelRightSide, checkBoxPanel, btnPanel;
    private JSeparator js;
    private JLabel idLbl, fNameLbl, lNameLbl;
    private JButton addBtn, cancelBtn;
    private JCheckBox janCB, febCB, marCB;

    public AddContingentGUI() {

        MemberMapper mMapper = new MemberMapper();
        final List<Member> data = mMapper.selectAllMembers();
        final MyTableModel m = new MyTableModel(data);

        membersTable = new JTable(m);
        membersTable.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);

        idLbl = new JLabel();
        fNameLbl = new JLabel();
        lNameLbl = new JLabel();
        janCB = new JCheckBox();
        febCB = new JCheckBox();
        marCB = new JCheckBox();

        addBtn = new JButton("Tilføj");
        cancelBtn = new JButton("Cancel");
        cancelBtn.addActionListener(new ActionListener(){
            @Override
            public void actionPerformed(ActionEvent arg0) {
                addContingentFrame.setVisible(false);
            }   
        });

        tablePanel = new JPanel(new BorderLayout());
        panelRight = new JPanel();
        panelRight.setLayout(new GridBagLayout());
        //checkBoxPanel = new JPanel();
        btnPanel = new JPanel(new FlowLayout(FlowLayout.RIGHT));

        tableScrollPane = new JScrollPane(membersTable);
        membersTable.setFillsViewportHeight(true);
        membersTable.getColumnModel().getColumn(0).setPreferredWidth(100);
        membersTable.setBorder(BorderFactory.createLineBorder(Color.black));
        membersTable.getTableHeader().setReorderingAllowed(false);
        membersTable.addMouseListener(new MouseAdapter() {
            public void mouseClicked(MouseEvent e) {
                if (e.getClickCount() == 1) {
                    JTable target = (JTable) e.getSource();
                    int row = target.getSelectedRow();
                    int column = target.getSelectedColumn();

                    Object index = membersTable.getValueAt(row, 0);
                    int indexValue = (Integer) index;

                    panelRight.removeAll();

                    GridBagConstraints c = new GridBagConstraints();

                    c.fill = GridBagConstraints.VERTICAL;
                    c.gridx = 0;
                    c.gridy = 0;
                    panelRight.add(idLbl, c);

                    c.fill = GridBagConstraints.VERTICAL;
                    c.gridx = 0;
                    c.gridy = 1;
                    panelRight.add(fNameLbl, c);

                    c.fill = GridBagConstraints.VERTICAL;
                    c.gridx = 0;
                    c.gridy = 2;
                    panelRight.add(lNameLbl, c);


                    c.fill = GridBagConstraints.HORIZONTAL;
                    c.gridx = 1;
                    c.gridy = 3;
                    panelRight.add(janCB, c);
                    panelRight.add(new JSeparator(JSeparator.HORIZONTAL));

                    idLbl.setText("MedlemsID: " + data.get(row).getMemberId());
                    fNameLbl.setText("Fornavn: " + data.get(row).getfName());
                    lNameLbl.setText("Efternavn: " + data.get(row).getlName());

                }
            }
        });



        tablePanel.setBounds(10, 10, 300, 400);
        tablePanel.setLayout(new BorderLayout());
        tablePanel.add(membersTable.getTableHeader(), BorderLayout.NORTH);
        tablePanel.add(membersTable, BorderLayout.WEST);
        tablePanel.setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10));

        js = new JSeparator(JSeparator.VERTICAL);

        btnPanel.add(addBtn);
        btnPanel.add(cancelBtn);

        panelRight.setLayout(new BoxLayout(panelRight, BoxLayout.PAGE_AXIS));
        panelRight.setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10));
        panelRightSide = new JPanel(new BorderLayout());
        panelRightSide.add(js, BorderLayout.WEST);
        panelRightSide.add(panelRight, BorderLayout.CENTER);
        panelRightSide.add(btnPanel, BorderLayout.SOUTH);



    }

    public void showGUI() {
        addContingentFrame = new JFrame();
        addContingentFrame.setTitle("Opret Kontingent");
        addContingentFrame.setLayout(new BorderLayout());
        addContingentFrame.setSize(700, 500);

        addContingentFrame.add(tablePanel, BorderLayout.WEST);
        addContingentFrame.add(panelRightSide, BorderLayout.CENTER);
        addContingentFrame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        addContingentFrame.setVisible(true);
    }

    class MyTableModel extends AbstractTableModel {

        private static final long serialVersionUID = 1L;
        private List<Member> members = new ArrayList<Member>();

        public MyTableModel(List<Member> members) {
            this.members = members;
        }

        public void addMember(Member m) {
            members.add(m);
            int row = members.size() - 1;
            fireTableRowsInserted(row, row);
        }

        public void removeMemberFromTable(int row) {
            members.remove(row);
            fireTableRowsDeleted(row, row);
        }

        public void tableUpdate(int row, Member m) {
            if (row >= 0 && row < members.size()) {
                members.set(row, m);
                fireTableRowsUpdated(row, row);
            }
        }

        public List<Member> getMembers() {
            return new ArrayList<Member>(members);
        }

        @Override
        public int getColumnCount() {
            return 3;
        }

        @Override
        public int getRowCount() {
            return members.size();
        }

        @Override
        public Object getValueAt(int rowIndex, int columnIndex) {
            Member m = members.get(rowIndex);

            switch (columnIndex) {
            case 0:
                return m.getMemberId();

            case 1:
                return m.getfName();

            case 2:
                return m.getlName();

            default:
                return "";
            }
        }

        public String getColumnName(int column) {
            switch (column) {
            case 0:
                return "MedlemsID";

            case 1:
                return "Fornavn";

            case 2:
                return "Efternavn";

            default:
                return "";
            }
        }

        public Class<?> getColumnClass(int columnIndex) {
            switch (columnIndex) {
            case 0:
                return String.class;

            case 1:
                return String.class;

            case 2:
                return String.class;

            default:
                return String.class;
            }
        }
    }
}

1 个答案:

答案 0 :(得分:2)

需要在这些更改后重新验证/重新绘制面板。

panelRight.revalidate();
panelRight.repaint();