我正在开发一个跟踪大量任务的内部应用程序。 我想要一个简单的任务监视器来列出任务名称和任务状态。 我需要它看起来有点漂亮,我不是设计师,所以无论我做什么都会很糟糕,但基本的文本显示无法满足项目要求。
我本质上试图做的是显示类似于 Firefox 下载窗口、I-Tunes 下载窗口的内容,我还可以举出更多的名称,但它们看起来基本相同。 在每个应用程序中,每个“进度面板”都是可选择的。 因此,为了实现这一点,我认为只需使用一个 JPanel 列表(每个 JPanel 都带有一个 JProgressBar 和一个 JLabel)就很简单,每个 JPanel 都可以接受焦点来确定它和其他 JPanel 是否被选择。 我认为这将是一项简单的任务,但如果我使用 JList,它只会显示文本。 然后我想我应该在一个更大的面板中显示所有任务面板,但我无法让内部面板识别焦点。
这有什么模式吗? 是否有我尚未找到的滚动标准解决方案? 或者有更好的方法来做到这一点吗? 我不想重新发明轮子,但我认为这会很简单。
I’m working on an in-house app that tracks a bunch of tasks. I wanted to have a simple task monitor that would list the task name and the task’s status. I need this to look just a little nice, I’m no designer so whatever I do is going to suck, but a basic text display won’t work for the project requirements.
What I am essentially attempting to do is show something similar to the Firefox download window, the I-Tunes download window, and well I could name more but they all look basically the same. In each of these apps, each of the ‘progress panels’ is selectable. So to implement this I thought it would be simple to just use a list of JPanels each with a JProgressBar and a JLabel, each of which can just accept focus to determine if it and others are selected. I thought this was going to be an easy task, but if I use a JList it just displays text. I then just figured I would show all the task panels in a larger panel, but I cannot get the inner panels to recognize focus.
Is there a pattern for this? Is there a rolled standard solution that I just have not found? Or is there a better method for doing this? I don’t want to re-invent the wheel, but I thought this was just going to be simple.
发布评论
评论(3)
听起来您可能正在寻找的是
JList
。您可以通过首先将“任务”添加到
JList
对象的ListModel
(请参阅 创建模型(来自 Java 教程)部分,然后您需要分配一个自定义ListCellRenderer
它将接受您的“任务”并在JList
上呈现为列表本身中的JPanel
。 这里的关键是让您的自定义ListCellRenderer
能够按照您希望在JList
上显示的方式在JList
中显示您的“任务”代码>.查看编写自定义单元格渲染器 的 如何使用列表 页面中的部分Java 教程。 它将描述如何制作自定义
ListCellRenderer
以便您可以将“任务”表示为您想要的任何内容。为了简单起见,您将实现
ListCellRenderer
通过实现getListCellRendererComponent
它将返回一个Component
这是JList
。 您可能希望在此方法中构造或实例化您的JPanel
并将其作为Component
返回。It sounds like what you may be looking for is an
JList
.You can add your items to the
JList
's by first adding your "task" to theJList
object'sListModel
(see the Create a Model section from The Java Tutorials), and then you'll want to assigned a customListCellRenderer
which will accept your "task" and render on theJList
as aJPanel
in the list itself. The key here is to make your customListCellRenderer
be able to display your "task" in theJList
the way you want to have it show on theJList
.Take a look into the Writing a Custom Cell Renderer section from the How to Use Lists page of The Java Tutorials. It will describe how to make your custom
ListCellRenderer
so you can represent your "task" as anything you want.To keep it short, you will implement the
ListCellRenderer
interface by implementing thegetListCellRendererComponent
which will return aComponent
which is the representation of your task in theJList
. You'll probably want to either construct or instantiate yourJPanel
in this method and return it as theComponent
.执行此类操作的标准方法是使用 JTable(或 JList)作为容器。
您不必使用表格单元格的默认渲染器,但您可以为特定单元格指定您自己的渲染器。 看一下 CellRenderer
The standard way of doing this kind of things is to use JTable (or JList) as a container.
You don't have to use default renderes fot table cells, but you can specify your own renderer for specific cells. Take a look at CellRenderer
一个
JTable
(您可以将其设置为允许选择多行)如何,其内部的JPanel
占据每行中的单个单元格,其中包含一个JProgressBar
和一个JLabel
。 或者您可以使用具有与我刚才描述的相同结构的JList
。How about a
JTable
(which you can set to allow multiple rows to be selected) with an internalJPanel
occupying the single cell in each row, which contains aJProgressBar
and aJLabel
. Or you could use aJList
with the same structure as I just described.