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.
Related
I am trying to make a UI to view recipes from a cookbook stored on the computer. Part of this tab is a JScrollPanel storing a JTextArea that displays the available recipes. All called functions work as intended (e.g. allRecipes() returns a string of the available recipes properly); however, the scroll pane itself does not appear. It is added to the frame, as I can see by a small grey block where the pane would be, but it is not filled as it should be. The code is as follows:
//First panel, buttons to limit displayed recipes
JPanel pane1 = new JPanel();
JButton all = new JButton("All");
JButton makeable = new JButton("Makeable");
JTextField search = new JTextField("", 10);
JButton searchButton = new JButton("Search Ingredient");
//Second panel, display of recipes
JPanel pane2 = new JPanel();
JTextArea recipes = new JTextArea(allRecipes());
JLabel list = new JLabel("List of Recipes:");
JScrollPane scroll = new JScrollPane(recipes);
//Third panel, options to add recipe and view specific recipe
JPanel pane3 = new JPanel();
JButton add = new JButton("Add Recipe");
JTextField view = new JTextField("", 10);
JButton viewButton = new JButton("View Recipe");
//Central method
public Recipes() {
//basic UI stuff
super("Recipes");
setSize(475,350);
setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
FlowLayout flo = new FlowLayout();
setLayout(flo);
//add pane 1
pane1.add(all);
pane1.add(makeable);
pane1.add(search);
pane1.add(searchButton);
pane1.setBorder(BorderFactory.createEmptyBorder(10,10,10,10));
add(pane1);
//add pane 2
pane2.add(list);
scroll.setPreferredSize(new Dimension(10,15));
scroll.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED);
scroll.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
pane2.add(scroll, BorderLayout.CENTER);
pane2.setBorder(BorderFactory.createEmptyBorder(10,10,10,10));
add(pane2);
//add pane 3
pane3.add(add);
pane3.add(view);
pane3.add(viewButton);
add(pane3);
//start up the UI
setVisible(true);
}
JTextArea recipes = new JTextArea(allRecipes());
We don't know what allRecipes() does, but I would guess it sets the text of the text area.
Instead you should define your text area with the rows/columns you wish. Something like:
JTextArea recipes = new JTextArea(5, 30);
then in the constructor you would add the text:
recipes.setText( allRecipes() );
You should NOT be trying to set the preferred size of the scroll pane. The preferred size will automatically be determined from the preferred size of the text area which is calculated based on the rows/columns provided in the constructor.
//scroll.setPreferredSize(new Dimension(10,15));
Also, the preferred size of a component is specified in pixels, to the above makes no sense.
pane2.add(scroll, BorderLayout.CENTER);
The default layout manager for a JPanel is the FlowLayout. So you can't just use a BorderLayout constraint when adding the component.
I am creating a profile page. At the left, I have added a jlabel containing a background image to my jpanel. Now over this panel, I want to add another jlabel for the icon. However, it is not showing up. Please help me.
P.s: This is only part of the code
public CustomerProfile()
{
super("Customer Profile");
setLayout(new BorderLayout());
//PROFILE DETAILS JPANEL
jpProfile = new JPanel();
add(jpProfile, BorderLayout.WEST);
//BACKGROUND IMAGE OF THE PROFILE JPANEL
ImageIcon background_img = new ImageIcon("background.jpg");
Image img = background_img.getImage();
Image tempImg = img.getScaledInstance(350, 600, Image.SCALE_SMOOTH);
background_img = new ImageIcon(tempImg);
background = new JLabel("",background_img,JLabel.CENTER);
jpProfile.add(background);
//PROFILE ICON
ImageIcon profImg = new ImageIcon("female.png");
jlProfileIcon = new JLabel();
jlProfileIcon.setIcon(profImg);
//ADDING PROFILE ICON TO JPANEL
jpProfileIcon = new JPanel();
jpProfileIcon.add(jlProfileIcon);
//jpProfile.setOpaque(false);
//jpProfileIcon.setOpaque(false);
jpProfileIcon.add(jlProfileIcon);
background.add(jpProfileIcon);
}
By default only a JPanel uses a layout manager.
The JLabel does not use a layout manager so the size/location of any component added to the label is not changed. The default size of a component is (0, 0) so there is nothing to paint.
Try:
background = new JLabel("",background_img,JLabel.CENTER);
background.setLayout( new BorderLayout() ); // added
…
background.add(jlProfileIcon);
The following code is not needed:
//jpProfileIcon = new JPanel();
//jpProfileIcon.add(jlProfileIcon);
That is you don't need to create a JPanel, just to add the label to it.
A better option is to paint the background image onto a panel and then you can just add your label normally to the panel. See: Background Panel for a class that implements this functionality.
I want to create new tabs in tabbedpane when new option is clicked. But new tab should include the panel with a text area on same position as in the first tab which I created through drag and drop in netbeans. I create one tab and want new instance of that tab as other tabs how can I do that?
I am creating a notepad application and I want to add the functionality of new files in the form of tabs in tabbedpane. I have created one tab through drag and drop in netbeans. but I don't know how to use this instance in new tab when new option is clicked.
//This is the code to create new panel but it is not working
int i= 1;
private void jMenuItem1ActionPerformed(java.awt.event.ActionEvent evt)
{
JPanel jp = new JPanel();
jp = jPanel1; // jPanel1 is the panel created by drag and drop.
// i cantains text area.
jTabbedPane1.addTab("untitled"+i,jp);
i++;
}
I want the application to create new tabs as tabs created in netbeans or dev etc.
jp = jPanel1; // jPanel1 is the panel created by drag and drop.
You can't share components. Swing components can only have a single parent.
So you need to create a new instance of JPanel and a new Instance of JTextArea.
JTextArea textArea = new JTextArea(5, 20);
JPanel panel = new JPanel( new BorderLaout() );
panel.add( textArea );
jTabbedPane1.addTab("untitled"+i, panel);
Now the text area will fill the space available in the tabbed pane.
You don't event need the JPanel if all you want is a text area on the tab. Just add the new text area to the tabbed pane.
JPanel jp1 = new JPanel();
JTextArea ta1 = new JTextArea();
ta1.setBounds(10, 10,100 , 100);
jp1.add(ta1);
javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jp1);
jp1.setLayout(jPanel1Layout);
jPanel1Layout.setHorizontalGroup( jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING).addComponent(ta1, javax.swing.GroupLayout.DEFAULT_SIZE, 501, Short.MAX_VALUE));
jPanel1Layout.setVerticalGroup( jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING).addComponent(ta1, javax.swing.GroupLayout.DEFAULT_SIZE, 321, Short.MAX_VALUE));
jTabbedPane1.addTab("untitled",jp1);
//this is the right answer to my question this is the way to pass a text area with a panel in the tabbed pane
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.
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