我正在尝试重现Firefox或Safari的搜索字段或stackoverflow.com此页面右上角的搜索字段的行为.
我的意思是,当可编辑时没有文本时JComboBox,会显示指令文本,如"Type here"或者其他.当JComboBox聚焦时,文本被删除.如果在没有输入文本的情况下丢失焦点,则返回指令文本.
如何让我的一些JComboBox项目无法选择?我试过这个:
@Override
public Component getListCellRendererComponent(JList list, Object value,
int index. boolean isSelected, boolean cellHasFocus) {
Component comp = super.getListCellRendererComponent(list, value, index,
isSelected, cellHasFocus);
if (not selectable conditions) {
comp.setEnabled(false);
comp.setFocusable(false);
} else {
comp.setEnabled(true);
comp.setFocusable(true);
}
return comp;
}
Run Code Online (Sandbox Code Playgroud)
项目变为灰色,但仍可由用户选择.
我需要你对以下任务的建议和指导.
我有一个框架有两个JComboBoxes,假设它们被命名为combo1和combo2,一个JTable和其他组件.
在使用上述组件可见框架的初始阶段.combo1组合框中填充了一些值,但在初始阶段没有选择任何值,combo2组合框被禁用,表格为空.
我在combo1和combo2上添加了一个actionListener.combo1中有两种类型的值,假设这些值是type1和type2.
条件1:当我们从Combo1中选择值type1时,将调用actionListener方法combo1,该方法调用combo2保持禁用的方法,并将一些行添加到与combo1中的选定值type1相关的表中.
条件2:当我们从combo1中选择值type2时,将调用actionListener方法combo1,该方法调用一个方法,该方法使combo2填充了与type2相关的一些值并被启用但是没有从combo2中选择任何值,并且在我们选择之前表也应保持为空combo2中的任何值.
每次向combo2添加值时,表都会触发combo2的动作侦听器方法.在combo2的actionListener方法中,它获取了combo2选择的值,但是这里没有选择的combo2值导致NullPointerException.
那么我应该怎么做才能在将值添加到combo2之后才能执行combo2的动作列表器方法.
我有一个JComboBox有一些选项.当我在另一个组件上做出选择时,我正在改变JComboBox的内容.首先我调用该removeAllItems()方法,然后逐个添加我现在需要的字符串.
问题是,默认情况下我有一些选项,其中一个是较大的文本,因此JComboBox获得了正确显示该选项所需的宽度.当我更改JComboBox内容时,该文本选项消失了,现在一个较小的文本给了JComboBox的宽度,所以当我更改内容时它会变小.
我的第一种方法是调用myComboBox.setPreferredSize(myComboBox.getSize()),然后它的尺寸是固定的,但不正确:它的高度和宽度略小一些.我认为这是因为我正在使用Nimbus Look&Feel,而我从该getSize()方法获得的维度是Java默认的Look%Feel给出的维度.
我也试过myCombo.setPreferredSize(new Dimension(myCombo.getHeight(), myCombo.getWidth()))了同样的结果.
我该如何处理这个问题?
我添加了一个代码示例,说明我如何使用布局:
private String[] comboEventDOutputStrings = { "Run", "Stop", "Pause", "Conditioning time", "Deposition time", "Equilibration time", "Measurement time"};
private String[] comboEventDInputStrings = { "Run", "Stop", "Pause"};
// The first String array is the default set of values. It's obvious that the JComboBox will get smaller
// when I change to the second array of contents
//...
JPanel pane = new JPanel(new GridBagLayout());
JPanel jPanelExterno = …Run Code Online (Sandbox Code Playgroud) 使用什么方法返回用户选择的选择?
JPanel ageSelection = new JPanel();
JLabel age = new JLabel("Age:");
ArrayList<Integer> ageList = new ArrayList<Integer>();
for (int i = 1; i <= 100; ++i) {
ageList.add(i);
}
DefaultComboBoxModel<Integer> modelAge = new DefaultComboBoxModel<Integer>();
for (Integer i : ageList) {
modelAge.addElement(i);
}
JComboBox<Integer> ageEntries = new JComboBox<Integer>();
ageEntries.setModel(modelAge);
ageEntries.addActionListener(new putInTextListener());
ageSelection.add(age);
ageSelection.add(ageEntries);
class putInTextListener implements ActionListener {
public void actionPerformed (ActionEvent event) {
ageEntries.getSelectedItem();
}
}
Run Code Online (Sandbox Code Playgroud)
当添加最后一行(ageEntries.getSelectedItem();)时,我收到一个错误:
线程"AWT-EventQueue-0"中的异常java.lang.NullPointerException
有任何想法吗?
编辑代码:
class putInAgeListener implements ItemListener {
public void itemStateChanged(ItemEvent e) …Run Code Online (Sandbox Code Playgroud) 我使用下面的编码使用另一个jcombobox将值添加到jcombobox我需要根据jcombobox1中选择的一个值将值添加到jcombobox2而不附加值,所以有人可以告诉我重置或清除组合框的方法选择其他选项时的值?下面是我的编码,我是java和netbeans的新手,所以如果有人可以帮助我会感激不尽:)
Class.forName("com.mysql.jdbc.Driver").newInstance();
Connection con = DriverManager.getConnection("jdbc:mysql://localhost/database1", "root", "senura123");
Statement stat = (Statement) con.createStatement();
String val=jComboBox1.getSelectedItem().toString();
String check; String col;
if ("Vehicles".equals(val)){
check = "select reg_no from vehicle;";
col="reg_no";
}
else if ("Travel Guides".equals(val)){
check = "select username from travelguide;";
col="username";
}
else{
check = "select username from transportofficer";
col="username";
}
ResultSet rslt = stat.executeQuery(check);
while (rslt.next()) {
jComboBox2.addItem(rslt.getString(col));
}
}
Run Code Online (Sandbox Code Playgroud) 我正在创建一个包含表格和一些按钮的表单.
一张图片胜过千言万语:

如何将复选框和组合框放入表格中?
我正在使用NetBeans.我尝试使用拖放但没有工作.
这是我的表单代码.
public class HttpOutput extends javax.swing.JPanel {
HttpElements current_Http_EleObject;
/**
* Creates new form HttpOutput
*/
public HttpOutput(HttpElements httpelements) {
initComponents();
current_Http_EleObject=httpelements;
TableColumn includeColumn = jTable1.getColumnModel().getColumn(0);
includeColumn.setCellEditor(new DefaultCellEditor(new JCheckBox()));
}
Run Code Online (Sandbox Code Playgroud) 我在JTable的第3和第4列中有一个JComboBox,但我不知道如何获取它的项目...问题不是获取项目的方法,而是演员表
JComboBox combo=(JComboBox) jTable1.getColumnModel().getColumn(3).getCellEditor();
Run Code Online (Sandbox Code Playgroud)
你能帮我吗?
我需要在组合框中提供一些禁用的项目。除了防止在单击禁用的项目后关闭组合框之外,所有其他方法都工作正常。
这是我的代码:
import java.awt.Component;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import javax.swing.DefaultComboBoxModel;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JList;
import javax.swing.UIManager;
import javax.swing.WindowConstants;
import javax.swing.plaf.basic.BasicComboBoxRenderer;
import javax.swing.plaf.basic.ComboPopup;
public class DisabledCombo {
public static void main(String[] args) {
final DisabledSupportComboModel model = new DisabledSupportComboModel();
model.addElement(new Item("First element"));
model.addElement(new Item("Second element"));
model.addElement(new Item("Disabled", false));
model.addElement(new Item("Fourth element"));
final JComboBox<Item> itemCombo = new JComboBox<DisabledCombo.Item>(model);
itemCombo.setRenderer(new DisabledSupportComboRenderer());
final ComboPopup popup = (ComboPopup) itemCombo.getUI().getAccessibleChild(itemCombo, 0);
final JList<?> l = popup.getList();
final MouseListener[] listeners = l.getMouseListeners();
for …Run Code Online (Sandbox Code Playgroud) 如何让一个项目的组合框不可选择,因为我需要在一个组合框与子课题单独项目.
是否可以单独修改该特定项目的字体?
jComboBox_btech_course.setFont(new java.awt.Font("Tahoma", 0, 14));
jComboBox_btech_course.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Select Course" }));
jComboBox_btech_course.setName("");
private class theHandler implements ActionListener
{
public void actionPerformed(ActionEvent evt)
{
//BTech courses
if(jComboBox_mtech_dept.getSelectedItem().equals("Civil Engineering"))
{
jComboBox_btech_course.removeAllItems();
jComboBox_btech_course.addItem("Building Construction");
jComboBox_btech_course.addItem("Principle And Practice");
jComboBox_btech_course.addItem("Surveying");
jComboBox_btech_course.addItem("Engineering Geology");
jComboBox_btech_course.addItem("Structural Analysis");
jComboBox_btech_course.addItem("Hydraulic Engineering");
jComboBox_btech_course.addItem("Environmental Engineering");
jComboBox_btech_course.addItem("Structural Design");
jComboBox_btech_course.addItem("Geotechnical Engineering");
/*This item has to be unselectable*/
jComboBox_btech_course.addItem("***Sub-topic***");
jComboBox_btech_course.addItem("Transportation Engineering");
jComboBox_btech_course.addItem("Foundation Engineering");
jComboBox_btech_course.addItem("Estimation & Valuation");
jComboBox_btech_course.addItem("Hydrology & Flood Control");
jComboBox_btech_course.addItem("System Analysis, Project Planning And Construction Management");
jComboBox_btech_course.addItem("Irrigation Engineering"); …Run Code Online (Sandbox Code Playgroud)