我把JComboBox放在了一个JTable里面,我这个JComboBox是为了显示用户选择的文件名,文件可以多选,但是当文件名太长的话就会看不完全,请问各位大虾有什么好的解决方法木有?先谢谢大家了。
哦,我原来还写了一个MyComboBoxUI继承自MetalComboBoxUI里面做了一点处理让用户不能进行选择,因为ComboBox里面被选中的文件代表我正在拷贝的文件,所以用户是不能选择的。
听别人说好像这个要在什么UIManager里面设置一下还是怎么弄??期待高手的帮忙:)
哦,我原来还写了一个MyComboBoxUI继承自MetalComboBoxUI里面做了一点处理让用户不能进行选择,因为ComboBox里面被选中的文件代表我正在拷贝的文件,所以用户是不能选择的。
听别人说好像这个要在什么UIManager里面设置一下还是怎么弄??期待高手的帮忙:)
高手是不是都休息去了呀 大家帮帮忙啊 我急着解决这个问题啦 分不够我可以加的啦!SOS
{
public static void main(String[] args)
{
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (Exception e) {
} JTable table = new JTable(5, 5) {
public void removeEditor()
{
super.removeEditor();
repaint();
}
};
JComboBox combobox = new JComboBox() {
public void setBounds(int x, int y, int width, int height)
{
width = Math.max(getPreferredSize().width, width);
super.setBounds(x, y, width, height);
}
};
combobox.setBorder(BorderFactory.createEmptyBorder());
combobox.addItem("Hello World - 1");
combobox.addItem("Hello World - 2");
combobox.addItem("Hello World - 3");
DefaultCellEditor editor = new DefaultCellEditor(combobox);
table.setDefaultEditor(Object.class, editor); JScrollPane sp = new JScrollPane(table); JFrame frame = new JFrame();
frame.getContentPane().add(sp, BorderLayout.CENTER);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
}
昨天前天一直加班今天才调休 晚上回来一看 gtlang78()您老大就帮我解决了个大问题啊
gtlang78()可以认识一下不?
改成combobox.addItem("Hello World - 1Hello World - 1Hello World - 1Hello World - 1Hello World - 1Hello World - 1Hello World - 1Hello World - 1Hello World - 1Hello World - 1Hello World - 1Hello World - 1Hello World - 1Hello World - 1Hello World - 1Hello World - 1Hello World - 1Hello World - 1Hello World - 1"); 那么显示就会出问题啦:)
我请教过别人 他们写了一个类 public class TEMetalComboBoxUI extends MetalComboBoxUI
好象在这个方法里面处理了一下protected ComboPopup createPopup() 也就是在combobox里面内容很长的时候就出现滚动条 (但是在他们这种处理方法中我还是发现了问题 ,就是说这种处理的横向和纵向滚动条老是)
但是在他们这种处理方法中我还是发现了问题 ,就是说这种处理的横向和纵向滚动条老是一起出现 所以当只有一个选项的时候纵向的滚动条不够长 所以显示不是很理想啦(这里我是说别人帮我弄出来的解决方法啦 不是说老大您的)老大你把你上面的代码中的这一行combobox.addItem("Hello World - 1");
改成combobox.addItem("Hello World - 1Hello World - 1Hello World - 1Hello World - 1Hello World - 1Hello World - 1Hello World - 1Hello World - 1Hello World - 1Hello World - 1Hello World - 1Hello World - 1Hello World - 1Hello World - 1Hello World - 1Hello World - 1Hello World - 1Hello World - 1Hello World - 1"); 试一下看看啦:)
import java.awt.Component;
import java.awt.Dimension;
import java.awt.Insets;import javax.swing.*;
import javax.swing.plaf.basic.BasicComboPopup;
import javax.swing.plaf.basic.ComboPopup;import com.sun.java.swing.plaf.windows.WindowsComboBoxUI;public class FullSizeComboBoxEditorDemo {
public static void main(String[] args) {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (Exception e) {
} JTable table = new JTable(5, 5) {
public void removeEditor() {
super.removeEditor();
repaint();
}
}; JComboBox combobox = new JComboBox() {
public void updateUI() {
setUI(new MyComboBoxUI());
}
}; combobox.setBorder(BorderFactory.createEmptyBorder());
DefaultCellEditor editor = new DefaultCellEditor(combobox) {
public Component getTableCellEditorComponent(
JTable table, Object value, boolean isSelected, int row, int column) {
super.getTableCellEditorComponent(table, value, isSelected, row, column); JComboBox comboBox = (JComboBox) editorComponent;
comboBox.removeAllItems();
if (column >= 0) {
comboBox.addItem("The quick brown fox jumps over the lazy dog.");
}
if (column >= 1) {
comboBox.addItem("The quick brown fox jumps over the lazy dog. The quick brown fox jumps over the lazy dog.");
}
if (column >= 2) {
comboBox.addItem("The quick brown fox jumps over the lazy dog. The quick brown fox jumps over the lazy dog. The quick brown fox jumps over the lazy dog.");
}
return editorComponent;
}
};
table.setDefaultEditor(Object.class, editor); JScrollPane sp = new JScrollPane(table); JFrame frame = new JFrame();
frame.getContentPane().add(sp, BorderLayout.CENTER);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
}class MyComboBoxUI extends WindowsComboBoxUI {
protected ComboPopup createPopup() {
return new MyComboBoxPopup(comboBox);
}
}class MyComboBoxPopup extends BasicComboPopup {
private static final int POPUP_MAX_WIDTH = 600; public MyComboBoxPopup(JComboBox combo) {
super(combo);
} protected JScrollPane createScroller() {
JScrollPane sp = new JScrollPane( list,
ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED,
ScrollPaneConstants.HORIZONTAL_SCROLLBAR_AS_NEEDED );
return sp;
} public void show() {
int selectedIndex = comboBox.getSelectedIndex();
if (selectedIndex == -1 ) {
list.clearSelection();
}
else {
list.setSelectedIndex( selectedIndex );
list.ensureIndexIsVisible( selectedIndex );
} Insets insets = getInsets();
int popupPrefWid = list.getPreferredSize().width + insets.left + insets.right; Dimension scrollSize = new Dimension(
0, getPopupHeightForRowCount( comboBox.getMaximumRowCount()));
if (popupPrefWid > POPUP_MAX_WIDTH) {
scrollSize.width = POPUP_MAX_WIDTH;
scrollSize.height += scroller.getHorizontalScrollBar().getPreferredSize().height;
}
else {
scrollSize.width = popupPrefWid;
}
scroller.setMaximumSize( scrollSize );
scroller.setPreferredSize( scrollSize );
scroller.setMinimumSize( scrollSize );
list.revalidate();
show( comboBox, 0, comboBox.getHeight());
}
}
但是我的comboBox是做成不可编辑的,所以我取不到用户选中的项啦 我觉得如果要通过鼠标的被点击的地方去取用户选中的项的值可能更麻烦啦:)
老大你在吗?
这个东东做得很好啦 不过我最开始可能木有表达好 我现在做的这个comboBox是要做成不可编辑的啦
用户不能进行选中的 我的comboBox被选中的项代表了一个正在进行拷贝的文件,这是需要后台向我上报的,我是要根据后台上报才能把comboBox选中的项进行设置,用户是不能设置,只能点开comboBox看一下啦:(
private static final int POPUP_MAX_WIDTH = 600; public MyComboBoxPopup(JComboBox combo) {
super(combo);
} protected JScrollPane createScroller() {
JScrollPane sp = new JScrollPane( list,
ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED,
ScrollPaneConstants.HORIZONTAL_SCROLLBAR_AS_NEEDED );
return sp;
} public void show() {
setListSelection(comboBox.getSelectedIndex()); Insets insets = getInsets();
int popupPrefWid = list.getPreferredSize().width + insets.left + insets.right; Dimension scrollSize = new Dimension(
0, getPopupHeightForRowCount( comboBox.getMaximumRowCount()));
if (popupPrefWid > POPUP_MAX_WIDTH) {
scrollSize.width = POPUP_MAX_WIDTH;
scrollSize.height += scroller.getHorizontalScrollBar().getPreferredSize().height;
}
else {
scrollSize.width = popupPrefWid;
}
scroller.setMaximumSize( scrollSize );
scroller.setPreferredSize( scrollSize );
scroller.setMinimumSize( scrollSize );
list.revalidate();
show( comboBox, 0, comboBox.getHeight());
}
public void itemStateChanged( ItemEvent e ) {
if (e.getStateChange() == ItemEvent.SELECTED) {
JComboBox comboBox = (JComboBox)e.getSource();
setListSelection(comboBox.getSelectedIndex());
}
} private void setListSelection(int selectedIndex) {
if ( selectedIndex == -1 ) {
list.clearSelection();
}
else {
((MyList)list).superSetSelectedIndex( selectedIndex );
list.ensureIndexIsVisible( selectedIndex );
}
}
protected JList createList() {
return new MyList();
} class MyList extends JList {
public MyList() {
super(comboBox.getModel());
}
public void superSetSelectedIndex(int index) {
super.setSelectedIndex(index);
} public void setSelectedIndex(int index) {
}
public void setSelectedIndices(int[] indices) {
}
public void setSelectedValue(Object anObject, boolean shouldScroll) {
}
public void setSelectionInterval(int anchor, int lead) {
}
}
}
我再试试上面的哦:)
不过还可以麻烦老大帮我看一下不? 可不可以把那个弹出来的下拉东东设置成和comboBox一样宽的啊:)
问得我都不好意思了:( 俺自己调了半天
public MyComboBoxPopup(JComboBox combo) {
super(combo);
} protected JScrollPane createScroller() {
JScrollPane sp = new JScrollPane( list,
ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED,
ScrollPaneConstants.HORIZONTAL_SCROLLBAR_AS_NEEDED );
return sp;
} public void show() {
setListSelection(comboBox.getSelectedIndex()); Insets insets = getInsets();
int popupPrefWid = list.getPreferredSize().width + insets.left + insets.right; Dimension scrollSize = new Dimension(
comboBox.getWidth(), getPopupHeightForRowCount( comboBox.getMaximumRowCount()));
if (popupPrefWid > scrollSize.width) {
scrollSize.height += scroller.getHorizontalScrollBar().getPreferredSize().height;
}
scroller.setMaximumSize( scrollSize );
scroller.setPreferredSize( scrollSize );
scroller.setMinimumSize( scrollSize );
list.revalidate();
show( comboBox, 0, comboBox.getHeight());
}
public void itemStateChanged( ItemEvent e ) {
if (e.getStateChange() == ItemEvent.SELECTED) {
JComboBox comboBox = (JComboBox)e.getSource();
setListSelection(comboBox.getSelectedIndex());
}
} private void setListSelection(int selectedIndex) {
if ( selectedIndex == -1 ) {
list.clearSelection();
}
else {
((MyList)list).superSetSelectedIndex( selectedIndex );
list.ensureIndexIsVisible( selectedIndex );
}
}
protected JList createList() {
return new MyList();
} class MyList extends JList {
public MyList() {
super(comboBox.getModel());
}
public void superSetSelectedIndex(int index) {
super.setSelectedIndex(index);
} public void setSelectedIndex(int index) {
}
public void setSelectedIndices(int[] indices) {
}
public void setSelectedValue(Object anObject, boolean shouldScroll) {
}
public void setSelectionInterval(int anchor, int lead) {
}
}
}
@_@
哇 搞定了~搞定了~
真是谢谢gtlang78()咯:) 嘿嘿嘿嘿 开心~