I have a JToolBar with several Jbuttons on it which act as Icons in my program. I wanted to implement a button that, when pressed, increases the size of all my icons in the toolbar. For example:
Here I create my toolbar:
private JToolBar toolBar = new JToolBar();
Here I create some of my icons:
private JButton openButton = new JButton(am.getToolbarOpenFileAction());
private JButton closeButton = new JButton(am.getToolbarCloseFileAction());
private JButton undoButton = new JButton(am.getToolbarUndoAction());
private JButton redoButton = new JButton (am.getToolbarRedoAction());
private JButton cutButton = new JButton(am.getToolbarEditCutAction());
And then I have a method which creates the toolbar:
public void createToolbar() throws Exception {
toolBar.setFloatable(false);
toolBar.add(openButton);
toolBar.add(closeButton);
statusButton.setBorder(javax.swing.BorderFactory.createEmptyBorder(0, 0, 0, 0));
statusButton.setFocusable(false);
statusButton.setBorderPainted(false);
statusButton.setContentAreaFilled(false);
statusButton.setText("Status");
statusButton.setHorizontalTextPosition(javax.swing.SwingConstants.LEFT);
setCompileStatus(1);
}
However no matter what I do I can't seem to find a way to alter the JButton sizes in the toolbar. Does anyone have any suggestions for how I can implement a method to alter the button size? Would it be better to alter the size of the toolbar or the buttons itself?
Thanks in advance.
Try adding the buttons to a JPanel and then finally add the JPanel to the toolbar. For setting the size of the button,try the setPreferredSize() method.
Check the below and see:
openButton.setPreferredSize(new Dimension(100, 20));
panel.add(openButton); //add button to panel
toolBar.add(panel);//add panel to toolbar
add(toolBar);//add toolbar to frame
use setPreferredSize() method you can make any size it will work fine.
Related
enter code hereI have a UI with a toolbar to apply different filters on an image and the image is displayed on the right side (the toolbar is on the left side) I am adding new buttons but the toolbar is now really long and i want to give a limit so once it is reaching 1/5 of the total frame, I want to display the buttons on a new line.
public class Toolbar extends JPanel {
private JButton filterOne;
private JButton filterTwo;
private JButton filterThree;
private JButton loadImage;
private JLabel picture;
Toolbar(){
filterOne = new JButton("filterOne");
filterTwo = new JButton("filterTwo");
filterThree = new JButton("filterThree");
loadImage = new JButton("loadImage");
picture = new JLabel();
setLayout(new FlowLayout());
add(loadImage);
loadImage.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
ImageIcon pic = new ImageIcon("ImageOne.png");
picture.setIcon(pic);
}
});
add(filterOne);
add(filterTwo);
add(filterThree);
add(picture);
}
}
public class MainFrame extends JFrame {
private JTextArea textArea;
private Toolbar toolbar;
private ImagePannel imagePannel;
MainFrame() {
// Generating a new
setLayout(new BorderLayout());
// Generating a new toolbar that contains all the buttons to generate new images.
toolbar = new Toolbar();
imagePannel = new ImagePannel();
add(imagePannel,BorderLayout.CENTER);
// Position of the toolbar within the upper side of the pannel.
add(toolbar, BorderLayout.WEST);
//This is the name of the window
JFrame frame = new JFrame("My view");
//Initialize the size of the window
setSize(1200, 800);
//Makes the window close when we are crossing the window
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// Makes the window visible
setVisible(true);
}
}
...so once it is reaching 1/5 of the total frame, I want to display the buttons on a new line.
Don't use setLayout(new FlowLayout()); since this is not a "smart" layout, one you can control easily. Instead use a different layout, perhaps GridLayout(0, 5) -- one with a variable number of rows and with 5 columns.
I'm trying to get my buttons to fill the width of my panel, this is my code that
makes a buttons and a panel and then just adds the button to the panel.
buttonPane = makeButtonPanel();
button = makeButton("Hello");
button2 = makeButton("World");
buttonPane.add(button);
buttonPane.add(button2);
private JButton makeButton(String msg){
JButton button = new JButton(msg);
button.setAlignmentX(Component.CENTER_ALIGNMENT);
return button;
}
private JPanel makeButtonPanel(){
JPanel btnPanel = new JPanel();
btnPanel.setLayout(new BoxLayout(btnPanel, BoxLayout.Y_AXIS));
btnPanel.setBorder(BorderFactory.createLineBorder(Color.BLACK));
return btnPanel;
}
This is the output I am getting...
How the button fits it's container depends on the layout of the container. If you want it to stretch you could make the button panel have a BorderLayout and add the button to the north position, or use any of the other layout types whose components attempt to try to occupy as much room as they can (BorderLayout with north is just one example).
For multiple buttons stretched widthwise out as much as they can go, place a container in the North position instead of a single button, where that container contains multiple JButtons. That container should probably be a JPanel with a GridLayout, 1 column, as many rows as you have buttons.
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);
Good day
I have a basic toolbar to which I added ImageIcon buttons. The images are however different in size.
How would I go about in resizing the Icons so that they are all the same size.
super("ToolBar");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//creating the icons for the toolbar
ImageIcon savePic = new ImageIcon("c:/Exercises/unitTwo/Chapter Three/Images/save.png");
ImageIcon openFilePic = new ImageIcon("c:/Exercises/unitTwo/Chapter Three/Images/open.png");
ImageIcon printPic = new ImageIcon("c:/Exercises/unitTwo/Chapter Three/Images/print.png");
//creating buttons with initial text and icons. I.o.w. the buttons for the toolbar are created
JButton save = new JButton("Save", savePic);
JButton open = new JButton("Open", openFilePic);
JButton print = new JButton("Print", printPic);
JToolBar bar = new JToolBar();
bar.add(save);
bar.add(open);
bar.add(new JToolBar.Separator());
bar.add(print);
JTextArea text = new JTextArea(10, 40);
add(BorderLayout.NORTH, bar);
add(BorderLayout.CENTER, text);
pack();
setVisible(true);
The flamingo component suite supports resizable icons, one of the support classes being ImageWrapperResizableIcon. You might try to have a look at the source to get an idea of how to implement automatically resizing icons without the need to manually do so.
Alternatively, just create a resized version of the image yourself and create the ImageIcon using that resized version.
how can i add components dynamically in a jpanel?
I am having add button when i click the button the components should be added to the JPanel.
my question is that adding a textfield and button to jpanel when i click on the add button the user can click on the add button any number of times according to that i have to add them to the jpanel. i have added to scrollerpane to my jpanel,and jpanel layout manager is set to null.
Just as you always do, except that you have to call:
panel.revalidate();
when you are done, since the container is already realized.
Use an ActionListener, you can use an anonymous class like this:
JPanel myJPanel = new JPanel();
...
b = new Button("Add Component");
b.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
JLabel someLabel = new JLabel("Some new Label");
myJPanel.add(someLabel);
myJPanel.revalidate();
}
});