I have a class which extends AbstractListModel and implements PropertyChangeListener, with a list of JProgressBar being hold within. Upon starting a SwingWorker this class will publish the JprogressBar to show the progress.
The class work fine in showing the progress, but I would like to modify for user to allow them click on the progress bar and cancel a task, I added mouselistener to the JprogressBar, however the mouselistener are not captured. Below is my class snippet, can someone advice where goes wrong please?
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();
}
}
答案 0 :(得分:0)
我将mouselistener添加到JprogressBar,但是没有捕获mouselistener。
JProgressBar不是真正的组件。渲染器只是描绘了组件的图片。
您需要向JPopupMenu
添加MouseListener
和JList
。 MouseListener需要选择单击的灯光。类似的东西:
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();
}
});
}
}