动态字段添加在java / swing窗体中 [英] Dynamic fields addition in java/swing form

查看:198
本文介绍了动态字段添加在java / swing窗体中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我想要做的是...



的形式。有一个jComboBox和一个JTextField,用户可以输入他正在选择的服务和一个观察。到现在为止还挺好。 JComboBox从数据库条目中填充。



问题是,用户可以一次输入N个不同的服务(有太多的复选框)。我正在考虑添加一个[+]按钮(以及一个[ - ]来删除)。因此,用户点击[+],另一个新行与jcombobox + jtextfield出现在前面的。



我坚持到这一点。在[+]按钮ActionPerformed我不能克隆和添加以前的节点。任何关于如何进行的想法。



我的背景是webdev。用javascript做这个会很快。嗯,我想你已经知道我在做什么。等待一些光Thx。

解决方案

你在正确的轨道上。以下是源代码给您一些想法



基本思想是EntryList负责跟踪要显示的行;每行都有一个加号/减号按钮,然后将实际的添加/删除委托给该EntryList。它还暴露了禁用减号/加号按钮的方法,以便列表视图可以确保您不删除单个条目(以便您没有空的显示)



这不能正常工作您会注意到需要调整框架大小以使新行正确显示。但这应该足以让你开始。


I'm pretty new to java, and using netbeans for design a UI.

What I am trying to do is...

in the form. there are a jComboBox and a JTextField, where user can input the service he is choosing and an observation. So far so good. JComboBox is populated from database entries.

The problem is, a user can input N different services at once (there are too much to be a bunch of checkboxes). I was thinking into add a "[+]" button (along with a "[-]" for removal). Thus, users click on [+] and another new line with a jcombobox + jtextfield appear right below the previous ones.

I'm stucked at this point. On [+] button ActionPerformed I just can't clone and add previous nodes. Any idea on how proceed.

My background is webdev. Doing this with javascript would be really quick. Well, I think you already know what I'm trying to do. Waiting for some light. Thx.

解决方案

You're on the right track. Here's some source code to give you some ideas

The basic idea is that the EntryList is responsible for keeping track of the rows to display; each row has a plus/minus button, and then delegates out the actual adding/removing to this EntryList. It also exposes methods to disable the minus/plus button so that the list view can ensure that you don't remove a single entry (so that you don't have an empty display)

This doesn't work perfectly; you'll notice you need to resize the frame to get the new rows to show up correctly. But this should be enough to get you started.

这篇关于动态字段添加在java / swing窗体中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆