JTabbedPane doesn't work correctly - java

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.

Related

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

Java Swing: Add a component by code in NetBeans

I'm using NetBeans, and I've a JFrame where I added a JPanel to it using the NetBeans's palette.
I want to add a JRadioButton manually to that JPanel, so this is the code I tried in the constructor :
ButtonGroup group = new ButtonGroup();
JRadioButton btn1 = new JRadioButton("btn1 ");
JPanel1.add(btn1);
But when I run that JFrame I don't see that JRadioButton anywhere, but it works when I add it using the NetBens's palette.
How can I solve this problem ?
Make sure that the JPanel is not using GroupLayout. Most any other layout would work well, but likely for the moment, JPanel's default FlowLayout will work best.
Be sure to call revalidate() and repaint() on the JPanel after adding a component, if you are adding the component after the GUI has been rendered, such as on a button push.
If still having problems, show your code.
General advice: avoid using code generation utilities until after you understand the underpinnings of the GUI library, here Swing. You won't regret doing this.
The problem with NetBeans GUI Builder is that it initializes everything for you, where you can't alter the code unless you open the file on some other platform. In which case you have the risk of totally messing up the code.
What I can suggest is to maybe attempt something like this
Create an empty JPanel with a preferred size that you set in the property pane. You may also want to set the layout also, depending on your requirements.
After the initComponent() then add the JRadioButtons
public MyGUI(){
initComponents();
ButtonGroup group = new ButtonGroup();
JRadioButton btn1 = new JRadioButton("btn1 ");
jPanel1.add(btn1);
jpanel1.revalidate(); // as #Hovercraft Full Of Eels suggested
jPanel1.repaint();
}

Adding JButton dynamically to the JPanel not working with Netbeans

I created a JFrame Class with Netbeans 7.3 and added two panels from the palette.
I have added a button in the first panel on the click of which I want to add a new button in the second panel(topoPane).
Below is the button click event that I have written for the same. But, the button is not getting added to the panel even when the event is getting called.
Please tell me what's wrong in it.
private void jButton1MouseClicked(java.awt.event.MouseEvent evt)
{
// TODO add your handling code here:
System.out.println("Creating the Button");
JButton but = new JButton();
but.setBackground(Color.red);
but.setText("New Button");
but.setBounds(500, 500, 500, 500);
topoPane.add(but);
topoPane.revalidate();
}
From your use of setBounds, it is obvious that you are using a null layout. Because of this you need to call repaint() as containers with no layout do not automatically repaint added components on revalidate.
Apart from the fact that calling repaint is good practice, layout managers can remove the need to make this call along with manage the sizing and positioning of components. This makes it a good reason to use a layout manager.

not displaying all the buttons

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.

How to build UI Framework using Java

Please help me create my own custom layout, container, component, layout manager...
Example:
Containers and Layout Managers
Create a window frame.
Nest panels within a frame for better layout control.
Create and display buttons in a panel.
List two component attributes that are controlled by a layout manager.
Set the layout manager for a container.
Place components in a panel using BorderLayout, GridLayout, and FlowLayout.
Name one advantage of each of the layout managers.
Create panels with titles.
i was search on google but can't find any that match my requirement
Thanks for your help
Edit: I was found with keyword "Open Source UI"
Updated: 31, Oct 2016
I would like to updated some information to make it clearly for someone who concern. Back in 6 years ago what i want to know is how to build "UI Framework" from beginning.
If you have interesting like me i would like recommend Android UI Framework is good start because of open source and well document. Enjoy deep dive in to legacy code :) Good luck
Create a window frame
new JFrame();
Nest panels within a frame for better layout control
final JFrame jframe = new JFrame();
final JPanel innerOne = new JPanel();
jframe.add(innerOne);
innerOne.add(otherComponents);
Create and display buttons in a panel
innerOne.add(new JButton("Hello World!"));
List two component attributes that are controlled by a layout manager
Obviously check out JavaDoc of BorderLayout: BorderLayout.NORTH and SOUTH
Set the layout manager for a container
innerOne.setLayout(...);
Place components in a panel using BorderLayout, ...
Just apply the layout, and add providing the arguments for the LayoutManager:
innerOne.setLayout(new BorderLayout());
innerOne.add(..., BorderLayout.NORTH);
Name one advantage of each of the layoutmanager.
Check out the JavaDoc's. They are really helpful in these situations.
Create panels with titles.
innerOne.setBorder(new TitledBorder("Hello World"));
You could use the following set of tutorials: http://java.sun.com/docs/books/tutorial/uiswing/.

Categories

Resources