我正在寻找创建一个充当菜单的JComboBox.例如,当您放下它时,您可以像在JMenuBar中一样选择项目.
所以它需要JMenus和JMenuItems而不是字符串.
这可能吗?
我想让JComboBox从右到左对齐并从右到左呈现文本我已经设置了组件方向并且还更改了渲染,如下所示:
import java.awt.Component;
import java.awt.ComponentOrientation;
import javax.swing.DefaultListCellRenderer;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.SwingConstants;
public class NewJFrame extends javax.swing.JFrame {
/** Creates new form NewJFrame */
public NewJFrame() {
initComponents();
jComboBox1.setComponentOrientation(ComponentOrientation.RIGHT_TO_LEFT);
jComboBox1.setRenderer(new DefaultListCellRenderer() {
public Component getListCellRendererComponent(JList jList, Object o,
int i, boolean b, boolean b1) {
JLabel rendrlbl = (JLabel) super.getListCellRendererComponent(jList, o, i, b, b1);
rendrlbl.setHorizontalAlignment(SwingConstants.RIGHT);
return rendrlbl;
}
});
}
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {
jComboBox1 = new javax.swing.JComboBox();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
jComboBox1.setEditable(true);
jComboBox1.setModel(new javax.swing.DefaultComboBoxModel(new String[] …Run Code Online (Sandbox Code Playgroud) 我想从数据库ID添加组合框索引.
public static void selectCompany(javax.swing.JComboBox cmbCategory ){
cmbCategory.removeAllItems();
String sql="SELECT * FROM company_details";
try{
Connection conn=dbConnection();
PreparedStatement pstmt=conn.prepareStatement(sql);
ResultSet rs=pstmt.executeQuery(sql);
while(rs.next()){
int id=rs.getInt("company_id");
String category=rs.getString("company_name");
cmbCategory.addItem(id);
cmbCategory.addItem(category);
}
}catch(Exception e){
JOptionPane.showMessageDialog(null, e);
}
}
Run Code Online (Sandbox Code Playgroud)
cmbCategory是组合框对象.我想将组合框索引和组合框列表显示为类别名称.数据库是mysql.
Run Code Online (Sandbox Code Playgroud)public class CursorAtStartFocusListener extends FocusAdapter { @Override public void focusGained(java.awt.event.FocusEvent evt) { Object source = evt.getSource(); if (source instanceof JTextComponent) { JTextComponent comp = (JTextComponent) source; comp.setCaretPosition(0); comp.selectAll(); } } }jComboBox.getEditor().getEditorComponent().addFocusListener(new CursorAtStartFocusListener());
从上面的代码中可以看出,我想在可编辑的JComboBox中选择所有文本,并将光标位置设置为开头.但是我有问题,如果我先写comp.setCaretPosition(0)然后comp.selectAll(),文本被选中但是光标在文本的末尾,否则如果我先写comp.selectAll()然后comp.setCaretPosition(0 ),我将光标放在我想要的位置,但未选择文本.知道我该怎么做这件事?
所以我要求基于JComboBox中项目的选择,我需要向用户显示选择确认对话框.我做的是添加一个ItemListener并基于某个逻辑,我弹出这个对话框.
我面临的相当棘手的问题是对话框首先弹出(即使ComboBox项目选择打开),我要点击两次以确认我的选择.第一个是关闭ComboBox弹出窗口,第二个是确认对话框中的实际弹出窗口.
这是SSCCE突出我的问题:
import java.awt.event.ItemEvent;
import javax.swing.JOptionPane;
public class TestFrame extends javax.swing.JFrame {
/**
* Creates new form TestFrame
*/
public TestFrame() {
initComponents();
}
/**
* This method is called from within the constructor to initialize the form.
* WARNING: Do NOT modify this code. The content of this method is always
* regenerated by the Form Editor.
*/
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
private void initComponents() {
jLabel1 = new javax.swing.JLabel();
selectCombo = new javax.swing.JComboBox(); …Run Code Online (Sandbox Code Playgroud) 我从来没有在java中编写GUI.这次我也可以跳过它并args用作UI(用户界面).但我想知道是否有一种简单的方法可以创建一个小的GUI来让用户选择其中一个选项.换句话说,要实现askUser()用户可以从下拉菜单中选择并按"确定"的功能.我花了一些时间学习这个主题,但甚至不确定我知道这个任务需要哪种类型的GUI.JFrame的?JPanel的?JMenu的?谢谢.
这是所需功能的一个例子.
package trygui;
public class Main {
public static void main(String[] args) {
String[] choices = new String[]{"cats", "dogs"};
int choice = askUser(choices);
System.out.println("selected: " + choices[choice]);
}
static int askUser(String[] choices) {
// create pop-up dialog
return 0;
}
}
Run Code Online (Sandbox Code Playgroud)
更新:我使用Netbeans,如果这可以有所作为.
我有可编辑的JCombobox,我为组合框编辑器组件添加了keylistener.当用户按下"Enter键"并且可编辑组合框上没有文本时,我需要使用JOptinoPane显示消息框.我已经在keyrelease事件中完成了必要的代码,它会按预期显示消息.
问题是,当我们收到消息框并且如果用户在JOptionPane的"确定"按钮上按回车键,组合框编辑器keyevent再次触发.因此,当用户在消息框上按Enter键时,JoptionPane会持续显示.
不知道怎么解决这个问题?
请注意,我不能使用Action侦听器.
我发现,当一个人希望用户从中选择jcombobox时,一种有用的方法是让它在通常在用户完成前一项时获得焦点时下拉.怎么能在Java中完成?
我想创建一个只读组合框.用户不应该从弹出列表中选择另一个项目.这意味着弹出列表不应该打开或应该为空.
我看到以下解决方案:
设置仅包含一个项目(当前所选项目)的ComboBox模型,因此当用户单击箭头按钮时,将显示一个空列表.
添加一个PopupMenuListener并popupMenuWillBecomeVisible隐藏菜单.这是有问题的:我们必须combo.hidePopup();从内部打电话SwingUtilities.invokeLater()
空模型方法看起来有点笨重.第二种方法显示弹出列表只有几分之一秒,足够短以至于被注意到.这非常难看.
有第三种解决方案吗?
编辑:已实施的解决方案:
我从splungebob实现了建议的方法,这是我的代码供将来参考:
private void makeComboReadonly() {
Component editorComponent = box.getEditor().getEditorComponent();
if (editorComponent instanceof JTextField) {
((JTextField) editorComponent).setEditable(false);
}
for (Component childComponent : box.getComponents()) {
if (childComponent instanceof AbstractButton) {
childComponent.setEnabled(false);
final MouseListener[] listeners = childComponent.getListeners(MouseListener.class);
for (MouseListener listener : listeners) {
childComponent.removeMouseListener(listener);
}
}
}
final MouseListener[] mouseListeners = box.getListeners(MouseListener.class);
for (MouseListener listener : mouseListeners) {
box.removeMouseListener(listener);
}
final KeyListener[] keyListeners = box.getListeners(KeyListener.class);
for (KeyListener …Run Code Online (Sandbox Code Playgroud) 
我想将jcombobox添加到jtable请有人解释我怎么能在netbeans中做到这一点
java ×10
jcombobox ×10
swing ×9
joptionpane ×2
cursor ×1
enter ×1
itemlistener ×1
jmenu ×1
jtable ×1
jtextfield ×1
keylistener ×1
mysql ×1
netbeans ×1
orientation ×1
popup ×1
selection ×1