JTabbedPane: icon on left side of tabs - java

hello i am using the nimbus look-and-feel and have a tabbedpane with an icon and text.
now the icon appears on the right side of the text, while i would like to have it on the left side.
also i would like to add some spacing between the icon and the text.
thanks!

You need to set the tab component yourself; which governs how the tab title is rendered.
// Create tabbed pane and add tabs.
JTabbedPane tabbedPane = ...
// Create bespoke component for rendering the tab.
JLabel lbl = new JLabel("Hello, World");
Icon icon = new ImageIcon(getClass().getResource("/foo/bar/hello.jpg"));
lbl.setIcon(icon);
// Add some spacing between text and icon, and position text to the RHS.
lbl.setIconTextGap(5);
lbl.setHorizontalTextPosition(SwingConstants.RIGHT);
// Assign bespoke tab component for first tab.
tabbedPane.setTabComponentAt(0, lbl);
Obviously you could encapsulate this in a utility method:
private void addTab(JTabbedPane tabbedPane, Component tab, String title, Icon icon) {
tabbedPane.add(tab);
JLabel lbl = ... // Create bespoke label for rendering tab title.
tabbedPane.setTabComponentAt(tabbedPane.getTabCount() - 1, lbl);
}

Related

How to add button in JTabbedPane background?

I want to add button in my JTabbedPane background like Google Chrome so that every time I can add new tabs by clicking it.
How can I do it? Thanks in advance!
EDIT: I have taken an undecorated JFrame.
Look into the JTabbedPane.setTabComponentAt( int index, Component component ) method. This method allows you to set the component with which to render the title.
Description from documentation:
Sets the component that is responsible for rendering the title for the specified tab. A null value means JTabbedPane will render the title and/or icon for the specified tab. A non-null value means the component will render the title and JTabbedPane will not render the title and/or icon.
Note: The component must not be one that the developer has already added to the tabbed pane.
What you can do:
Create the JTabbedPane
Add a new tab to it, its intended function like the chrome "add tab page"
Set the title component of that tab to a button (style it appropriately)
When that button is clicked, add a new tab right before the button tab and show the newly added tab
This code will create only one tab and button to it.
class Test extends JFrame
{
JTabbedPane jtab;
JButton but;
JPanel panel;
Test()
{
super("JTabbedPane");
jtab=new JTabbedPane();
but=new Button("Click");
panel=new JPanel();
panel.add(but);
jtab.add("Tab",panel);
add(jtab);
setVisible(true);
setSize(400,400);
}
public static void main(String[] args)
{
new Test();
}
}

How to extend JTabbedPane to always have an "Add Tab" tab?

I am trying to create a JTabbedPane so that the final "tab" is actually a button that will open up a dialog box to add something to the tabbed pane. I tried looking around the source for JTabbedPane, but I can't find where the tab objects (the row of clickable "buttons" that when clicked change the currently visible component) actually are. There is a private list of Page objects (so they can't be accessed by child classes anyway) but they only contain the info about the tab and aren't the tab object themselves. My goal is to be able to add a button that comes after the tab list (horizontally).
I also tried using JTabbedPane.setTabComponentAt() to change the string of the final tab to a JButton with a component of null, but that still adds the tab component. If you click slightly to the right/left of the button in the tab, a blank tab will show because there's always padding around the component. Perhaps there's a way to get rid of this? But I suppose in some Look and Feels that have tabs like this: /---\ instead of this: |---| you could still click in the tab but not click the button.
Does anyone know how I can get what I'm looking for without writing my own version of JTabbedPane?
Thanks!
My goal is to be able to add a button that comes after the tab list (horizontally).
The proper solution is to write a custom UI, but that can be complicated and I'm not sure what code to change.
As a simple hack you can us a panel with an OverlayLayout to display the button at the top/right of the tabbed pane:
import java.awt.*;
import javax.swing.*;
import javax.swing.plaf.*;
public class TabbedPaneWithComponent
{
private static void createAndShowUI()
{
JPanel panel = new JPanel();
panel.setLayout( new OverlayLayout(panel) );
JTabbedPane tabbedPane = new JTabbedPane();
tabbedPane.add("1", new JTextField("one"));
tabbedPane.add("2", new JTextField("two"));
tabbedPane.setAlignmentX(1.0f);
tabbedPane.setAlignmentY(0.0f);
JCheckBox checkBox = new JCheckBox("Check Me");
checkBox.setOpaque( false );
checkBox.setAlignmentX(1.0f);
checkBox.setAlignmentY(0.0f);
panel.add( checkBox );
panel.add(tabbedPane);
JFrame frame = new JFrame("TabbedPane With Component");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add( panel );
frame.setLocationByPlatform( true );
frame.setSize(400, 100);
frame.setVisible( true );
}
public static void main(String[] args)
{
EventQueue.invokeLater(new Runnable()
{
public void run()
{
createAndShowUI();
}
});
}
}
Works pretty good except when the width of the tabbed pane becomes too small and the button overlaps the tabs.

How to make a tab header with only an icon and translucent components using Swing?

I want to make a tabbed pane in my Swing GUI such that the tab headers appear as icons (i.e. I want to place a picture on the tabbed pane headers, like we can place pictures on buttons)
A GUI with tabbed pane headers appearing as icons (only pictures on those headers are visible, rest of the entire component is completely transparent), and when the user clicks on a picture/icon/tabbed pane header, the entire tabbed pane becomes translucent, and displays its contents (like text). The pictures attached show it better. Please note that there are no visible borders of the tabbed pane.
Is it possible? Any tips or suggestions are welcome?
Moreover, are such transparent and translucent swing components possible through the drag and drop facility of the NetBeans IDE?
When the user clicks:
I will be thankful for any ideas on how to do this.
For the tabs use addTab(null, icon, component) of JTabbedPane with icon the image you want and component a translucent JLabel. You can make one by calling its setOpaque(true) and setBackground(new Color(255, 255, 255, 100)).
Edit
Try to run this and see if it's getting closer to what you want.
public class TransparentTabs extends JFrame {
private final static Color TRANSPARENT = new Color(0, 0, 0, 0);
TransparentTabs() {
JTabbedPane tabs = new JTabbedPane();
JLabel label = new JLabel("Text Here");
label.setOpaque(true);
label.setBackground(TRANSPARENT);
tabs.addTab(null, new ImageIcon("path to image"), label); // Change path
tabs.addTab("Tab to the left has only an image", new JLabel("Something"));
tabs.setSelectedIndex(-1);
getContentPane().setBackground(Color.RED); // Placeholder for background image
getContentPane().add(tabs);
setLocationRelativeTo(null);
setDefaultCloseOperation(EXIT_ON_CLOSE);
pack();
setVisible(true);
}
public static void main(String[] args) {
UIManager.put("TabbedPane.contentAreaColor", TRANSPARENT);
UIManager.put("TabbedPane.selected", TRANSPARENT);
UIManager.put("TabbedPane.background", TRANSPARENT);
UIManager.put("TabbedPane.borderHightlightColor", TRANSPARENT);
UIManager.put("TabbedPane.darkShadow", TRANSPARENT);
UIManager.put("TabbedPane.focus", TRANSPARENT);
new TransparentTabs();
}
}

Creating a composite Swing component for JToolbar

When using setRollover(true), buttons on Swing toolbars are flat without border and the border is drawn only when hovering/pushing the button. However, if the buttons are first added to a panel, and then the panel is added to the toolbar, this does not work. Is there some easy way how to achieve it?
I want the buttons to be in a JPanel to make them act as a single component (imagine a paging component with first/prev/next/last page buttons). I also want it to work regardless of L&F (as it would if the JPanel was not between the toolbar and the buttons).
EDIT:
Compare the buttons One & Two (added directly) with buttons Three & Four (added via a JPanel) in the following example:
import javax.swing.*;
public class ToolbarTest extends JFrame {
ToolbarTest() {
JToolBar toolbar = new JToolBar();
toolbar.setRollover(true);
JButton button = new JButton("One");
button.setFocusable(false);
toolbar.add(button);
button = new JButton("Two");
button.setFocusable(false);
toolbar.add(button);
JPanel panel = new JPanel();
button = new JButton("Three");
button.setFocusable(false);
panel.add(button);
button = new JButton("Four");
button.setFocusable(false);
panel.add(button);
toolbar.add(panel);
add(toolbar);
pack();
}
public static void main(String[] args) throws Throwable {
// optional: set look and feel (some lf might ignore the rollover property)
for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
if ("Nimbus".equals(info.getName())) { // or "Windows", "Motif"
javax.swing.UIManager.setLookAndFeel(info.getClassName());
break;
}
}
ToolbarTest frame = new ToolbarTest();
frame.setVisible(true);
}
}
Here are the screenshots:
The toolbar on Nimbus LF:
The same toolbar when mouse hovers over the second button (the mouse cursor is not shown):
The same toolbar on Windows LF:
I would like the Three and Four buttons to work the same way as the One and Two buttons.
1) I'd suggesting to set JMenuBar as container rather than JToolbar,
disadvantages:
isn't moveable and detachable, nor out of Container
could by placed everywhere but only inside Container, like as another JComponent by using LayoutManager
2) for JToolBar would be better to place there one JPanel nested another JComponents, as shows from your code example
3) in your code example you define one JButton fouth times, in Java is required define as separate Objects
Using another JToolbar instead of JPanel works.
But: then I would probably (or maybe not?) have a problem, if I wanted to include the composite component into a dialog or something else than a toolbar. (This would be similar to having two types of buttons, one for toolbars and one for the rest)
this is the portion of the code from the question adding a panel changed to add a toolbar.
JToolBar component = new JToolBar();
component.setRollover(true);
component.setBorder(null);
component.setFloatable(false);
button = new JButton("Three");
button.setFocusable(false);
component.add(button);
button = new JButton("Four");
button.setFocusable(false);
component.add(button);
toolbar.add(component);

Java - How to set the height and width of the component using box layout

I am creating the applet using the BoxLayout. In this layout i have 3 components(i.e, 2 text areas and one button). I want to set the height and width of the button.Please can anybody help me.
code
public class parsetextdata extends Applet
{
TextArea ta1,ta2;
Button parse;
public void init()
{
this.setLayout(new BoxLayout(this, BoxLayout.Y_AXIS));
ta1 = new TextArea();
add(ta1);
parse = new Button();
parse.setLabel("parse");
parse.setBackground(Color.DARK_GRAY);
parse.setForeground(Color.WHITE);
add(parse);
ta2 = new TextArea();
ta2.setEditable(false);
ta2.setBackground(Color.GRAY);
ta2.setForeground(Color.WHITE);
add(ta2);
}
}
Do not add the JButton directly. Instead, add it to a JPanel, and then add that JPanel to the applet's content pane. The reason for this is the layout manager of the applet's content pane is causing the components to take up as much space as possible. By adding the button to the panel first, and then adding the panel to the applet's content pane, the panel will be resized and the button will keep it's preferred size.
EDIT -
I just noticed that you're using AWT components. Therefore, here are the component translations:
JButton = Button
JPanel = Panel

Categories