在 JList 中使 JProgressBar 可点击
Making JProgressBar clickable in JList
我有一个 class,它扩展了 AbstractListModel 并实现了 PropertyChangeListener,其中包含一个 JProgressBar 列表。
启动 SwingWorker 后,此 class 将发布 JprogressBar 以显示进度。
class 在显示进度方面工作正常,但我想为用户修改以允许他们单击进度条并取消任务,我将 mouselistener 添加到 JprogressBar,但是 mouselistener 是没有被捕获。下面是我的 class 片段,有人可以告诉我哪里出了问题吗?
public final class JobManager extends AbstractListModel implements PropertyChangeListener {
private static final List<JProgressBar> progressBarList = new ArrayList<JProgressBar>();
private static final JobManager INSTANCE = new JobManager();
// Called by swing ui to add into Jpanel
public JScrollPane getJobListPane() {
final JList jobTable = new JList(INSTANCE);
jobTable.setName("Job List");
jobTable.setCellRenderer(new CustomListCellRenderer());
JScrollPane scrollPane = new JScrollPane(jobTable);
return new JScrollPane(tasksTable);
}
private void startJob(List<Job> jobList) {
JProgressBar progressBar = new JProgressBar();
progressBar.setMaximum(100);
progressBar.setStringPainted(true);
progressBar.setString(String.format("%s (%d%%)", caption, 0));
progressBar.setForeground(Colours.GREEN.brighter());
progressBar.setBackground(Color.GREEN);
final JPopupMenu popUpMenu = new JPopupMenu();
JMenuItem cancelMenuItem = new JMenuItem("Cancel");
cancelMenuItem.addActionListener(new ActionListener() {
@Override
public void actionPerformed(final ActionEvent e) {
System.out.println("Pressed cancellation option..............");
cancelTask();
}
});
popUpMenu.add(cancelMenuItem);
final JProgressBar progressBar1 = progressBar;
MouseListener mouseListener = new MouseAdapter() {
@Override
public void mouseClicked(final MouseEvent mouseEvent) {
popUpMenu.show(mouseEvent.getComponent(), progressBar1.getX(), progressBar1.getY());
}
};
progressBar.addMouseListener(mouseListener);
JobManager.progressBarList.add(progressBar);
fireIntervalAdded(this, JobManager.jobList.size() - 1, JobManager.jobList.size() - 1);
runnable.addPropertyChangeListener(this);
}
@Override
public Object getElementAt(final int row) {
if (row < JobManager.progressBarList.size()) {
return JobManager.progressBarList.get(row);
} else {
return null;
}
}
@Override
public int getSize() {
return someSize;
}
@Override
public void propertyChange(final PropertyChangeEvent evt) {
int index = jobList.indexOf(evt.getSource());
if (index >= 0) {
int progress = (Integer) evt.getNewValue();
JProgressBar progressBar = JobManager.progressBarList.get(index);
progressBar.setString(String.format("%s (%d%%)", "Job title ", progress));
progressBar.setValue(progress);
fireContentsChanged(this, index, index);
}
}
}
}
public class CustomListCellRenderer implements ListCellRenderer {
@Override
public Component getListCellRendererComponent(final JList list, final Object value, final int index,
final boolean isSelected, final boolean cellHasFocus) {
if (value instanceof Component) {
return (Component) value;
}
return new JProgressBar();
}
}
I added mouselistener to the JprogressBar, however the mouselistener are not captured.
JProgressBar 不是真正的组件。渲染器只是绘制组件的图片。
您需要在 JList
中添加 JPopupMenu
和 MouseListener
。 MouseListener 需要 select 被点击的灯光。类似于:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
public class ListRightClick extends JPanel implements ListSelectionListener
{
public ListRightClick()
{
setLayout( new BorderLayout() );
final JPopupMenu popup = new JPopupMenu();
popup.add( new JMenuItem("Do Something1") );
popup.add( new JMenuItem("Do Something2") );
popup.add( new JMenuItem("Do Something3") );
String[] data = { "zero", "one", "two", "three", "four", "five", "six", "seven", "eight" };
JList<String> list = new JList<String>( data );
list.setVisibleRowCount( 5 );
list.setComponentPopupMenu( popup );
list.addListSelectionListener( this );
list.addMouseListener( new MouseAdapter()
{
public void mousePressed(MouseEvent e)
{
if ( SwingUtilities.isRightMouseButton(e) )
{
JList list = (JList)e.getSource();
int row = list.locationToIndex(e.getPoint());
list.setSelectedIndex(row);
}
}
});
add( new JScrollPane(list) );
}
public void valueChanged(ListSelectionEvent e)
{
JList list = (JList)e.getSource();
System.out.println(list.getSelectedValue());
}
private static void createAndShowGUI()
{
JFrame frame = new JFrame("ListRightClick");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(new ListRightClick(), BorderLayout.NORTH);
frame.setLocationByPlatform( true );
frame.pack();
frame.setVisible( true );
}
public static void main(String[] args)
{
EventQueue.invokeLater(new Runnable()
{
public void run()
{
createAndShowGUI();
}
});
}
}
我有一个 class,它扩展了 AbstractListModel 并实现了 PropertyChangeListener,其中包含一个 JProgressBar 列表。 启动 SwingWorker 后,此 class 将发布 JprogressBar 以显示进度。
class 在显示进度方面工作正常,但我想为用户修改以允许他们单击进度条并取消任务,我将 mouselistener 添加到 JprogressBar,但是 mouselistener 是没有被捕获。下面是我的 class 片段,有人可以告诉我哪里出了问题吗?
public final class JobManager extends AbstractListModel implements PropertyChangeListener {
private static final List<JProgressBar> progressBarList = new ArrayList<JProgressBar>();
private static final JobManager INSTANCE = new JobManager();
// Called by swing ui to add into Jpanel
public JScrollPane getJobListPane() {
final JList jobTable = new JList(INSTANCE);
jobTable.setName("Job List");
jobTable.setCellRenderer(new CustomListCellRenderer());
JScrollPane scrollPane = new JScrollPane(jobTable);
return new JScrollPane(tasksTable);
}
private void startJob(List<Job> jobList) {
JProgressBar progressBar = new JProgressBar();
progressBar.setMaximum(100);
progressBar.setStringPainted(true);
progressBar.setString(String.format("%s (%d%%)", caption, 0));
progressBar.setForeground(Colours.GREEN.brighter());
progressBar.setBackground(Color.GREEN);
final JPopupMenu popUpMenu = new JPopupMenu();
JMenuItem cancelMenuItem = new JMenuItem("Cancel");
cancelMenuItem.addActionListener(new ActionListener() {
@Override
public void actionPerformed(final ActionEvent e) {
System.out.println("Pressed cancellation option..............");
cancelTask();
}
});
popUpMenu.add(cancelMenuItem);
final JProgressBar progressBar1 = progressBar;
MouseListener mouseListener = new MouseAdapter() {
@Override
public void mouseClicked(final MouseEvent mouseEvent) {
popUpMenu.show(mouseEvent.getComponent(), progressBar1.getX(), progressBar1.getY());
}
};
progressBar.addMouseListener(mouseListener);
JobManager.progressBarList.add(progressBar);
fireIntervalAdded(this, JobManager.jobList.size() - 1, JobManager.jobList.size() - 1);
runnable.addPropertyChangeListener(this);
}
@Override
public Object getElementAt(final int row) {
if (row < JobManager.progressBarList.size()) {
return JobManager.progressBarList.get(row);
} else {
return null;
}
}
@Override
public int getSize() {
return someSize;
}
@Override
public void propertyChange(final PropertyChangeEvent evt) {
int index = jobList.indexOf(evt.getSource());
if (index >= 0) {
int progress = (Integer) evt.getNewValue();
JProgressBar progressBar = JobManager.progressBarList.get(index);
progressBar.setString(String.format("%s (%d%%)", "Job title ", progress));
progressBar.setValue(progress);
fireContentsChanged(this, index, index);
}
}
}
}
public class CustomListCellRenderer implements ListCellRenderer {
@Override
public Component getListCellRendererComponent(final JList list, final Object value, final int index,
final boolean isSelected, final boolean cellHasFocus) {
if (value instanceof Component) {
return (Component) value;
}
return new JProgressBar();
}
}
I added mouselistener to the JprogressBar, however the mouselistener are not captured.
JProgressBar 不是真正的组件。渲染器只是绘制组件的图片。
您需要在 JList
中添加 JPopupMenu
和 MouseListener
。 MouseListener 需要 select 被点击的灯光。类似于:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
public class ListRightClick extends JPanel implements ListSelectionListener
{
public ListRightClick()
{
setLayout( new BorderLayout() );
final JPopupMenu popup = new JPopupMenu();
popup.add( new JMenuItem("Do Something1") );
popup.add( new JMenuItem("Do Something2") );
popup.add( new JMenuItem("Do Something3") );
String[] data = { "zero", "one", "two", "three", "four", "five", "six", "seven", "eight" };
JList<String> list = new JList<String>( data );
list.setVisibleRowCount( 5 );
list.setComponentPopupMenu( popup );
list.addListSelectionListener( this );
list.addMouseListener( new MouseAdapter()
{
public void mousePressed(MouseEvent e)
{
if ( SwingUtilities.isRightMouseButton(e) )
{
JList list = (JList)e.getSource();
int row = list.locationToIndex(e.getPoint());
list.setSelectedIndex(row);
}
}
});
add( new JScrollPane(list) );
}
public void valueChanged(ListSelectionEvent e)
{
JList list = (JList)e.getSource();
System.out.println(list.getSelectedValue());
}
private static void createAndShowGUI()
{
JFrame frame = new JFrame("ListRightClick");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(new ListRightClick(), BorderLayout.NORTH);
frame.setLocationByPlatform( true );
frame.pack();
frame.setVisible( true );
}
public static void main(String[] args)
{
EventQueue.invokeLater(new Runnable()
{
public void run()
{
createAndShowGUI();
}
});
}
}