تحديد قيمة النموذج (لحساب لصناعة السيارات في العرض) عمود JTable

StackOverflow https://stackoverflow.com/questions/838095

سؤال

وإما هذا لا وجود لها، أو أنا لا أفكر / البحث بشكل صحيح لأنه في وقت متأخر ...

وأريد أن تحديد عرض العمود JTable في أرجوحة على أساس القيمة النموذج لأكبر سلسلة أتوقع (أو تعرف) موجودة في عمود معين. أنا لا أعرف # بكسل لأنني لا أعرف بالضرورة الخط في وقت الترجمة.

هل هناك طريقة لتعيين قيمة النموذج لأغراض عرض العمود، في الطريق إلى هناك لأغراض ارتفاع الصف؟ إذا كان الأمر كذلك، كيف؟

هل كانت مفيدة؟

المحلول

هل حاولت خلق JLabel في وقت التشغيل واستخدام حجمه لتغيير حجم الجدول الخاص بك؟

// create a label that will be using the run-time font
JLabel prototypeLabel = new JLabel("Not Applicable")

// get the labels preferred sizes
int preferredWidth = prototypeLabel.getPreferredSize().getWidth();
int preferredHeight = prototypeLabel.getPreferredSize().getHeight();

// set the sizes of the table's row and columns
myTable.setRowHeight(preferredHeight);

for(TableColumn column : myTable.getColumnModel.getColumns()){
   column.setPreferredWidth(preferredWidth);        
}

نصائح أخرى

ويمكنك أن تجرب البرمجية التالية:

/**
 * Sets the preferred width of the columns of a table from prototypes
 * @param table the target table
 * @param prototypes an array of prototypes, {@code null} values will be ignored
 * @param setMaxWidth {@code true} if the maximum column width should also be set
 */
public static void setWidthFromPrototype(JTable table, Object[] prototypes, boolean setMaxWidth) {
if (prototypes.length != table.getColumnCount())
  throw new IllegalArgumentException("The prototypes array should contain exactly one element per table column");
for (int i = 0; i < prototypes.length; i++) {
    if (prototypes[i] != null) {
        Component proto = table.getCellRenderer(0,i)
                .getTableCellRendererComponent(table, prototypes[i], false, false, 0, i);
        int prefWidth = (int) proto.getPreferredSize().getWidth() + 1;
        table.getColumnModel().getColumn(i).setPreferredWidth(prefWidth);
        if (setMaxWidth)
            table.getColumnModel().getColumn(i).setMaxWidth(prefWidth);
    }
}
}

SwingX بمد JXTable / عمود ودعم وضع نماذج لالأولي التحجيم عرض العمود، يمكنك القيام بذلك إما بعد الأعمدة وقد تم إنشاء

for(int col = 0; ....) {
    table.getColumnExt(col).setPrototypeValue(myPrototype[col]
}

وأو من خلال تنفيذ ColumnFactory العرف الذي بتكوين الأعمدة على خلق

ColumnFactory factory = new ColumnFactory() {
    @Override
    protected void configureTableColumn(TableModel model, TableColumnExt columnExt) {
        super(...);
        columnExt.setPrototypeValue(myPrototype[columnExt.getModelIndex()];
    }
}
table.setColumnFactory(factory);
table.setModel(myModel);

وبدلا من إنشاء التسمية، الحصول على المكون الفعلي من TableCellRenderer واختبار الحجم على ما يلي:

// create the component that will be used to render the cell
Comp prototype = table.getDefaultRenderer(model.getColumnClass(i)).getTableCellRendererComponent(table, "Not Applicable", false, false, 0, i);

// get the labels preferred sizes
int preferredWidth = comp.getPreferredSize().getWidth();
int preferredHeight = comp.getPreferredSize().getHeight();

وهذا هو مثال عمود واحد، سوف تحتاج إلى تكرار هذا للحصول على حجم كل عمود (وأيضا ضبطه). انظر http://www.exampledepot.com/egs/javax.swing .table / PackCol.html للحصول على مثال على ذلك.

إذا كنت لا تعرف الخط في وقت الترجمة ثم عرض أي عمود JTable دائما ما تكون غير معروفة. كما شيك التعقل، فتح مستند نص واللعب مع خطوط مختلفة مع إبقاء حجم نقطة ثابتة. طول ما هو مكتوب يختلف على أساس خط تلو الخط ولكن الارتفاع لا.

ويجب أن يكون ارتفاع صف JTable قادرة على أن تحدد لأي حجم الخط (في نقاط) كما هو <وأ href = "http://en.wikipedia.org/wiki/Point_(typography)" يختلط = "noreferrer نوفولو"> تعريف المعيار. قد يستغرق قليلا من التجريب، على الرغم، بالنظر إلى أن JTable ربما يعطي قليلا من تباعد الخلايا في المنتصف.

إذا كنت لا تستطيع أن تضمن ولا حجم الخط ولا الخط نفسه في وقت الترجمة ثم أنا مهتم في ما يجيب الآخرين يأتي مع:)

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top