2017-03-17 1 views
1

私はaplicationをやろうとしているが、例えば、このような何かを行うことがかのうである場合、私は知らない:enter image description hereJTextAreaまたはJTextPaneをクリックして展開することは可能ですか?

ので長方形がJTextAreaに(あるいはJTextPaneの)であり、それは固定されていzise、それはサスペンションポイントがある理由ですが、私はこのようにそれにclikcとき:我々はJTextAreaに(あるいはJTextPaneの)の拡大を得たが、フォーカスがlostedされたとき、それは時に戻ってくる

enter image description here

開始:

enter image description here

テキストはテキストが長すぎるときに、自動的に追加し、何もすることができます「...」最後に

+1

はい、おそらく、 'JTextArea'の' setRows'メソッドを見ることができます。 'JTextPane'はもっと難しいでしょう – MadProgrammer

答えて

0

FocusListener

JTextFieldを追加および削除するのJPanelを使用しJTextAreaenter image description here enter image description here

1行の入力にはJTextFieldを使用し、 "..."を追加するにはKeyListenerインターフェイスを実装する必要があります。

JTextFieldでGainFocusを設定するたびに設定されるグローバルフラグを使用します。スレッドはこのフラグをチェックし、設定されている場合はJPanelからJTextFieldオブジェクトを削除し、保存されたStringでJTextAreaを追加します。

OR

代わりに、より複雑なスレッド物のJPanelのにFocusListnerを実装することができます。

1

別のオプションはデフォルトJLabel切り捨て機能を使用するために、代わりにJTextFieldCardLayoutとフォーカス可能JLabelを使用することです:

screenshot

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

public class TextAreaExpandTest { 
    private static final String TEXT = 
    "The text can be anything, so when the text is too long," + 
    " automatically add '...' at the end."; 
    public JComponent makeUI() { 
    CardLayout cardLayout = new CardLayout(); 
    JPanel cp = new JPanel(cardLayout); 

    JTextArea textArea = new JTextArea(TEXT, 5, 10) { 
     @Override public void updateUI() { 
     super.updateUI(); 
     setLineWrap(true); 
     setWrapStyleWord(true); 
     setMargin(new Insets(1, 1, 1, 1)); 
     } 
    }; 

    JLabel textField = new JLabel(" ") { 
     @Override public void updateUI() { 
     super.updateUI(); 
     setOpaque(true); 
     setFocusable(true); 
     setBackground(UIManager.getColor("TextField.background")); 
     setForeground(UIManager.getColor("TextField.foreground")); 
     setBorder(UIManager.getBorder("TextField.border")); 
     } 
    }; 

    textArea.addFocusListener(new FocusAdapter() { 
     @Override public void focusLost(FocusEvent e) { 
     String text = textArea.getText(); 
     textField.setText(text.isEmpty() ? " " : text); 
     cardLayout.show(cp, "TextField"); 
     } 
    }); 
    textField.addFocusListener(new FocusAdapter() { 
     @Override public void focusGained(FocusEvent e) { 
     cardLayout.show(cp, "TextArea"); 
     textArea.requestFocusInWindow(); 
     } 
    }); 
    textField.addMouseListener(new MouseAdapter() { 
     @Override public void mousePressed(MouseEvent e) { 
     cardLayout.show(cp, "TextArea"); 
     textArea.requestFocusInWindow(); 
     } 
    }); 
    JPanel panel = new JPanel(new BorderLayout()); 
    panel.add(textField, BorderLayout.NORTH); 
    JScrollPane scroll = new JScrollPane(
     textArea, 
     ScrollPaneConstants.VERTICAL_SCROLLBAR_NEVER, 
     ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER); 
    cp.add(panel, "TextField"); 
    cp.add(scroll, "TextArea"); 

    JPanel p = new JPanel(new BorderLayout()); 
    p.setBorder(BorderFactory.createEmptyBorder(32, 32, 32, 32)); 
    p.add(cp, BorderLayout.NORTH); 
    p.add(new JButton("focus dummy"), BorderLayout.SOUTH); 
    return p; 
    } 
// //TEST: JTextArea"setRows(...) 
// public JComponent makeUI2() { 
//  JPanel p = new JPanel(new BorderLayout()); 
//  JTextArea textArea = new JTextArea("", 1, 10); 
//  textArea.setLineWrap(true); 
//  textArea.addFocusListener(new FocusListener() { 
//  @Override public void focusGained(FocusEvent e) { 
//   JTextArea ta = (JTextArea) e.getComponent(); 
//   ta.setRows(8); 
//   p.revalidate(); 
//  } 
//  @Override public void focusLost(FocusEvent e) { 
//   JTextArea ta = (JTextArea) e.getComponent(); 
//   ta.setRows(1); 
//   p.revalidate(); 
//  } 
//  }); 
//  JScrollPane scroll = new JScrollPane(
//  textArea, 
//  ScrollPaneConstants.VERTICAL_SCROLLBAR_NEVER, 
//  ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER); 
//  p.add(scroll, BorderLayout.NORTH); 
//  p.add(new JButton("focus dummy"), BorderLayout.SOUTH); 
//  p.setBorder(BorderFactory.createEmptyBorder(20, 20, 20, 20)); 
//  return p; 
// } 
    public static void main(String... args) { 
    EventQueue.invokeLater(() -> { 
     UIManager.put("swing.boldMetal", Boolean.FALSE); 
     JFrame f = new JFrame(); 
     f.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); 
     f.getContentPane().add(new TextAreaExpandTest().makeUI()); 
     f.setSize(320, 240); 
     f.setLocationRelativeTo(null); 
     f.setVisible(true); 
    }); 
    } 
} 
0

すべては、あなたが使用しているレイアウトに依存します。私が行うことは、そのコンポーネントのpreferredSizeを考慮したレイアウトを使用することです。次に、ブールパラメータ(拡張または拡張されていない)を指定して、JTextPaneのpreferredSizeを変更します。

私はpreferredSizeを変更すると言うとき、私はgetPreferredSizeメソッドをオーバーライドすることを意味します。

関連する問題