change the size of JTextField on KeyPress

前端 未结 4 1089
臣服心动
臣服心动 2021-01-28 07:12

I have to extend the size of JTextField on KeyPressed event as user enter the text in textfield.please give me some idea how to achieve this?

thanks in advance

4条回答
  •  猫巷女王i
    2021-01-28 07:40

    1) never to use KeyListener for JTextComponent, use DocumentListener, nothing else

    2) you can to use FontMetrics, TextLayout, SwingUtilities

    3) after resize you have to notify LayoutManager,

    4) if is there only JTextField then to use pack() for Top-Level Container,

    5) otherwise (in the case that there is more than one JPanel nested other JComponents) you have to re_layout whole Container with revalidate()and repaint() then

    • call pack() to the Top-Level Container if you want to resize continiously with its contens

    • don't call pack() to the Top-Level Container if you don't want to resize, but required usage of JScrollPane

    6) in the case that value of String could be very long, then to use proper JTextComponent with supporting multiline output to the GUI, to use JTextArea (in JScrollPane) rather than plain JTextField

提交回复
热议问题