Java: Jprogressbar (Or Equivalent) in a Jtabbedpane Tab Title

Java: JProgressBar (or equivalent) in a JTabbedPane tab title

Enclose the JProgressbar in a JPanel and add that JPanel to the JTabbedPane.

Edit: From the JTabbedPane JavaDoc:

// In this case the custom component
is responsible for rendering the
title of the tab.

tabbedPane.addTab(null, myComponent); 
tabbedPane.setTabComponentAt(0, new JLabel("Tab"));

So you could basically simply replace new JLabel("Tab") by a reference to your JProgressbar (though this JProgressbar must not be added to the Tab itself).
However, I think this method doesn't exist prior to Java 1.6.

Java: JProgressBar (or equivalent) in a JTabbedPane tab title

Enclose the JProgressbar in a JPanel and add that JPanel to the JTabbedPane.

Edit: From the JTabbedPane JavaDoc:

// In this case the custom component
is responsible for rendering the
title of the tab.

tabbedPane.addTab(null, myComponent); 
tabbedPane.setTabComponentAt(0, new JLabel("Tab"));

So you could basically simply replace new JLabel("Tab") by a reference to your JProgressbar (though this JProgressbar must not be added to the Tab itself).
However, I think this method doesn't exist prior to Java 1.6.

JTabbedPane: show task progress in a tab

The Swing tutorial about progress bars (and showing progress in general) is a very good place to start. It shows you how to perform long-lasting operations on a worker thread by using a SwingWorker, and updating your UI at certain intervals to show progress of the long-lasting operation to the user. There is another tutorial available for more information on the SwingWorker and concurrency in Swing

And as always, this site is filled with examples. For example a previous answer of mine uses the SwingWorker class to show progress to a user

Edit

As I missed the title of tab part of your question. You could create a 'progress icon' and set that on the tab. The SwingWorker can then be used to update the icon.

An example of such an icon is example progress icon, which is basically an image you rotate each time some progress is made. The tabbed pane tutorial shows you how to add icons to your tabs (or even use custom components)

Edit2

As it seems my Mac in combination with JDK1.7 makes it much easier to show an animated gif then on other systems, I created a small SSCCE as well, quite similar to that of Andrew but with a rotating icon which does not look like it has been created by, and I quote, 'demented Chimpanzee'. The rotating icon code comes from this site (I used a stripped down version and added the timer). Only thing I am not too happy about is the fact I need to pass my tabbed pane to the rotating icon to trigger. Possible solution is to pull the timer outside the RotatingIcon class, but hey, it's only an SSCCE . Images are not included but were found with Google.

import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTabbedPane;
import javax.swing.Timer;
import java.awt.Component;
import java.awt.EventQueue;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Rectangle;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.geom.AffineTransform;

public class ProgressTabbedPane {

public static void main( String[] args ) {
EventQueue.invokeLater( new Runnable() {
@Override
public void run() {
JFrame frame = new JFrame( "RotatingIcon" );
JTabbedPane tabbedPane = new JTabbedPane( );
tabbedPane.addTab( "Searching", new RotatingIcon( new ImageIcon( "resources/images/progress-indeterminate.png" ), tabbedPane ),
new JLabel( new ImageIcon( "resources/images/rotatingIcon.gif" ) ) );
frame.getContentPane().add( tabbedPane );
frame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
frame.pack();
frame.setVisible( true );
}
} );
}

private static class RotatingIcon implements Icon{
private final Icon delegateIcon;
private double angleInDegrees = 90;
private final Timer rotatingTimer;
private RotatingIcon( Icon icon, final JComponent component ) {
delegateIcon = icon;
rotatingTimer = new Timer( 100, new ActionListener() {
@Override
public void actionPerformed( ActionEvent e ) {
angleInDegrees = angleInDegrees + 10;
if ( angleInDegrees == 360 ){
angleInDegrees = 0;
}
component.repaint();
}
} );
rotatingTimer.setRepeats( false );
rotatingTimer.start();
}

@Override
public void paintIcon( Component c, Graphics g, int x, int y ) {
rotatingTimer.stop();
Graphics2D g2 = (Graphics2D )g.create();
int cWidth = delegateIcon.getIconWidth() / 2;
int cHeight = delegateIcon.getIconHeight() / 2;
Rectangle r = new Rectangle(x, y, delegateIcon.getIconWidth(), delegateIcon.getIconHeight());
g2.setClip(r);
AffineTransform original = g2.getTransform();
AffineTransform at = new AffineTransform();
at.concatenate(original);
at.rotate(Math.toRadians( angleInDegrees ), x + cWidth, y + cHeight);
g2.setTransform(at);
delegateIcon.paintIcon(c, g2, x, y);
g2.setTransform(original);
rotatingTimer.start();
}

@Override
public int getIconWidth() {
return delegateIcon.getIconWidth();
}

@Override
public int getIconHeight() {
return delegateIcon.getIconHeight();
}
}
}

A screenshot for reference. A shame the icons do not rotate in the screenshot.
SSCCE screenshot

How to handle the height of the tab title in JTabbedPane

Just the sight of screenshot:

Sample Image

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class TabHeightTest {
public JComponent makeUI() {
JTabbedPane tabbedPane = new JTabbedPane(
JTabbedPane.TOP, JTabbedPane.SCROLL_TAB_LAYOUT);
tabbedPane.setUI(new javax.swing.plaf.basic.BasicTabbedPaneUI() {
@Override protected int calculateTabHeight(
int tabPlacement, int tabIndex, int fontHeight) {
return 32;
}
@Override protected void paintTab(
Graphics g, int tabPlacement, Rectangle[] rects, int tabIndex,
Rectangle iconRect, Rectangle textRect) {
if(tabIndex==0) {
rects[tabIndex].height = 20 + 1;
rects[tabIndex].y = 32 - rects[tabIndex].height + 1;
} else if(tabIndex==1) {
rects[tabIndex].height = 26 + 1;
rects[tabIndex].y = 32 - rects[tabIndex].height + 1;
}
super.paintTab(g, tabPlacement, rects, tabIndex, iconRect, textRect);
}
});
tabbedPane.addTab("000", new JLabel("Java: Jprogressbar (Or Equivalent) in a Jtabbedpane Tab Titleaaa"));
tabbedPane.addTab("111", new JScrollPane(new JTree()));
tabbedPane.addTab("222", new JSplitPane());

return tabbedPane;
}
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
@Override public void run() {
createAndShowGUI();
}
});
}
public static void createAndShowGUI() {
JFrame frame = new JFrame();
frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
frame.getContentPane().add(new TabHeightTest().makeUI());
frame.setSize(320, 240);
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
}

How to handle the height of the tab title in JTabbedPane

Just the sight of screenshot:

Sample Image

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class TabHeightTest {
public JComponent makeUI() {
JTabbedPane tabbedPane = new JTabbedPane(
JTabbedPane.TOP, JTabbedPane.SCROLL_TAB_LAYOUT);
tabbedPane.setUI(new javax.swing.plaf.basic.BasicTabbedPaneUI() {
@Override protected int calculateTabHeight(
int tabPlacement, int tabIndex, int fontHeight) {
return 32;
}
@Override protected void paintTab(
Graphics g, int tabPlacement, Rectangle[] rects, int tabIndex,
Rectangle iconRect, Rectangle textRect) {
if(tabIndex==0) {
rects[tabIndex].height = 20 + 1;
rects[tabIndex].y = 32 - rects[tabIndex].height + 1;
} else if(tabIndex==1) {
rects[tabIndex].height = 26 + 1;
rects[tabIndex].y = 32 - rects[tabIndex].height + 1;
}
super.paintTab(g, tabPlacement, rects, tabIndex, iconRect, textRect);
}
});
tabbedPane.addTab("000", new JLabel("Java: Jprogressbar (Or Equivalent) in a Jtabbedpane Tab Titleaaa"));
tabbedPane.addTab("111", new JScrollPane(new JTree()));
tabbedPane.addTab("222", new JSplitPane());

return tabbedPane;
}
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
@Override public void run() {
createAndShowGUI();
}
});
}
public static void createAndShowGUI() {
JFrame frame = new JFrame();
frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
frame.getContentPane().add(new TabHeightTest().makeUI());
frame.setSize(320, 240);
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
}

How to get coordinates of a tab header in the jTabbedPane?

You have to create own BasicTabbedPaneUI, because these methods are protected and there no way to override these methods from outside, (that came from Standard Java API)

Adding multiple JProgressBar to TableColumn of JTable

basically there are two ways move with JProgressBar by using SwingWorker and Runnable#Thread, example for SwingWorker

import java.awt.*;
import java.util.*;
import javax.swing.*;
import javax.swing.table.*;

public class TableCellProgressBar {

private String[] columnNames = {"String", "ProgressBar"};
private Object[][] data = {{"dummy", 100}};
private DefaultTableModel model = new DefaultTableModel(data, columnNames) {
private static final long serialVersionUID = 1L;

@Override
public Class<?> getColumnClass(int column) {
return getValueAt(0, column).getClass();
}

@Override
public boolean isCellEditable(int row, int col) {
return false;
}
};
private JTable table = new JTable(model);

public JComponent makeUI() {
TableColumn column = table.getColumnModel().getColumn(1);
column.setCellRenderer(new ProgressRenderer());
EventQueue.invokeLater(new Runnable() {

@Override
public void run() {
startTask("test");
startTask("error test");
startTask("test");
}
});
JPanel p = new JPanel(new BorderLayout());
p.add(new JScrollPane(table));
return p;
}
//http://java-swing-tips.blogspot.com/2008/03/jprogressbar-in-jtable-cell.html

private void startTask(String str) {
final int key = model.getRowCount();
SwingWorker<Integer, Integer> worker = new SwingWorker<Integer, Integer>() {

private int sleepDummy = new Random().nextInt(100) + 1;
private int lengthOfTask = 120;

@Override
protected Integer doInBackground() {
int current = 0;
while (current < lengthOfTask && !isCancelled()) {
if (!table.isDisplayable()) {
break;
}
if (key == 2 && current > 60) { //Error Test
cancel(true);
publish(-1);
return -1;
}
current++;
try {
Thread.sleep(sleepDummy);
} catch (InterruptedException ie) {
break;
}
publish(100 * current / lengthOfTask);
}
return sleepDummy * lengthOfTask;
}

@Override
protected void process(java.util.List<Integer> c) {
model.setValueAt(c.get(c.size() - 1), key, 1);
}

@Override
protected void done() {
String text;
int i = -1;
if (isCancelled()) {
text = "Cancelled";
} else {
try {
i = get();
text = (i >= 0) ? "Done" : "Disposed";
} catch (Exception ignore) {
ignore.printStackTrace();
text = ignore.getMessage();
}
}
System.out.println(key + ":" + text + "(" + i + "ms)");
}
};
model.addRow(new Object[]{str, 0});
worker.execute();
}

public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {

@Override
public void run() {
createAndShowGUI();
}
});
}

public static void createAndShowGUI() {
JFrame frame = new JFrame();
frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
frame.getContentPane().add(new TableCellProgressBar().makeUI());
frame.setSize(320, 240);
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
}

class ProgressRenderer extends DefaultTableCellRenderer {

private final JProgressBar b = new JProgressBar(0, 100);

public ProgressRenderer() {
super();
setOpaque(true);
b.setBorder(BorderFactory.createEmptyBorder(1, 1, 1, 1));
}

@Override
public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) {
Integer i = (Integer) value;
String text = "Completed";
if (i < 0) {
text = "Error";
} else if (i < 100) {
b.setValue(i);
return b;
}
super.getTableCellRendererComponent(table, text, isSelected, hasFocus, row, column);
return this;
}
}

How to make a JProgress bar with a gradient

You'll need a custom ProgressBarUI, perhaps derived from BasicProgressBarUI illustrated here. You may be able to use the existing paintDeterminate() implementation as a guide. A LinearGradientPaint applied to a BasicSliderUI is seen here.

Alternatively, consider the ProgressIcon shown here.



Related Topics



Leave a reply



Submit