How to add button in JTabbedPane background? - java

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();
}
}

Related

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.

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);

How action a button to open a new pane in java for a GUI

I am creating a GUI in which my home page has a button labelled "Welcome to the Panel"
The point is that when you press on this button, it will navigate to a new page where I will have other functions. My only problem is that I dont know the syntax or how that when clicking a button, it will navigate to new page.
JButton btn = new JButton("Welcome to the Panel");
btn.setActionListener(new ActionListener()
{
#Override
public void actionPerformed(ActionEvent e)
{
// Here you open the other window. You can use JFrame, JOptionPane or JDialog
}
});
button.addActionListener(new ActionListner()
{
public void actionPerformed(ActionEvent ae)
{
//code to show pane
}
});
You need to register an ActionListener on your button and inside that action listener you make that panel (the page) visible.
How you do that depends on your layout, i.e. with a CardLayout you'd show the corresponding card (here's the doc). Using other layouts you might have to replace a component, e.g. if you use a BorderLayout and your content is placed in the center, replace the center component with the panel you want to show.
Note that if you're not familiar with layout managers yet, you should first have a look at those before doing dynamic changes to the ui (like navigation etc.).

Rich swing radiobutton

Developing a desktop application based on Java + Swing I faced the problem of creating a radio button which instead of text next to it, should have and image or, say, another widget like a spinner.
Clicking on the image or the spinner should select also the corresponding radioButton.
Is it possible? if so, how?
To me, the JRadioButton with icon given for constructor doesn't seem to work; it replaces the "native radio button icon" with given icon. I think to original asked wanted for radio button with icon in addition to the "radio button icon".
There has been some debate on the behaviour at Sun bug database with Bug #4177248 but no changes have been made.
Instead, one could try JRadioButtonMenuItem, even though there will probably be some non-wanted behaviour with that?
Short evaluation for both JRadioButton and JRadioButtonMenuItem:
public class IconRadioButtonEval {
public static void main(String[] args) throws Exception {
JFrame frame = new JFrame();
JPanel panel = new JPanel();
// Use some arbitrary working URL to an icon
URL url =
new URL(
"http://mikeo.co.uk/demo/sqlspatial/Images/RSS_Icon.png");
Icon icon = new ImageIcon(url);
JRadioButton button = new JRadioButton(icon);
panel.add(new JLabel("RadioButton with icon:"));
panel.add(button);
panel.add(new JLabel("RadioButtonMenuItem with icon:"));
panel.add(new JRadioButtonMenuItem(icon));
frame.getContentPane().add(panel);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.pack();
frame.setVisible(true);
}
}
Justin's suggestion for another component next to JRadioButton with empty string should probably work well in most cases.
Well, there is a constructor for JRadioButton that takes an Icon. You can use that to make it have an icon next to it.
JRadioButton(Icon icon)
-- Creates an initially unselected radio button with the specified image but no text.
Otherwise, it is fairly easy to make the JRadioButtons text empty, and place another component next to it. Then you will need to add a Listener to that component, so that the JRadioButton gets clicked if the other component gets clicked.

JTabbedPane: icon on left side of tabs

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);
}

Categories