gpt4 book ai didi

java - 表格单元格自动右对齐

转载 作者:行者123 更新时间:2023-11-30 04:08:58 25 4
gpt4 key购买 nike

您好,我遇到了这个问题,JTable 中的单元格自动右对齐,仅在事件发生后的第一列中。我正在使用其他类(class)的 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;
}
}

最佳答案

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

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

关于java - 表格单元格自动右对齐,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20095464/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com