• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

JTabbedPane pblm.

 
Ranch Hand
Posts: 64
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
I am designing a swing application. I am to use JTabbedPanes. In one JFrame screen, I want two JTabbed Panes
(1).Add New User
(2).Change New User.
(1) contains a Label "ADD".
(2) contains a Label "MODIFY"
What I require:
1. When I click "Add New User" tab, "ADD". should be displayed hiding "MODIFY"
2. When I click " Change New User" tab, "MODIFY". should be displayed hiding "ADD".
Problems faced:
----------------
1. When first time I run the pgm,I click the ADD User Tab, nothing is displayed.
Then I click the MODIFY Tab, "MODIFY" is displayed and after that when I click ADD Tab,the same label "modify" is being displayed and then "modify" is always displaced irrespective of the tabbed panes clicked.
What could be the cause of this?.
2. I changed the setLayout(null) so that I could position components according to my desired locations. The pgm is running but only the blank JFrame is displayed. Will JTabbedPannes not work in setLayout(null)?.
3. There are only 2 JTabbedpanes. They occupy only a portion of the top left hand side of the screen. Is there any method through which I can place the 2 panes equidistant from each other?
The Program:
------------
import javax.swing.*;
import javax.swing.event.*;
import java.awt.*;
import java.awt.event.*;
//public class ModifyProposal extends JInternalFrame
public class Tabbedpane extends JFrame
{
Container container;
JTabbedPane tp;
JLabel laddUser = new JLabel();
JLabel lmodifyUser = new JLabel();
public Tabbedpane()
{
container=this.getContentPane();
//container.setLayout(null);
tp = new JTabbedPane();
tp.addTab("Add User",laddUser);
// When I click this tab, add User Details
should be displayed
tp.addTab("Modify User",lmodifyUser);
// When I click this tab, Modify User Details
should be displayed
tp.addChangeListener(new TPListener());
container.add(tp);
setSize(805,550);
setVisible(true);
}
class TPListener implements ChangeListener
{
JTabbedPane tempTP;
public void stateChanged(ChangeEvent ce)
{
int idx=0;
tempTP = (JTabbedPane) ce.getSource();
JLabel myLabel1 = new JLabel("Add");
JLabel myLabel2 = new JLabel("Modify");
container.add(myLabel1);
container.add(myLabel2);
idx = tempTP.getSelectedIndex();
if (idx==0)
{
myLabel2.setVisible(false);
myLabel1.setVisible(true);
}
else if (idx==1)
{
myLabel1.setVisible(false);
myLabel2.setVisible(true);
}
else
{
// do nothing
}
}
}
public static void main(String[] a)
{
Tabbedpane ob = new Tabbedpane();
}
} // end of pgm.
with warm regards,
Arun.
 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Upon analysis of your code I've noticed the following things:
The reason your labels are not showing up properly is because you are adding labels directly to the content pane (in your ChangeListener). Since you are not providing a layout model, they are being layed on top of each other, thus the last one added is the one that shows. This also explains why you see no label initially, but only after you click a tab (i.e. invoke the ChangeListener).
The easist solution is to provide the label text for the labels you created to add to the Tabbedpane:
public class Tabbedpane extends JFrame
{
Container container;
JTabbedPane tp;
JLabel laddUser = new JLabel("Add");
JLabel lmodifyUser = new JLabel("Modify");
...
And remove the mylabel stuff from the ChangeListener. Java will automatically switch between components contained within the JTabbedPane component, you don't have to worry about that. You do need the stateChanged method, but you only need to worry about putting in actions to handle things outside of displaying the selected component.
Here is your code, refined:
import javax.swing.*;
import javax.swing.event.*;
import java.awt.*;
import java.awt.event.*;
//public class ModifyProposal extends JInternalFrame
public class Tabbedpane extends JFrame
{
Container container;
JTabbedPane tp;
JLabel laddUser = new JLabel("Add");
JLabel lmodifyUser = new JLabel("Modify");
public Tabbedpane()
{
container=this.getContentPane();
//container.setLayout(null);
tp = new JTabbedPane();
tp.addTab("Add User",laddUser);
// When I click this tab, add User Details
//should be displayed
tp.addTab("Modify User",lmodifyUser);
// When I click this tab, Modify User Details
//should be displayed
tp.addChangeListener(new TPListener());
container.add(tp);
setSize(805,550);
setVisible(true);
}
class TPListener implements ChangeListener
{
JTabbedPane tempTP;
public void stateChanged(ChangeEvent ce)
{
int idx=0;
tempTP = (JTabbedPane) ce.getSource();
//JLabel myLabel1 = new JLabel("Add");
//JLabel myLabel2 = new JLabel("Modify");
//container.add(myLabel1);
//container.add(myLabel2);
idx = tempTP.getSelectedIndex();
if (idx==0)
{
//myLabel2.setVisible(false);
//myLabel1.setVisible(true);
}
else if (idx==1)
{
//myLabel1.setVisible(false);
//myLabel2.setVisible(true);
}
else
{
// do nothing
}
}
}
public static void main(String[] a)
{
Tabbedpane ob = new Tabbedpane();
}
} // end of pgm.
As far as for the placement of the tabs, the only things I am aware of is the JTabbedPane.setTabPlacement(int tabPlacement) method, which allows you to specify whether the tabs should be placed at the top, bottom, left, or right of the JTabbedPane. This can also be specified in the constructor.
Hope this helps.
Sean C. Mitchem
 
Arun Martin
Ranch Hand
Posts: 64
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Sean,
Thanks for the solution.It is perfectly working. I am enclosing the new code which has been modified to meet my expectations.
The Code:
import javax.swing.*;
import javax.swing.event.*;
import java.awt.*;
import java.awt.event.*;

class Tabbedpane extends JFrame
{
Container container;
Tabbedpane()
{
container=this.getContentPane();

JTabbedPane tp = new JTabbedPane();
tp.addTab("Add User",new AddUser());
tp.addTab("Modify User",new ModifyUser());

container.add(tp);
setSize(805,550);
setVisible(true);
} // end of constructor TabbedPane

public static void main(String[] a)
{
Tabbedpane ob = new Tabbedpane();
}
} // end of class Tabbedpane


class AddUser extends JPanel
{
public AddUser()
{
JLabel l1 = new JLabel("Add User Label");
JTextField tf1 = new JTextField(20);
JButton b1 = new JButton("ADD USER");

add(l1);
add(tf1);

add(b1);
} // end of AddUser
}// end of class AddUser
class ModifyUser extends JPanel implements ActionListener
{
JTextField tf2;
JButton b2;
public ModifyUser()
{
JLabel l2 = new JLabel("Modify User Label");
tf2 = new JTextField(20);
b2 = new JButton("MODIFY USER");
b2.addActionListener(this);
tf2.addActionListener(this);

add(l2);
add(tf2);
add(b2);
} // end of constructor ModifyUser

public void actionPerformed(ActionEvent evt)
{
String str = evt.getActionCommand();
if (str.equals("MODIFY USER"))
{
String getText = tf2.getText();
System.out.println(" getText is "+getText);
}
} // end of ActionPerformed in Class ModifyUser

} // end of Class ModifyUser

with warm regards,
Arun.


 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic