表格单元格自动对齐

时间:2013-11-20 12:03:22

标签: java swing jtable alignment

您好我遇到了这个问题,我的JTable中的单元格自动对齐,仅在事件发生后的第一列。我正在使用我的其他类和表中的TableModel。如果你想看到它,我可以告诉你我的其他课程的代码。

我尝试使用此代码设置对齐方式:

DefaultTableCellRenderer leftRenderer = new DefaultTableCellRenderer();
leftRenderer.setHorizontalAlignment(JLabel.LEFT);
table.getColumnModel().getColumn(1).setCellRenderer(leftRenderer);

这也没有解决它。

package gui;

import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.List;

import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.SwingConstants;
import javax.swing.SwingUtilities;
import javax.swing.table.DefaultTableCellRenderer;

import mappers.MemberMapper;
import domains.Member;

public class EditMemberGUI {
    private JFrame editMemberFrame;
    private JPanel btnsPanel, panel;
    private JButton editBtn, cancelBtn;
    private JLabel fNameLbl, lNameLbl, addressLbl, zipCodeLbl, cityLbl, emailLbl,
                    phoneNoLbl;
    private JTextField fNameTF, lNameTF, addressTF, zipCodeTF, cityTF, emailTF,
    phoneNoTF; ;
    private GridLayout layout;

    public EditMemberGUI(final ShowMembersGUI gui) {

        layout = new GridLayout(0,2,1,0);

        panel = new JPanel();
        panel.setLayout(layout);
        layout.setVgap(0);

        fNameLbl = new JLabel("Fornavn: ");
        fNameTF = new JTextField(15);

        lNameLbl = new JLabel("Efternavn: ");
        lNameTF = new JTextField(15);

        addressLbl = new JLabel("Adresse: ");
        addressTF = new JTextField(15);


        zipCodeLbl = new JLabel("Postnummer: ");
        zipCodeTF = new JTextField(15);

        cityLbl = new JLabel("By: ");
        cityTF = new JTextField(15);

        emailLbl = new JLabel("Email: ");
        emailTF = new JTextField(15);

        phoneNoLbl = new JLabel("Telefonnr: ");
        phoneNoTF = new JTextField(15);

        panel.add(fNameLbl);
        panel.add(fNameTF);
        panel.add(lNameLbl);
        panel.add(lNameTF);
        panel.add(addressLbl);
        panel.add(addressTF);
        panel.add(zipCodeLbl);
        panel.add(zipCodeTF);
        panel.add(cityLbl);
        panel.add(cityTF);
        panel.add(emailLbl);
        panel.add(emailTF);
        panel.add(phoneNoLbl);
        panel.add(phoneNoTF);


        //------------

        btnsPanel = new JPanel(new FlowLayout(FlowLayout.RIGHT));

        editBtn = new JButton("Redigér");
        editBtn.addActionListener(new ActionListener(){
            @Override
            public void actionPerformed(ActionEvent e) {                
                int zipCode = Integer.parseInt(zipCodeTF.getText());
                Member m = new Member(fNameTF.getText(), lNameTF.getText(), 
                        addressTF.getText(), zipCode, cityTF.getText(),
                        emailTF.getText(), phoneNoTF.getText());
                MemberMapper mMapper = new MemberMapper();
                int i = gui.getIndex();
                mMapper.editMember(m, i);

                editMemberFrame.setVisible(false);


                List<Member> data = mMapper.selectAllMembers();
                MyTableModel myTableModel = new MyTableModel(data);

                gui.getTable().setModel(myTableModel);
                int row = gui.getTable().getSelectedRow();
                myTableModel.tableUpdate(row, m);

            }   
        });

        cancelBtn = new JButton("Cancel");
        cancelBtn.addActionListener(new ActionListener(){
            @Override
            public void actionPerformed(ActionEvent arg0) {
                editMemberFrame.setVisible(false);
            }   
        });

        btnsPanel.add(editBtn);
        btnsPanel.add(cancelBtn);

    }

    public void showGUI() {

        editMemberFrame = new JFrame();
        editMemberFrame.setTitle("Redigér Medlem");
        editMemberFrame.setLayout(new BorderLayout());
        editMemberFrame.setSize(400,300);

        editMemberFrame.add(panel, BorderLayout.CENTER);
        editMemberFrame.add(btnsPanel, BorderLayout.SOUTH);
        //editMemberFrame.pack();
        editMemberFrame.setVisible(true);
    }

    public JTextField getfNameTF() {
        return fNameTF;
    }

    public void setfNameTF(JTextField fNameTF) {
        this.fNameTF = fNameTF;
    }

    public JTextField getlNameTF() {
        return lNameTF;
    }

    public void setlNameTF(JTextField lNameTF) {
        this.lNameTF = lNameTF;
    }

    public JTextField getAddressTF() {
        return addressTF;
    }

    public void setAddressTF(JTextField addressTF) {
        this.addressTF = addressTF;
    }

    public JTextField getZipCodeTF() {
        return zipCodeTF;
    }

    public void setZipCodeTF(JTextField zipCodeTF) {
        this.zipCodeTF = zipCodeTF;
    }

    public JTextField getCityTF() {
        return cityTF;
    }

    public void setCityTF(JTextField cityTF) {
        this.cityTF = cityTF;
    }

    public JTextField getEmailTF() {
        return emailTF;
    }

    public void setEmailTF(JTextField emailTF) {
        this.emailTF = emailTF;
    }

    public JTextField getPhoneNoTF() {
        return phoneNoTF;
    }

    public void setPhoneNoTF(JTextField phoneNoTF) {
        this.phoneNoTF = phoneNoTF;
    }
    }

2 个答案:

答案 0 :(得分:1)

出于此目的,您需要自己的TableCellRenderer,或者可以覆盖getTableCellRendererComponent()的{​​{1}}方法,例如:

DefaultTableCellRenderer

这里private TableCellRenderer getCellRenderer() { return new DefaultTableCellRenderer(){ @Override public Component getTableCellRendererComponent(JTable arg0, Object arg1, boolean arg2, boolean arg3, int arg4, int arg5) { //logic for Alignment setHorizontalAlignment(left ? JLabel.LEFT : JLabel.RIGHT); return super.getTableCellRendererComponent(arg0, arg1, arg2, arg3, arg4, arg5); } }; } 是布尔变量,作为对齐的标志。

编辑: 像你发布的那样试试吧:

left

table.getColumnModel().getColumn(1).setCellRenderer(getCellRenderer()); 为您的专栏返回渲染器,您需要自己调用其方法。

编辑: 这是一个例子

getCellRenderer()

答案 1 :(得分:1)

您没有显示构建表的实际数据的代码,但我假设右对齐的行中的数据类型不同。 getColumnClass()方法的默认实现可能是为该列分配不同的渲染器。要确认这一点,请尝试在传递给数据模型的数据中交换列,并查看右对齐是否使用相同的数据移动。

指定您自己的渲染器应解决此问题,但另一种解决方案是扩展DefaultTableModel并专门定义getColumnClass() mehtod中第一列的类。