View Full Version : مشکل در نحوه ی نمایش عناصر طراحی شده در jtable
bigtime
پنج شنبه 17 مرداد 1392, 22:26 عصر
با سلام و تبریک عید سعید فطر به همه ی دوستان
من یک jtable که در یکی از ستون های اون یک combo box گذاشتم در نظر گرفتم. وقتی داده رو داخل این جدول وارد می کنم این combo box دیگه وجود نداره.
می خواستم بدونم که این مشکل از کجا ناشی میشه و راه حلش چیه؟
با تشکر قبلی از کمک شما
sajjad4563
جمعه 18 مرداد 1392, 01:24 صبح
اینطوری که نمیشه، کد رو قرار بدین
javaphantom
جمعه 18 مرداد 1392, 12:17 عصر
با سلام و تبریک عید سعید فطر به همه ی دوستان
من یک jtable که در یکی از ستون های اون یک combo box گذاشتم در نظر گرفتم. وقتی داده رو داخل این جدول وارد می کنم این combo box دیگه وجود نداره.
می خواستم بدونم که این مشکل از کجا ناشی میشه و راه حلش چیه؟
با تشکر قبلی از کمک شما
import javax.swing.*;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableCellRenderer;
import javax.swing.table.TableColumn;
import java.awt.*;
/**
* Created with IntelliJ IDEA.
* User: babak
* Date: 8/9/13
* Time: 7:54 PM
* To change this template use File | Settings | File Templates.
*/
public class MyFrame extends JFrame {
private JPanel myPanel;
private JComboBox myComboBox;
private JTable myTable;
private MyTableModel myTableModel = new MyTableModel();
public MyFrame() {
this.setDefaultCloseOperation(DISPOSE_ON_CLOSE);
this.setBounds(new Rectangle(400, 400));
this.getContentPane().add(this.getMyPanel());
this.setVisible(true);
}
public static void main(String[] s) {
new MyFrame();
}
public JPanel getMyPanel() {
if (this.myPanel == null) {
this.myPanel = new JPanel(new FlowLayout());
this.myPanel.add(this.getMyTable());
}
return myPanel;
}
public JComboBox getMyComboBox() {
if (this.myComboBox == null) {
String[] stringItem = {"yes", "no"};
this.myComboBox = new JComboBox(stringItem);
}
return myComboBox;
}
public JTable getMyTable() {
if (this.myTable == null) {
this.myTable = new JTable();
this.myTable.setModel(this.myTableModel);
this.myTable.setRowHeight(20);
TableColumn column = this.myTable.getColumnModel().getColumn(1);
column.setCellRenderer(new TableCellRenderer() {
private JComboBox myComboBox;
public JComboBox getMyComboBox() {
if (this.myComboBox == null) {
String[] stringItem = {"yes", "no"};
this.myComboBox = new JComboBox(stringItem);
}
return myComboBox;
}
@Override
public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) {
return this.getMyComboBox();
}
});
column.setCellEditor(new DefaultCellEditor(getMyComboBox()));
Object[] rows = new Object[2];
for (int i = 0; i < 2; i++) {
rows[0] = i;
this.myTableModel.addRow(rows);
}
}
return myTable;
}
}
class MyTableModel extends DefaultTableModel {
private String[] columns = {"A", "B"};
/**
* Returns the number of columns in this data table.
*
* @return the number of columns in the model
*/
@Override
public int getColumnCount() {
return columns.length;
}
/**
* Returns the column name.
*
* @return a name for this column using the string value of the
* appropriate member in <code>columnIdentifiers</code>.
* If <code>columnIdentifiers</code> does not have an entry
* for this index, returns the default
* name provided by the superclass.
*/
@Override
public String getColumnName(int column) {
return this.columns[column];
}
/**
* Returns true regardless of parameter values.
*
* @param row the row whose value is to be queried
* @param column the column whose value is to be queried
* @return true
* @see #setValueAt
*/
@Override
public boolean isCellEditable(int row, int column) {
if (column == 0) return false;
return true;
}
}
vBulletin® v4.2.5, Copyright ©2000-1404, Jelsoft Enterprises Ltd.