not displaying all the buttons - java

I have an array with buttons:
JButton[] commandButtons = {
new JButton("Add Chair"),
new JButton("Add Table"),
new JButton("Add Desk"),
new JButton("Clear All"),
new JButton("Total Price"),
new JButton("Save"),
new JButton("Load"),
new JButton("Summary")
};
I want to put them all in the panel, but it is displaying only the last button.
so if anyone know how to fix it or have an advice it would be great.
Also I am not sure how to do the for loop as a for each.
for(int i=0; i<commandButtons.length; i++)
{
westPanel.add(commandButtons[i]);
commandButtons[i].addActionListener(this);
}

Set a FlowLayout manager on the westPanel JPanel and they will all show up.

I believe that you have not set layout, so the default one is BorderLayout. When you are adding elements to this layout without telling the parameter (where to put the element) it is added by default to the center. But there can be only one element in the center, so you see only the last button.
The fast fix is define flow layout:
pannel.setLayout(new FlowLayout());
Do it before adding the buttons. Now you will see all buttons. If you do not see enlarge the window.
Now, if the layout is not what you really want, read about layouts and decide which one (or combination of them) do you need.

Yeah it depends on the layout manager. If you want no layout manager you have to set the location and size yourself, otherwise they'll all be 0,0.
setLayout(null); //gives no layout manager
Always try to use a layout manager though.

Related

Refreshing JLabel - Java

I have some problems with JLabels and Frames..
I have the following code:
list.addListSelectionListener(
new ListSelectionListener() {
#Override
public void valueChanged(ListSelectionEvent e) {
//System.out.println(e.getFirstIndex());
String selectedFile = list.getSelectedValue().toString();
System.out.println("Selected file " + selectedFile);
JLabel label;
ImageIcon icon = new ImageIcon("C:\\Users\\danie\\Pictures\\" + selectedFile);
// frame.setSize(1047, 680);
label = new JLabel(icon);
//label.setSize(100,100);
frame.getContentPane().add(label, BorderLayout.CENTER);
frame.revalidate();
frame.repaint();
}
});
I want to refresh the label in the center of the borderlayout if a item in a JList is selected. But with this code, the old image is futher displayed and the new image is only drawed behind the existing image.
Could anyone help me? :)
First you are creating a new JLabel instance instead of working on the existing one. What you actually want to do is:
labe.setIcon(icon);
This will automatically refresh the element.
Suggestions:
Don't create a new JLabel, give it an ImageIcon and expect the existing JLabel to change. The two JLabels, the original one and the one created here, are two completely different objects, and changing the state of one (the icon it's displaying) will not magically change the state of the other.
Do make sure that the original JLabel has an instance variable in the class (not in your listener class), a field, refer to it, and then in your listener code, change the icon displayed in that JLabel by calling its setIcon(...) method
No need to call revalidate() or repaint() here as this should be done if you change components held within a container, such as if you removed the original JLabel from the JFrame's contentPane and swapped in a new one. Note that I do not recommend that you do this as it is over-complicating what should be a simple thing -- swapping icons.
To simplify things, I suggest that you read all your images in at program startup, create ImageIcons at that time, and then put them into an array or collection (such as an ArrayList<Icon> or a HashMap<String, Icon>), and then in your listener code, extract the correct icon, and put it into the existing JLabel.
You are not using the same JLabel it seems. You should alter the label you already have, not create a new one.

JDialog layout issues

I'm using a JDialog to create a customized dialog box for my Java project. I'm having issues with the layout at the moment. It seems each JLabel I add to the dialog goes over the existing one. Do I need to add some sort of JPanel?
I also seem to have a issue with the size. I set it too 500x500 but why does it only goes as large as the text width?
JDialog processData = new JDialog(f1, "TItle goes here");
JLabel centretext = new JLabel("Look at me im centre!");
JLabel leftext = new JLabel("LOok at me im left");
JLabel righttext = new JLabel("LOok at me im right");
processData.setVisible(true);
processData.add(centretext);
processData.add(lefttext);
processData.add(rightext);
processData.toFront();
processData.setSize(500,500);
processData.setLocation(500,500);
processData.pack();
JDialog uses a BorderLayout by default, which means, it will only show a single component in any of the five available positions, all the others get ignored.
Consider using a different layout manager. See Laying Out Components Within a Container for more details

JTabbedPane doesn't work correctly

public void tabbedPane(){
JPanel tab1 = new JPanel();
JButton btn = new JButton("Buton - 1");
btn.setPreferredSize(new Dimension(50, 20));
btn.setLocation(0, 10);
tab1.add(btn);
JTabbedPane tabPanel = new JTabbedPane();
tabPanel.addTab("tab1", null, tab1);
tabPanel.addTab("tab2", tab1);
tabPanel.addTab("tab3", btn);
tabPanel.setPreferredSize(new Dimension(450, 150));
tabPanel.setLocation(50, 0);
mainPanel.add(tabPanel);//Main panel on frame
}
When I run my application, I see only tab2 and tab3 pane, and I have many issues:
tabPanel.setLocation doesn't work
tabPanel.addTab("tab1" ...) doesn't work
btn.setPreferredSize(new Dimension(50, 20)); when I
click "tab2" it works correctly, however when I click "tab3" it doesn't change button
size.. why?
and i use null layout
tabPanel.setLocation doesn't work
Don't use setLocation(...) but instead use nested components and layout managers to achieve a pleasing and easy to maintain GUI layout.
tabPanel.addTab("tab1" ...) doesn't work
With Swing, you can only add a component to one container, that's it. The component will only show up in the last container that it was added to.
btn.setPreferredSize(new Dimension(50, 20)); when I click "tab2" it works correctly, however when I click "tab3" it doesn't change button size.. why?
Again, you will want to study the layout managers
and i use null layout
You almost never want to do this as this will make your application not look correct on any platform but your own and will make it very very difficult to maintain and upgrade. It is much better to use the layout managers and let them do the heavy lifting of laying out and sizing components for you.
What is your objective with this?
A JTabbedPane is used to organize views, I see you're trying to add a JPanel as a first tab, this is the 'main goal' of the JTabbedPanes.
tabPanel.addTab("Tab 1", tab1);
Try to add the tab like this, you're passing a 'null' value as the icon, which must not affect at all, but if you're not using an icon, then just add the panel as a tab with the intended name.
On second adding, you're adding again the same component (tab1).
On third adding, you're trying to add a component already on a container (tab1). This will make this component to appear only in the last container you add it to. Besides, component is a JButton. I cannot see the goal of a JButton as a tab.
For the setLocation(x, y) issue, check the layout you're using on the container.
Again, I think the main issue here is that you're not correctly approaching your problem, or you're not using the required tools.

Adding to JPanel in application

When a particular button is clicked I want another set of buttons to be added to the Panel, however at the moment when I do this, I can add them as many times as I want, I need this to be only possible once. Would the best way to do this be set the adding of the buttons and fields in a while loop?
if(e.getSource() == selectScript){
while(scriptB < 1 ){
imageID = new JTextField("INT");
imageDescription = new JTextField("imgDescription");
imagePath = new JTextField("imagePath");
manageImageTab.add(imageID);
manageImageTab.add(imageDescription);
manageImageTab.add(imagePath);
insertImage = new JButton("Add an Image");
insertImage.addActionListener(new dbaccess());
manageImageTab.add(insertImage);
manageImageTab.revalidate();
validate();
scriptB++;
}
}
Perhaps rather than add and remove the JButtons, you could add the buttons once at the code start, just don't make them visible until you need them, or perhaps better place them all on a JPanel that is not visible and then made visible when desired. Just don't forget to call revalidate() and repaint() on the container that holds the buttons and their panel.
If I understand you correctly, I would use a flag alreadyAdded that starts out false, gets set to true after the controls have been added, then don't allow it to add after that.

Dynamically remove Component from JPanel

I am adding and deleting components dynamically in a JPanel.
Adding and deleting functionality works fine but when I delete the component it deletes the last component rather than the component to be deleted.
How can I solve this issue?
Interestingly enough I am coming across the same issue and I am surprised people are upvoting the other answer, as he is clearly asking about dynamically created Components, not components already created under a variable name which is obtainable, instead of anonymously created objects.
The answer is pretty simple. Use
getComponents() to iterate through an array of components added to the JPanel. Find the kind of component you want to remove, using instanceof for example. In my example, I remove any JCheckBoxes added to my JPanel.
Make sure to revalidate and repaint your panel, otherwise changes will not appear
Component is from java.awt.Component.
//Get the components in the panel
Component[] componentList = panelName.getComponents();
//Loop through the components
for(Component c : componentList){
//Find the components you want to remove
if(c instanceof JCheckBox){
//Remove it
clientPanel.remove(c);
}
}
//IMPORTANT
panelName.revalidate();
panelName.repaint();
Using the method Container.remove(Component), you can remove any component from the container. For example:
JPanel j = new JPanel();
JButton btn1 = new JButton();
JButton btn2 = new JButton();
j.add(btn1);
j.add(btn2);
j.remove(btn1);

Categories

Resources