2012-02-23 5 views
2

JDialogを拡張するクラスを作成しました。そこにはチェックボックスと3つのボタンがあります:受け入れ、キャンセル、すべてを選択します。クリックしたときのJButtonテキストの変更

すべて選択をクリックすると、すべてのチェックボックスがオンになり、もう一度クリックするとすべてのチェックボックスがオフになります。それはうまくいきますが、ボタンのテキストを「すべて選択」と「すべてを選択解除」の間で変更したいこともあります。そこに問題があるので、ユーザーがボタンをクリックしてテキストが「すべて選択解除」に変わると、ボタンが消えます。

私はここで最も単純な形にクラスが低下している:私が間違っているものを見ることができない

public class NodeSelectionCheckBoxJDialog extends JDialog { 
    public enum Options {ACEPT, CANCEL}; 
    private Options selectedOption; 
    private JButton allButton; 
    private boolean allCheckBoxesSelected; 
    private JButton aceptButton; 

    public NodeSelectionCheckBoxJDialog(){ 
     super(MainFrame.getInstance()); 
     this.setTitle("Select nodes to apply"); 
     this.setModal(true); 

     selectedOption = Options.CANCEL; 
     nodeCheckBoxesSet = new HashSet<NodeCheckBox>(); 

     try { 
      initComponents(); 
     } catch (Exception e) { 
      JOptionPane.showMessageDialog(null, e.getMessage(), "Error", JOptionPane.ERROR_MESSAGE); 
     } 

     this.pack(); 
     this.setLocationRelativeTo(null); 
     this.setVisible(true); 
    } 

    private void initComponents() throws Exception { 
     this.getContentPane().add(createActionButtons(), BorderLayout.SOUTH); 
    } 

    private Component createActionButtons() { 
     JPanel buttonsPanel = new JPanel(); 
     allCheckBoxesSelected = false; 
     aceptButton = new JButton("Accept"); 
     aceptButton.setEnabled(false); 
     buttonsPanel.add(aceptButton); 
     aceptButton.addActionListener(new ActionListener() { 

      @Override 
      public void actionPerformed(ActionEvent e) { 
       selectedOption = Options.ACEPT; 
       dispose(); 
      } 
     }); 

     JButton cancelButton = new JButton("Cancel"); 
     buttonsPanel.add(cancelButton); 
     cancelButton.addActionListener(new ActionListener() { 

      @Override 
      public void actionPerformed(ActionEvent e) { 
       selectedOption = Options.CANCEL; 
       dispose(); 
      } 
     }); 

     allButton = new JButton("Select all"); 
     buttonsPanel.add(allButton); 
     allButton.addActionListener(new ActionListener() { 

      @Override 
      public void actionPerformed(ActionEvent e) { 
       if(allCheckBoxesSelected){ 
        allCheckBoxesSelected = false; 
        allButton.setText("Select all"); 
       } else { 
        allCheckBoxesSelected = true; 
        allButton.setText("Unselect all"); 
       } 
      } 
     }); 

     return buttonsPanel; 
    } 
} 

。どんな助け?ボタンが消えないクリックされたボタンに

+0

のための興味深い方法をimplemeted使用ButtonModelですあなたのコードの残りの部分は?どのようにチェックボックスを選択/選択解除していますか? 'allButton'変数を別の場所で操作していますか? – Marcelo

+0

私はチェックボックスのリスナーを持っていますが、コードを簡単にするためにそれを消去しました。選択/選択解除は正常に機能しますが、ボタンのテキストを変更しようとしても問題が表示されます。そして、いいえ、allButtonは他の場所で操作されていません –

答えて

5

にアクセスするための

+0

作品!ありがとう:) –

1

event.getSource()、それだけで、ウィンドウに収まるようにあまりにも広くなります。ボタンのラベルを変更するときだけコンポーネントを再描画:

@Override 
public void actionPerformed(ActionEvent e) { 
    if(allCheckBoxesSelected){ 
     allCheckBoxesSelected = false; 
     allButton.setText("Select all"); 
    } else { 
     allCheckBoxesSelected = true; 
     allButton.setText("Unselect all"); 
     NodeSelectionCheckBoxJDialog.this.pack(); 
    } 
} 
+0

これは私がそれに近づいているのと同じです。コンパイルされますが、問題はそのまま残り、ボタンは消えます。 –

2

別の方法は、たとえば

enter image description here

enter image description here

enter image description here

import java.awt.*; 
import java.awt.event.*; 
import javax.swing.*; 
import javax.swing.border.AbstractBorder; 
import javax.swing.border.Border; 
import javax.swing.event.ChangeEvent; 
import javax.swing.event.ChangeListener; 

public class TextAreaInButton { 

    private JFrame frame = new JFrame("sssssssss"); 
    private JButton tip1Null = new JButton(" test button "); 

    public TextAreaInButton() { 
     Border line, raisedbevel, loweredbevel, title, empty; 
     line = BorderFactory.createLineBorder(Color.black); 
     raisedbevel = BorderFactory.createRaisedBevelBorder(); 
     loweredbevel = BorderFactory.createLoweredBevelBorder(); 
     title = BorderFactory.createTitledBorder(""); 
     empty = BorderFactory.createEmptyBorder(1, 1, 1, 1); 
     final Border compound; 
     Color crl = (Color.blue); 
     compound = BorderFactory.createCompoundBorder(empty, new OldRoundedBorderLine(crl)); 
     Color crl1 = (Color.red); 
     final Border compound1; 
     compound1 = BorderFactory.createCompoundBorder(empty, new OldRoundedBorderLine(crl1)); 
     Color crl2 = (Color.black); 
     final Border compound2; 
     compound2 = BorderFactory.createCompoundBorder(empty, new OldRoundedBorderLine(crl2)); 
     tip1Null.setFont(new Font("Serif", Font.BOLD, 14)); 
     tip1Null.setForeground(Color.darkGray); 
     tip1Null.setPreferredSize(new Dimension(50, 30)); 
     tip1Null.addActionListener(new java.awt.event.ActionListener() { 

      @Override 
      public void actionPerformed(ActionEvent e) { 
      } 
     }); 
     tip1Null.setBorderPainted(true); 
     tip1Null.setFocusPainted(false); 
     tip1Null.setBorder(compound); 
     tip1Null.setHorizontalTextPosition(SwingConstants.CENTER); 
     tip1Null.setVerticalTextPosition(SwingConstants.BOTTOM); 

     tip1Null.getModel().addChangeListener(new ChangeListener() { 

      @Override 
      public void stateChanged(ChangeEvent e) { 
       ButtonModel model = (ButtonModel) e.getSource(); 
       if (model.isRollover()) { 
        tip1Null.setBorder(compound1); 
       } else { 
        tip1Null.setBorder(compound); 
       } 
       if (model.isPressed()) { 
        tip1Null.setBorder(compound2); 
        String btnText = (tip1Null.getText()); 
        if (btnText.equals("Selected")) { 
         tip1Null.setText("Un_Selected"); 
        } else { 
         tip1Null.setText("Selected"); 
        } 
       } 
      } 
     }); 

     frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
     frame.add(tip1Null, BorderLayout.CENTER); 
     frame.setLocation(150, 150); 
     frame.setPreferredSize(new Dimension(310, 75)); 
     frame.setLocationRelativeTo(null); 
     frame.pack(); 
     frame.setVisible(true); 
    } 

    public static void main(String args[]) { 
     EventQueue.invokeLater(new Runnable() { 

      @Override 
      public void run() { 
       TextAreaInButton taib = new TextAreaInButton(); 
      } 
     }); 
    } 
} 

class OldRoundedBorderLine extends AbstractBorder { 

    private final static int MARGIN = 5; 
    private static final long serialVersionUID = 1L; 
    private Color color; 

    OldRoundedBorderLine(Color clr) { 
     color = clr; 
    } 

    public void setColor(Color clr) { 
     color = clr; 
    } 

    @Override 
    public void paintBorder(Component c, Graphics g, int x, int y, int width, int height) { 
     ((Graphics2D) g).setRenderingHint(
       RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); 
     g.setColor(color); 
     g.drawRoundRect(x, y, width, height, MARGIN, MARGIN); 
    } 

    @Override 
    public Insets getBorderInsets(Component c) { 
     return new Insets(MARGIN, MARGIN, MARGIN, MARGIN); 
    } 

    @Override 
    public Insets getBorderInsets(Component c, Insets insets) { 
     insets.left = MARGIN; 
     insets.top = MARGIN; 
     insets.right = MARGIN; 
     insets.bottom = MARGIN; 
     return insets; 
    } 
} 
関連する問題