嗨我有這個問題,我的單元格在我的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;
}
}
這是不是很奇怪,它只是第一列呢? – ilhami
我不知何故通過更改列類來修復它。 :) 謝謝! – ilhami
@Ilhami Integer.class和其他數字在表格單元格中對齊。 Object.class和String.class不應該。 –