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);
Related
I am almost certain this question was asked before here: Java Swing: How to change GUI dynamically , but I seem to just have some fundamental misunderstanding in how it works.
import javax.swing.*;
import javax.swing.event.*;
import java.awt.*;
import java.awt.event.*;
public class JTest extends JFrame
{
public static void main(String[] args)
{
JTest t = new JTest();
}
Container pane;
public JTest()
{
setSize(500,500);
setTitle("JTest");
setDefaultCloseOperation(EXIT_ON_CLOSE);
pane = getContentPane();
pane.setLayout(new GridLayout(1,2));
JButton old = new JButton("old");
old.addActionListener(new OldButton());
pane.add(old);
JScrollPane scroll = new JScrollPane(new JTextArea(50,20));
pane.add(scroll);
setVisible(true);
}
private class OldButton implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
pane.setLayout(new GridLayout(1,2));
JButton old = new JButton("new");
old.addActionListener(new NewButton());
pane.add(old);
JScrollPane scroll = new JScrollPane(new JTextArea(50,20));
pane.add(scroll);
pane.validate();
}
}
private class NewButton implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
pane.setLayout(new GridLayout(1,2));
JButton old = new JButton("old");
old.addActionListener(new OldButton());
pane.add(old);
JScrollPane scroll = new JScrollPane(new JTextArea(50,20));
pane.add(scroll);
pane.validate();
}
}
}
This code should replace the preexisting layout with a new one anytime the button in the corner is pressed, but instead, it just adds the new layout to the frame. Can someone tell me what I'm doing wrong?
EDIT:
Adding some information. A picture for reference:
I'm making a set of components like this inside the scroll pane. whenever I press the "Make new field" button, I want it to add a "field" (the name of the field followed by a textarea or some such) to the set in that scrollpane. This means changing the layout of the area inside the scrollpane to include the new field.
OK -- so it looks like what you want to do (and please correct me if I'm wrong) is to add a new component to a JPanel that is displayed within a JScrollPane. If so, then you do not want to change or swap layouts, and you certainly don't want to keep adding new JScrollPanes. Instead consider doing:
Create one JScrollPane and add to your GUI. Don't re-add this as you'll only need one.
add a JPanel to the JScrollPane's viewport that uses a layout that allows multiple components to be easily added to it. Perhaps a GridLayout or a BoxLayout, depending on what you need.
Also consider not adding the above JPanel directly to the viewport but rather adding it to another JPanel, one that uses BorderLayout, adding the first JPanel to the BorderLayout-using JPanel's BorderLayout.PAGE_START position, and then add this to the JScrollPane's viewport. This way the first JPanel won't stretch to fill the viewport initially.
Then in your button's ActionListener, add your components to the first JPanel by calling .add(...) on it, and then call revalidate() and repaint() on that first JPanel to layout the newly added components and repaint the JPanel and its contents.
Ok, so it turns out this wasn't a layout problem at all. I had failed to realize that setting a new layout doesn't cause the previous layout's components to disappear, you have to remove them before adding the new components. That's why I was getting duplication.
Thanks for pointing me in the right direction, though.
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();
}
}
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.
I am a beginner into Java and OOPS in general. Am studyin Head First Java to start, and studying GUI and Swing concepts in it.
The below code is just for understanding purposes.
On running the code, The frame window is displayed with Button, and when I expand it I can see Radio Button too.
Issues-
Button works till the window size is not more than the button size . As soon as I increase the window size even slightly more than button's dimensions, then the button is displayed only when cursor is on it.
I am changing window size using mouse.
Also even if I set Frame size to be more than button. say frame.setSize(800,800); then the button covers whole contentPane. and still behaves same way on resizing.
And the button responds to clicking on mouse, irrespective of where I click in the contentPane. It should respond only when i click directly on the button.
Please inform me why it is behaving this way.
And if possible,corrections in code or additions to correct this.
import java.awt.Color;
import javax.swing.*;
import java.awt.event.*;
public class Test1 implements ActionListener {
JFrame frame = new JFrame("Frame");
JButton button = new JButton("Button!");
JRadioButton radio = new JRadioButton("VideoKilledTheRadioStar!",true);
int j=0;
public static void main(String[] args) {
Test1 t = new Test1();
t.method1();
}
public void method1()
{
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
button.setSize(100,100);
button.setBackground(Color.ORANGE);
frame.add(button);
frame.setSize(100,100);
frame.setVisible(true);
button.addActionListener(this);
frame.getContentPane().add(radio);
radio.addActionListener(this);
}
public void actionPerformed(ActionEvent e)
{j++;
button.setText("clicked .. " + j);
if(button.getBackground()==Color.ORANGE)
button.setBackground(Color.BLUE);
else
button.setBackground(Color.ORANGE);
}
}
P.S I did not know which segment of code is important or more relevant to this question, so I have put complete code.
You are trying to add the JButton button and JRadioButton objects in the default layout(BorderLayout) of the JFrame.
Whenevery you add a component to JFrame having BorderLayout the components goes in the Middle Section and BorderLayout center section has tendency to occupy the complete space, so to position elements properly you will need to specify the location as well as set the PreferredSize of the component.
frame.add(radio, BorderLayout.SOUTH);
component.setPreferredSize(Dimension);
You are adding the JButton button and the JRadioButton both in the BorderLayout.CENTER location so only one is being displayed. Components at this location will be sized in the X and Y axis.
The JButton only displays when the cursor is over it due to the fact that it has its own MouseListener used for painting.
Also, the statements
frame.add(myComponent);
and
frame.getContentPane().add(myComponent);
both add the component to the frame's ContentPane & are equivalent but the first is chosen for convenience.
Note that components cannot co-exist in the same position in a BorderLayout. You could place the button at the BorderLayout.SOUTH position (& add directly to the frame):
frame.add(radio, BorderLayout.SOUTH);
BorderLayout disregards any preferred sizes for components so you would have to use a different layout manager such as BoxLayout to maintain a fixed size JButton.
See more about Layout Managers
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