防止JXTable自动调整列宽

时间:2015-11-16 20:29:45

标签: java jtable swingx tablecolumn jxtable

在我执行“打包所有列”调用后发生的事情中,我遇到了一个与JXTable(可能甚至是JTable问题)的特定情况。

'打包所有列'工作正常,但是当我手动使列宽变小时,在收到下一个TableModelEvent时,我修改的列宽总是变得更大。我注意到JTable的私有setWidthsFromPreferredWidths()最终被调用,看起来就像罪魁祸首。

同样,只有在选择“打包所有列”后才会出现此问题。这是一个已知的错误,还是“打包所有列”的预期行为?

1 个答案:

答案 0 :(得分:0)

我实际上在http://www.coderanch.com/

找到了我的问题的答案

它涉及覆盖 columnMarginChanged(ChangeEvent)方法,以确保列的 getWidth() getPreferredWidth()同步:

public class RecyclerViewAdapter extends RecyclerView.Adapter<RecyclerViewAdapter.ViewHolder> {

    Context context;
    List<String> mUrls = new ArrayList<String>();

    IRecyclerViewClickListener mIRecyclerViewClickListener;

    public int position;

    public int getPosition() {
        return position;
    }

    public void setPosition(int position) {
        this.position = position;
    }



    public RecyclerViewAdapter(Context context, List<String> urls) {
        this.context = context;
        this.mUrls.clear();
        this.mUrls = urls;

        Log.d(Const.DEBUG, "Urls Size: " + urls.size());
        Log.d(Const.DEBUG, urls.toString());

        if (context instanceof IRecyclerViewClickListener)
            mIRecyclerViewClickListener = (IRecyclerViewClickListener) context;
        else
            Log.d(Const.DEBUG, "Implement IRecyclerViewClickListener in Activity");
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(context).inflate(R.layout.item_horizontal_recyclerview, parent, false);
        ViewHolder holder = new ViewHolder(view);
        return holder;
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {
        Picasso.with(context).load(mUrls.get(position)).into(holder.mImageView);
    }

    @Override
    public int getItemCount() {
        return mUrls.size();
    }


    public void rotateThumbnail() {


    }

    public class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {

        public ImageView mImageView;
        public View v;

        public ViewHolder(View v) {
            super(v);
            v.setTag(getAdapterPosition());
            v.setOnClickListener(this);
            this.mImageView = (ImageView) v.findViewById(R.id.image);
        }

        @Override
        public void onClick(View v) {
            this.v = v;
            mIRecyclerViewClickListener.onItemClick(getAdapterPosition());
        }
    }


}