4

いくつかのオプションがあるJComboBoxがあります。別のコンポーネントで選択すると、JComboBoxの内容が変更されます。最初にメソッドを呼び出し、removeAllItems()次に必要な文字列を1つずつ追加します。

問題は、デフォルトでいくつかのオプションがあり、そのうちの1つはより大きなテキストだったため、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 = new JPanel(new GridBagLayout());
            GridBagConstraints cExterna = new GridBagConstraints();
            GridBagConstraints c = new GridBagConstraints();
            Border loweredetched = BorderFactory.createEtchedBorder(EtchedBorder.LOWERED);
            jPanelExterno.setBorder(loweredetched);
            jPanelExterno.setName("");

            cExterna.fill = GridBagConstraints.BOTH;
            cExterna.anchor = GridBagConstraints.NORTH;
            cExterna.gridx = 0;
            cExterna.gridy = 0;
            cExterna.insets = new Insets(10,10,5,5);

                JPanel jPanel1 = new JPanel(new GridBagLayout());
                jPanel1.setBorder(loweredetched);
                jPanel1.setName("PIO 1");

                jCheckBox1 = new JCheckBox("Enable");
                jCheckBox1.setSelected(false);
                jCheckBox1.setName("1");
                jCheckBox1.addActionListener(new PIOCheckListener());

                c.fill = GridBagConstraints.BOTH;
                c.anchor = GridBagConstraints.NORTH;
                c.gridx = 0;
                c.gridy = 0;
                c.insets = new Insets(10,5,10,10);
                jPanel1.add(jCheckBox1, c);

                c.gridy++;
                c.insets = new Insets(5,10,5,5);
                JLabel label1 = new JLabel("IO Type");
                jPanel1.add(label1, c);
                c.gridx++;
                c.insets = new Insets(5,5,5,10);

                combo1 = new JComboBox(comboIOTypeStrings);
                combo1.setEnabled(false);
                combo1.setSelectedIndex(0);
                combo1.setName("1");
                combo1.addActionListener (new PIOComboListener());
                jPanel1.add(combo1, c);

                c.gridy++;
                c.insets = new Insets(5,10,5,5);
                c.gridx=0;
                JLabel label2 = new JLabel("Active level");
                jPanel1.add(label2, c);
                c.gridx++;
                c.insets = new Insets(5,5,5,10);

                combo2 = new JComboBox(comboActiveLevelStrings);
                combo2.setEnabled(false);
                combo2.setSelectedIndex(0);
                jPanel1.add(combo2, c);

                c.gridy++;
                c.insets = new Insets(5,10,5,5);
                c.gridx=0;
                JLabel label3 = new JLabel("Event");
                jPanel1.add(label3, c);
                c.gridx++;
                c.insets = new Insets(5,5,10,10);

                combo3 = new JComboBox(comboEventDOutputStrings);
                combo3.setEnabled(false);
                combo3.setSelectedIndex(0);
                jPanel1.add(combo3, c);

                c.gridy++;
                c.insets = new Insets(5,10,5,5);
                c.gridx=0;
                JLabel label4 = new JLabel("Node");
                jPanel1.add(label4, c);
                c.gridx++;
                c.insets = new Insets(5,5,10,10);

                combo4 = new JComboBox(comboNodeStrings);
                combo4.setEnabled(false);
                combo4.setSelectedIndex(0);
                jPanel1.add(combo4, c);

            jPanelExterno.add(jPanel1, cExterna);

        pioDialog.add(pane);
        pioDialog.pack();
        pioDialog.setLocationRelativeTo(null);
        pioDialog.setVisible(true);
    //...
}   


    class PIOComboListener implements ActionListener{

        @Override
        public void actionPerformed(ActionEvent a) {
            JComboBox cb = (JComboBox)a.getSource();
            JComboBox target1 = null;
            JComboBox target2 = null;
            JComboBox target3 = null;
            switch(Integer.parseInt(cb.getName())){
                case 1:
                    target1 = combo2;
                    target2 = combo3;
                    target3 = combo4;
                    break;
                default:
                    Register.debug("PIODialog error: No target for comboBoxes");
                    break;
            }

            if(cb.getSelectedIndex()==2){ //Analog input
                target1.setEnabled(false);
                target2.setEnabled(false);
                target3.setEnabled(false);
            }
            else{
                target1.setEnabled(true);
                target2.setEnabled(true);
                target3.setEnabled(true);
                target2.removeAllItems();
                if(cb.getSelectedIndex()==0){
                    for(int i=0; i<comboEventDOutputStrings.length; i++)
                        target2.addItem(comboEventDOutputStrings[i]);
                } else {
                    for(int i=0; i<comboEventDInputStrings.length; i++)
                        target2.addItem(comboEventDInputStrings[i]);
                }
            }

        }
    }

これは基本的に、6つのJPanelを備えたGridBagLayoutであり、それぞれに新しいGridBagLayoutがあります。物事を単純化するために、ここにjPanel1を記述しました。あまり面倒ではないといいのですが。

4

3 に答える 3

16

最後に私は簡単な解決策を見つけました:

jComboBox1.setPrototypeDisplayValue("XXXXXXXXXXXXXXXXXX");

そして、私が欲しかったものにぴったりです

于 2011-10-19T11:59:27.367 に答える
1

NetBeansは、コンポーネントの右クリックコンテキストメニューのいくつかのチェックボックス(自動サイズ変更->水平および垂直)を使用してこれを行います。

それらをオフにして、リビジョン管理履歴でNetBeansで生成されたコードを比較しました。コンポーネントのサイズとは関係のない驚くべき数の変更がありましたが、コンボボックスが含まれているレイアウトグループに追加される方法に変更が見られました。本質的に、

....addComponent(destinationFolderComboBox, 0, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)

に変更されました

....addComponent(myComboBox, javax.swing.GroupLayout.PREFERRED_SIZE, 514, javax.swing.GroupLayout.PREFERRED_SIZE)

JComboBox.setPreferredSize()が明示的に呼び出されることはないため、JComboBoxに通知してレイアウトマネージャーがこれらのプロパティをバックグラウンドで読み取ることを期待するよりも、レイアウトマネージャーに優先サイズとサイズ変更プロパティを通知する方が良い結果が得られると思います。

(ちなみに、は....addComponent()、とaddComponent()の長いカスケードシーケンスで呼び出されます。最初は。です。自動生成されたNetBeansコードの核心に触れたくなかったので、上記の短い形式を示しました。手動で書き始めることも説明することもできませんでした。全部が欲しいですか?全部を処理することはできません!ここにあります:createParallelGroup()createSequentialGroup()new javax.swing.GroupLayout(myPanel)

    javax.swing.GroupLayout taskPanelLayout = new javax.swing.GroupLayout(taskPanel);
    taskPanel.setLayout(taskPanelLayout);
    taskPanelLayout.setHorizontalGroup(
        taskPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
        .addGroup(taskPanelLayout.createSequentialGroup()
            .addContainerGap()
            .addGroup(taskPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, taskPanelLayout.createSequentialGroup()
                    .addGroup(taskPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addComponent(jScrollPane1)
                        .addGroup(taskPanelLayout.createSequentialGroup()
                            .addGroup(taskPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                                .addComponent(tagModifierLabel, javax.swing.GroupLayout.Alignment.LEADING)
                                .addComponent(jLabel1, javax.swing.GroupLayout.Alignment.LEADING))
                            .addGap(18, 18, 18)
                            .addGroup(taskPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                .addComponent(destinationFolderComboBox, javax.swing.GroupLayout.PREFERRED_SIZE, 514, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addComponent(tagModifierTextField, javax.swing.GroupLayout.PREFERRED_SIZE, 514, javax.swing.GroupLayout.PREFERRED_SIZE))))
                    .addGap(18, 18, 18)
                    .addGroup(taskPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addComponent(importFilesButton)
                        .addComponent(selectFilesButton)
                        .addComponent(clearFilesListButton)
                        .addComponent(commitChangesButton)))
                .addGroup(taskPanelLayout.createSequentialGroup()
                    .addComponent(importFilesLabel, javax.swing.GroupLayout.PREFERRED_SIZE, 103, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGap(0, 0, Short.MAX_VALUE)))
            .addContainerGap())
    );

)。

于 2013-05-27T19:11:31.127 に答える
0

JComboBoxjpanel1にtoを追加する前の時点で、 (ではなく) toのfill属性を設定してみてください。これにより、(うまくいけば)コンボボックスのサイズ変更ができなくなります。GridBagConstraintsNONEBOTH

c.fill = GridBagConstraints.NONE;
jPanel1.add(combo1, c);
于 2011-10-19T08:37:52.747 に答える