2016-04-02 4 views
1

JCheckBoxのチェックボックスだけを別のラベルとチェックボックスコンポーネントを作成せずに右に配置する簡単な方法はありますか?私は認識していますsetHorizontalTextPosition(SwingConstants.LEADING);これは、ボックスがテキストの右側にありますが、右揃えにはなりません。親の反対側のJCheckBoxのテキストとチェックボックスを整列

私が欲しいもの

:代わりに、事前に

| Some sample text ☑           | 

感謝の

| Some sample text           ☑ | 

+0

また、2列 'JTable'は最後の列にタイプ' Boolean.class'のモデル値を有することを検討してください。 – trashgod

答えて

2

手動でチェックボックスのサイズが変更ComponentListenerにあるチェックボックスを追加することで、アイコンのテキストギャップを毎回操作することができます。

import java.awt.*; 
import java.awt.event.*; 
import javax.swing.*; 

public class SSCCE extends JPanel 
{ 
    public SSCCE() 
    { 
     setLayout(new BorderLayout()); 

     JCheckBox checkBox = new JCheckBox("Some Sample Text"); 
     checkBox.setHorizontalTextPosition(JCheckBox.LEADING); 
     add(checkBox, BorderLayout.PAGE_START); 

     checkBox.addComponentListener(new ComponentAdapter() 
     { 
      @Override 
      public void componentResized(ComponentEvent e) 
      { 
       JCheckBox checkBox = (JCheckBox)e.getComponent(); 
       int preferredWidth = getPreferredSize().width; 
       int actualWidth = getSize().width; 
       int difference = actualWidth - preferredWidth; 
       int gap = checkBox.getIconTextGap() + difference; 
       gap = Math.max(UIManager.getInt("CheckBox.textIconGap"), gap); 
       checkBox.setIconTextGap(gap); 
      } 
     }); 

    } 

    private static void createAndShowGUI() 
    { 
     JFrame frame = new JFrame("SSCCE"); 
     frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
     frame.add(new SSCCE()); 
     frame.setLocationByPlatform(true); 
     frame.pack(); 
     frame.setVisible(true); 
    } 

    public static void main(String[] args) 
    { 
     EventQueue.invokeLater(() -> createAndShowGUI()); 
/* 
     EventQueue.invokeLater(new Runnable() 
     { 
      public void run() 
      { 
       createAndShowGUI(); 
      } 
     }); 
*/ 
    } 
} 
2

あなたは、適切なレイアウトでラベルやチェックボックスを分離することができます。私は囲み枠を任意の要素で広げてその効果を見せました。

enter image description here

import java.awt.BorderLayout; 
import java.awt.Color; 
import java.awt.EventQueue; 
import java.awt.GridLayout; 
import javax.swing.BorderFactory; 
import javax.swing.JCheckBox; 
import javax.swing.JFrame; 
import javax.swing.JLabel; 
import javax.swing.JPanel; 

/** @see http://stackoverflow.com/a/2933256/230513 */ 
public class Test { 

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

      @Override 
      public void run() { 
       JFrame frame = new JFrame(); 
       frame.setLayout(new GridLayout(0, 1)); 
       frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
       frame.add(createPanel("Some label")); 
       frame.add(createPanel("Another label")); 
       frame.add(createPanel("Yet another label")); 
       frame.pack(); 
       frame.setSize(frame.getWidth() * 2, frame.getHeight()); 
       frame.setLocationByPlatform(true); 
       frame.setVisible(true); 
      } 

      private JPanel createPanel(String s) { 
       JPanel p = new JPanel(new BorderLayout()); 
       p.add(new JLabel(s, JLabel.LEFT), BorderLayout.WEST); 
       p.add(new JCheckBox(), BorderLayout.EAST); 
       p.setBorder(BorderFactory.createLineBorder(Color.blue)); 
       return p; 
      } 
     }); 
    } 
} 
関連する問題