存储来自多个jtable的值(storing values from multiple jtable)

编程入门 行业动态 更新时间:2024-10-22 19:29:13
存储来自多个jtable的值(storing values from multiple jtable)

我的应用程序需要在不同的jtable中显示不同的信息集,例如,如果应用程序具有Group 1结果,Group 2结果和Group 3结果,那么我需要在单独的选项卡中单独显示每个组,每个使用单独的jtable组。 当用户按下工具栏上的按钮时,我需要验证所有jtable并确保它们不为空。 我面临的问题是 - 由于创建的jtable数量是动态的(基于运行时的组数),有人可以建议我一种设计方法,帮助我维护创建的jtables列表并迭代jtable。 任何建议或参考示例都会有很大帮助!

示例代码: -

table.addMouseListener(new MouseAdapter() { public void mouseClicked(MouseEvent e){ // here i need to print the JTable Name } });

My application needs to display different sets of information in different jtable, for example, if the application has Group 1 result,Group 2 result and Group 3 result then, I need to display each group in a separate tab in separate using separate jtable for each group. When the user press a button on tool bar, i need to validate all the jtable and ensure they are not empty. The issue what i am facing is - since the number of jtable created will be dynamic(based on the number of groups at run time), can some one suggest me a design approach which help me to maintain the list of jtables created and iterate through the jtable. Any suggestions or reference example will be of great help!

Sample Code: -

table.addMouseListener(new MouseAdapter() { public void mouseClicked(MouseEvent e){ // here i need to print the JTable Name } });

最满意答案

您的问题非常广泛,可能过于宽泛而无法充分回答,但似乎您需要一些java.util.List<T> (通常实现为ArrayList<T> )。 这可能是List<JTable> ,或List<ClassThatHoldsATable>或者可能是List<ModelsForATable> 。 这样,您可以根据需要在列表中添加或删除项目,遍历列表,执行所需的任何验证,...等。

编辑:你已经编辑了你的问题,现在几乎说了同样的事情。 鉴于此编辑,我不确定你被困在哪里。


编辑 根据您更新的问题和代码:

table.addMouseListener(new MouseAdapter() { public void mouseClicked(MouseEvent e){ // here i need to print the JTable Name } });

问题:

我通常会避免使用mouseClicked,因为如果点击,轻微移动鼠标然后释放,它就很容易响应用户。 我通常使用mousePressed方法。 你是什​​么意思“JTable名字”? 如果你的意思是变量名,那么就不要去那条路,因为变量名并不是那么有用或有用。 如果你的意思是JTable对象引用,它比变量名更重要,那么你就得到了它:

例如,

table.addMouseListener(new MouseAdapater() { @Override // never forget this guy public void mousePressed(MouseEvent e) { // you've got your JTable reference right here! JTable selectedTable = (JTable) e.getSource(); // now what you do with it will depend on your needs and your code } });

如果您绝对需要将对象与String关联,请考虑使用Map<JTable, String>例如HashMap。

Your question is very broad, perhaps too broad to answer adequately, but it appears that you will need a java.util.List<T> of something (often implemented as an ArrayList<T>). That may be a List<JTable>, or a List<ClassThatHoldsATable> or perhaps List<ModelsForATable>. This way you can add or remove items from the list as needed, iterate through the list doing whatever validation is needed,... etc.

Edit: You've edited your question and now state pretty much the same thing. Given this edit, I'm not really sure where you're stuck.


Edit Based on your updated question and code:

table.addMouseListener(new MouseAdapter() { public void mouseClicked(MouseEvent e){ // here i need to print the JTable Name } });

Issues:

I usually avoid mouseClicked since it can be easy for it to not respond to the user if the click, move the mouse slightly and then release. I usually use the mousePressed method. What do you mean by "JTable Name"? If you mean the variable name, then don't go that route since variable names aren't all that helpful or useful. If you mean the JTable object reference, which is much more important than a variable name, well, then you've got it:

e.g.,

table.addMouseListener(new MouseAdapater() { @Override // never forget this guy public void mousePressed(MouseEvent e) { // you've got your JTable reference right here! JTable selectedTable = (JTable) e.getSource(); // now what you do with it will depend on your needs and your code } });

If you absolutely need to associate an object with a String, then consider using a Map<JTable, String> such as a HashMap.

更多推荐

本文发布于:2023-08-07 20:40:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1465983.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:多个   jtable   storing   values   multiple

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!