Help in adding a label and textfield
/*
* TabbedPaneDemo.java is a 1.4 example that requires one additional file:
*images/middle.gif.
*/
import javax.swing.*;
import javax.swing.JTabbedPane;
import javax.swing.ImageIcon;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JFrame;
import javax.swing.JComponent;
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.event.KeyEvent;
public class TabbedPaneDemo extends JPanel {
public TabbedPaneDemo() {
JTabbedPane tabbedPane = new JTabbedPane();
JComponent panel1 = makeTextPanel("Panel #1");
tabbedPane.addTab("Tab 1",panel1);
JComponent panel2 = makeTextPanel("Panel #2");
tabbedPane.addTab("Tab 2",panel2);
JComponent panel3 = makeTextPanel("Panel #3");
tabbedPane.addTab("Tab 3",panel3);
JComponent panel4 = makeTextPanel("Panel #4 ");
panel4.setPreferredSize(new Dimension(800, 600));
tabbedPane.addTab("Tab 4",panel4);
//Add the tabbed pane to this panel.
add(tabbedPane);
//Uncomment the following line to use scrolling tabs.
//tabbedPane.setTabLayoutPolicy(JTabbedPane.SCROLL_TAB_LAYOUT);
}
protected JComponent makeTextPanel(String text) {
JPanel panel = new JPanel(false);
JLabel filler = new JLabel(text);
filler.setHorizontalAlignment(JLabel.CENTER);
panel.setLayout(new GridLayout(1, 1));
panel.add(filler);
return panel;
}
/**
* Create the GUI and show it. For thread safety,
* this method should be invoked from the
* event-dispatching thread.
*/
private static void createAndShowGUI() {
//Make sure we have nice window decorations.
JFrame.setDefaultLookAndFeelDecorated(true);
//Create and set up the window.
JFrame frame = new JFrame("TabbedPaneDemo");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//Create and set up the content pane.
JComponent newContentPane = new TabbedPaneDemo();
newContentPane.setOpaque(true); //content panes must be opaque
frame.getContentPane().add(new TabbedPaneDemo(),
BorderLayout.CENTER);
//Display the window.
frame.pack();
frame.setVisible(true);
}
public static void main(String[] args) {
//Schedule a job for the event-dispatching thread:
//creating and showing this application's GUI.
javax.swing.SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGUI();
}
});
}
}
Actually this code consists of four tabs. i just need someone to help me to tell me where shall i add the code for creating the GUI.I want to add some texfields and labels in the respective tab. Actually i already have the code. I just need to knoe where shall i add it
Thank you very much
[2964 byte] By [
ajitpal] at [2007-9-30 20:53:50]

i have another minor problem here
/*
* TabbedPaneDemo.java is a 1.4 example that requires one additional file:
*images/middle.gif.
*/
import javax.swing.*;
import java.awt.*;
import javax.swing.JTabbedPane;
import javax.swing.ImageIcon;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JFrame;
import javax.swing.JComponent;
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.event.KeyEvent;
public class TabbedPaneDemo extends JPanel {
public TabbedPaneDemo() {
JTabbedPane tabbedPane = new JTabbedPane();
JComponent panel1 = makeTextPanel("Panel #1");
tabbedPane.addTab("Tab 1",panel1);
JComponent panel2 = makeTextPanel("Panel #2");
tabbedPane.addTab("Tab 2",panel2);
JComponent panel3 = makeTextPanel("Panel #3");
tabbedPane.addTab("Tab 3",panel3);
JComponent panel4 = makeTextPanel("Panel #4 ");
panel4.setPreferredSize(new Dimension(800, 600));
tabbedPane.addTab("Tab 4",panel4);
//Add the tabbed pane to this panel.
add(tabbedPane);
//Uncomment the following line to use scrolling tabs.
//tabbedPane.setTabLayoutPolicy(JTabbedPane.SCROLL_TAB_LAYOUT);
}
protected JComponent makeTextPanel(String text) {
//JPanel panel = new JPanel(false);
//JLabel labelName = new JLabel("It's a Label");
// panel.add(labelName);;
//JTextField filler = new JTextField(10);
//labelName.setHorizontalAlignment(JLabel.CENTER);
//panel.setLayout(new GridLayout(1, 1));
//panel.add(filler);
//return panel;
String[] labels = {"Name: ", "Fax: ", "Email: ", "Address: "};
int numPairs = labels.length;
//Create and populate the panel.
JPanel p = new JPanel(new SpringLayout());
for (int i = 0; i < numPairs; i++) {
JLabel l = new JLabel(labels, JLabel.TRAILING);
p.add(l);
JTextField textField = new JTextField(10);
l.setLabelFor(textField);
p.add(textField);
}
//Lay out the panel.
SpringUtilities.makeCompactGrid(p,
2, 4, //rows, cols
6, 6,//initX, initY
6, 6);//xPad, yPad
return p;
}
/**
* Create the GUI and show it. For thread safety,
* this method should be invoked from the
* event-dispatching thread.
*/
private static void createAndShowGUI() {
//Make sure we have nice window decorations.
JFrame.setDefaultLookAndFeelDecorated(true);
//Create and set up the window.
JFrame frame = new JFrame("TabbedPaneDemo");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//Create and set up the content pane.
JComponent newContentPane = new TabbedPaneDemo();
newContentPane.setOpaque(true); //content panes must be opaque
frame.getContentPane().add(new TabbedPaneDemo());
//Display the window.
frame.pack();
frame.setSize(1024,768);
frame.setVisible(true);
}
public static void main(String[] args) {
//Schedule a job for the event-dispatching thread:
//creating and showing this application's GUI.
javax.swing.SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGUI();
}
});
}
}
i have declared the panel as size 800*600..im adding 4 components in a row..like below
label: textfieldlabel: textfield
label: textfieldlabel: textfield
label: textfieldlabel: textfield
label: textfieldlabel: textfield
But i dunno why the text fields are becoming so big(occupying the empty screen)...
Any suggestions?
thnx