Here in this example, we create a frame with different components.
Our frame would look like the figures below:
Fig. 1
Fig. 2
Step 1:
We declare variables which we are going to use for creating this frame.
JPanel contentPane;
TitledBorder titledBorder1;
Box jHeader = new Box(BoxLayout.X_AXIS);
// Components are laid out horizontly from let to
// right
// Horizontal arrangement will remain horizontal
// even if the frame is resized.
Box jHeaderInfo = new Box(BoxLayout.Y_AXIS);
// Components are laid down vertically from
// top to bottom.
// Vertical arrangtement will remain vertical
// even if the frame is resized.
Box jButtonPane = new Box(BoxLayout.X_AXIS);
JScrollPane jScrollPaneStructures = new JScrollPane();
// Creates an empty scroll pane
// where both horizontal and vertical
// scroll bars appear when needed.
JPanel jPanelStructures = new JPanel();
JLabel jIconLabel = new JLabel();
JLabel jInfoLabel1 = new JLabel();
JLabel jInfoLabel2 = new JLabel();
JLabel jInfoLabel3 = new JLabel();
JButton jStartButton = new JButton();
JPanel jPanel1 = new JPanel();
JScrollPane jTraceScrollPane = new JScrollPane();
JTextArea jTraceTextArea = new JTextArea();
JLabel jSLSRPTSELLLabel0 = new JLabel();
JTextField jSLSRPTSELLTextField0 = new JTextField();
JLabel jSLSRPT3Label1 = new JLabel();
JTextField jSLSRPT3TextField1 = new JTextField();
JLabel jSLSRPT2Label2 = new JLabel();
JTextField jSLSRPT2TextField2 = new JTextField();
JLabel jSLSRPTTRANLabel3 = new JLabel();
JTextField jSLSRPTTRANTextField3 = new JTextField();
JLabel jSLSRPT3Label4 = new JLabel();
JTextField jSLSRPT3TextField4 = new JTextField();
JLabel jSLSRPT4Label5 = new JLabel();
JTextField jSLSRPT4TextField5 = new JTextField();
Step 2:
We generate the first componet which is a header and which displays the information.
jInfoLabel1.setText("THIS APPLICATION WAS GENERATED BY MapForce 2009");
jInfoLabel2.setForeground(Color.blue);
jInfoLabel2.setText("http://www.altova.com/mapforce");
jInfoLabel3.setText("Please check the input and output files, and press the Start button...");
jHeaderInfo.add(jInfoLabel1,0);
jHeaderInfo.add(jInfoLabel2,1);
jHeaderInfo.add(jInfoLabel3,2);
// Since jHeaderInfo uses BoxLayout.Y_AXIS, so each label below the other.
jIconLabel.setIcon(new ImageIcon(SLSRPTFrame.class.getResource("mapforce.png")));
jIconLabel.setText("");
jHeader.add(jIconLabel);
jHeader.add(Box.createHorizontalStrut(15));
// Box.createHorizontalStrut(width)
// creates a component that is invisible.
// except that is has specified width.
jHeader.add(jHeaderInfo);
jHeader.add(Box.createGlue());
// “glue” component is a gooey substance that expands
// as much as necessary to fill the space between
// neighboring components
For example, suppose you have a horizontal box that contains two fixed-size components. If the box gets extra space, the fixed-size components won't become larger, so where does the extra space go? Without glue, the extra space goes to the right of the second component. If you put glue between the fixed-size components, then the extra space goes there.
Step 3:
Next we create the scroll pane which displays the input files:
jScrollPaneStructures.setBorder(BorderFactory.createLineBorder(Color.black));
jScrollPaneStructures.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
// Sets the horizontal scrollbar-display
// policy.
// In figure we see no horizontal bar.
jScrollPaneStructures.setAutoscrolls(true);
jPanelStructures.setLayout(null);
fillScrollPane(); // This method is called to fill the
// scroll pane. Method is discussed
// in the bottom.
jScrollPaneStructures.getViewport().add(jPanelStructures, null);
The JViewport provides a window, or "viewport" onto a data source -- for example, a text file. That data source is the "scrollable client" (aka data model) displayed by the JViewport view. A JScrollPane basically consists ofJScrollBars, a JViewport, and the wiring between them, as shown above diagram.
The scroll pane's client is also known as the view or viewport view.
A scroll pane uses a JViewport instance to manage the visible area of the client. The viewport is responsible for positioning and sizing the client, based on the positions of the scroll bars, and displaying it.
The "viewport" or "porthole" through which you see the underlying information. When you scroll, what moves is the viewport. It is like peering through a camera's viewfinder. Moving the viewfinder upwards brings new things into view at the top of the picture and loses things that were at the bottom.
jScrollPaneStructures.getViewport().add(jPanelStructures, null);
// here we add the jPanelStructures
// to the ViewPort of jScrollPaneStructures
// And fillScrollPane() method creates or fills the
// jPanelStructures
void fillScrollPane() {
jSLSRPTSELLLabel0.setText("Source instance of SLSRPTSELL file:");
jSLSRPTSELLLabel0.setBounds(new Rectangle(15, 10, 438, 23));
jPanelStructures.add(jSLSRPTSELLLabel0, null);
jSLSRPTSELLTextField0.setText("D:/Documents and Settings/maxisadm/My Documents/Projects/EDIFACT/MapForce Designs/New Formats/CSV/SLSRPTSELL_TESTCASE1.csv");
jSLSRPTSELLTextField0.setBounds(new Rectangle(15, 35, 438, 23));
jSLSRPTSELLTextField0.setEditable(false);
jPanelStructures.add(jSLSRPTSELLTextField0, null);
jSLSRPT3Label1.setText("Source instance of SLSRPT3.xsd:");
jSLSRPT3Label1.setBounds(new Rectangle(15, 60, 438, 23));
jPanelStructures.add(jSLSRPT3Label1, null);
jSLSRPT3TextField1.setText("D:/Documents and Settings/maxisadm/My Documents/Projects/EDIFACT/MapForce Designs/New Formats/Constants/SLSRPT.xml");
jSLSRPT3TextField1.setBounds(new Rectangle(15, 85, 438, 23));
jSLSRPT3TextField1.setEditable(false);
jPanelStructures.add(jSLSRPT3TextField1, null);
jSLSRPT2Label2.setText("Instance of SLSRPT2 file:");
jSLSRPT2Label2.setBounds(new Rectangle(15, 110, 438, 23));
jPanelStructures.add(jSLSRPT2Label2, null);
jSLSRPT2TextField2.setText("SLSRPT.edi");
jSLSRPT2TextField2.setBounds(new Rectangle(15, 135, 438, 23));
jSLSRPT2TextField2.setEditable(false);
jPanelStructures.add(jSLSRPT2TextField2, null);
jSLSRPTTRANLabel3.setText("Source instance of SLSRPTTRAN file:");
jSLSRPTTRANLabel3.setBounds(new Rectangle(15, 160, 438, 23));
jPanelStructures.add(jSLSRPTTRANLabel3, null);
jSLSRPTTRANTextField3.setText("D:/Documents and Settings/maxisadm/My Documents/Projects/EDIFACT/MapForce Designs/New Formats/CSV/SLSRPTTRAN_TESTCASE3.csv");
jSLSRPTTRANTextField3.setBounds(new Rectangle(15, 185, 438, 23));
jSLSRPTTRANTextField3.setEditable(false);
jPanelStructures.add(jSLSRPTTRANTextField3, null);
jSLSRPT3Label4.setText("Source instance of SLSRPT3.xsd:");
jSLSRPT3Label4.setBounds(new Rectangle(15, 210, 438, 23));
jPanelStructures.add(jSLSRPT3Label4, null);
jSLSRPT3TextField4.setText("D:/Documents and Settings/maxisadm/My Documents/Projects/EDIFACT/MapForce Designs/New Formats/Constants/SLSRPT.xml");
jSLSRPT3TextField4.setBounds(new Rectangle(15, 235, 438, 23));
jSLSRPT3TextField4.setEditable(false);
jPanelStructures.add(jSLSRPT3TextField4, null);
jSLSRPT4Label5.setText("Instance of SLSRPT4 file:");
jSLSRPT4Label5.setBounds(new Rectangle(15, 260, 438, 23));
jPanelStructures.add(jSLSRPT4Label5, null);
jSLSRPT4TextField5.setText("SLSRPT.edi");
jSLSRPT4TextField5.setBounds(new Rectangle(15, 285, 438, 23));
jSLSRPT4TextField5.setEditable(false);
jPanelStructures.add(jSLSRPT4TextField5, null);
jPanelStructures.setLayout(null);
jPanelStructures.setPreferredSize(new Dimension(285, 500));
jPanelStructures.setSize(new Dimension(285, 500));
jPanelStructures.setMinimumSize(new Dimension(285, 500));
jPanelStructures.setMaximumSize(new Dimension(285, 500));
}
Step 4:
We, also see that in the bottom of frame there is a text box with vertical scroll pane. Below is the code for creating that:
First, we create the text area:
jTraceTextArea.setBackground(Color.white);
jTraceTextArea.setForeground(Color.black);
jTraceTextArea.setToolTipText("");
jTraceTextArea.setText("");
jTraceTextArea.setRows(20);
Secondly, we create the scroll pane:
jTraceScrollPane.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
jTraceScrollPane.setViewportBorder(null);
jTraceScrollPane.setAutoscrolls(true);
jTraceScrollPane.setBorder(BorderFactory.createLineBorder(Color.black));
jTraceScrollPane.setDebugGraphicsOptions(0);
jTraceScrollPane.setToolTipText("");
jTraceScrollPane.setVerifyInputWhenFocusTarget(true);
jTraceScrollPane.getViewport().add(jTraceTextArea, null);
jTraceScrollPane.setPreferredSize(new Dimension(0, 200));
Step 5:
We create the component “Start” button.
jStartButton.setFont(new java.awt.Font("Dialog", 0, 11));
jStartButton.setText("Start");
jStartButton.addActionListener(new SLSRPTFrame_jStartButton_actionAdapter(this));
jButtonPane.add(Box.createHorizontalStrut(5));
jButtonPane.add(jStartButton);
jButtonPane.add(Box.createGlue());
Step 6:
Finally, we add all components to the frame:
contentPane = (JPanel)this.getContentPane();
titledBorder1 = new TitledBorder("");
contentPane.setLayout(new BoxLayout(contentPane, BoxLayout.Y_AXIS));
this.setSize(new Dimension(603, 511));
this.setTitle("Mapforce Application");
contentPane.add(jHeader, 0);
contentPane.add(jScrollPaneStructures, 1);
contentPane.add(jButtonPane, 2);
contentPane.add(jTraceScrollPane, 3);
Our frame would look like the figures below:
Fig. 1
Fig. 2
Step 1:
We declare variables which we are going to use for creating this frame.
JPanel contentPane;
TitledBorder titledBorder1;
Box jHeader = new Box(BoxLayout.X_AXIS);
// Components are laid out horizontly from let to
// right
// Horizontal arrangement will remain horizontal
// even if the frame is resized.
Box jHeaderInfo = new Box(BoxLayout.Y_AXIS);
// Components are laid down vertically from
// top to bottom.
// Vertical arrangtement will remain vertical
// even if the frame is resized.
Box jButtonPane = new Box(BoxLayout.X_AXIS);
JScrollPane jScrollPaneStructures = new JScrollPane();
// Creates an empty scroll pane
// where both horizontal and vertical
// scroll bars appear when needed.
JPanel jPanelStructures = new JPanel();
JLabel jIconLabel = new JLabel();
JLabel jInfoLabel1 = new JLabel();
JLabel jInfoLabel2 = new JLabel();
JLabel jInfoLabel3 = new JLabel();
JButton jStartButton = new JButton();
JPanel jPanel1 = new JPanel();
JScrollPane jTraceScrollPane = new JScrollPane();
JTextArea jTraceTextArea = new JTextArea();
JLabel jSLSRPTSELLLabel0 = new JLabel();
JTextField jSLSRPTSELLTextField0 = new JTextField();
JLabel jSLSRPT3Label1 = new JLabel();
JTextField jSLSRPT3TextField1 = new JTextField();
JLabel jSLSRPT2Label2 = new JLabel();
JTextField jSLSRPT2TextField2 = new JTextField();
JLabel jSLSRPTTRANLabel3 = new JLabel();
JTextField jSLSRPTTRANTextField3 = new JTextField();
JLabel jSLSRPT3Label4 = new JLabel();
JTextField jSLSRPT3TextField4 = new JTextField();
JLabel jSLSRPT4Label5 = new JLabel();
JTextField jSLSRPT4TextField5 = new JTextField();
Step 2:
We generate the first componet which is a header and which displays the information.
jInfoLabel1.setText("THIS APPLICATION WAS GENERATED BY MapForce 2009");
jInfoLabel2.setForeground(Color.blue);
jInfoLabel2.setText("http://www.altova.com/mapforce");
jInfoLabel3.setText("Please check the input and output files, and press the Start button...");
jHeaderInfo.add(jInfoLabel1,0);
jHeaderInfo.add(jInfoLabel2,1);
jHeaderInfo.add(jInfoLabel3,2);
// Since jHeaderInfo uses BoxLayout.Y_AXIS, so each label below the other.
jIconLabel.setIcon(new ImageIcon(SLSRPTFrame.class.getResource("mapforce.png")));
jIconLabel.setText("");
jHeader.add(jIconLabel);
jHeader.add(Box.createHorizontalStrut(15));
// Box.createHorizontalStrut(width)
// creates a component that is invisible.
// except that is has specified width.
jHeader.add(jHeaderInfo);
jHeader.add(Box.createGlue());
// “glue” component is a gooey substance that expands
// as much as necessary to fill the space between
// neighboring components
For example, suppose you have a horizontal box that contains two fixed-size components. If the box gets extra space, the fixed-size components won't become larger, so where does the extra space go? Without glue, the extra space goes to the right of the second component. If you put glue between the fixed-size components, then the extra space goes there.
Step 3:
Next we create the scroll pane which displays the input files:
jScrollPaneStructures.setBorder(BorderFactory.createLineBorder(Color.black));
jScrollPaneStructures.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
// Sets the horizontal scrollbar-display
// policy.
// In figure we see no horizontal bar.
jScrollPaneStructures.setAutoscrolls(true);
jPanelStructures.setLayout(null);
fillScrollPane(); // This method is called to fill the
// scroll pane. Method is discussed
// in the bottom.
jScrollPaneStructures.getViewport().add(jPanelStructures, null);
The JViewport provides a window, or "viewport" onto a data source -- for example, a text file. That data source is the "scrollable client" (aka data model) displayed by the JViewport view. A JScrollPane basically consists ofJScrollBars, a JViewport, and the wiring between them, as shown above diagram.
The scroll pane's client is also known as the view or viewport view.
A scroll pane uses a JViewport instance to manage the visible area of the client. The viewport is responsible for positioning and sizing the client, based on the positions of the scroll bars, and displaying it.
The "viewport" or "porthole" through which you see the underlying information. When you scroll, what moves is the viewport. It is like peering through a camera's viewfinder. Moving the viewfinder upwards brings new things into view at the top of the picture and loses things that were at the bottom.
jScrollPaneStructures.getViewport().add(jPanelStructures, null);
// here we add the jPanelStructures
// to the ViewPort of jScrollPaneStructures
// And fillScrollPane() method creates or fills the
// jPanelStructures
void fillScrollPane() {
jSLSRPTSELLLabel0.setText("Source instance of SLSRPTSELL file:");
jSLSRPTSELLLabel0.setBounds(new Rectangle(15, 10, 438, 23));
jPanelStructures.add(jSLSRPTSELLLabel0, null);
jSLSRPTSELLTextField0.setText("D:/Documents and Settings/maxisadm/My Documents/Projects/EDIFACT/MapForce Designs/New Formats/CSV/SLSRPTSELL_TESTCASE1.csv");
jSLSRPTSELLTextField0.setBounds(new Rectangle(15, 35, 438, 23));
jSLSRPTSELLTextField0.setEditable(false);
jPanelStructures.add(jSLSRPTSELLTextField0, null);
jSLSRPT3Label1.setText("Source instance of SLSRPT3.xsd:");
jSLSRPT3Label1.setBounds(new Rectangle(15, 60, 438, 23));
jPanelStructures.add(jSLSRPT3Label1, null);
jSLSRPT3TextField1.setText("D:/Documents and Settings/maxisadm/My Documents/Projects/EDIFACT/MapForce Designs/New Formats/Constants/SLSRPT.xml");
jSLSRPT3TextField1.setBounds(new Rectangle(15, 85, 438, 23));
jSLSRPT3TextField1.setEditable(false);
jPanelStructures.add(jSLSRPT3TextField1, null);
jSLSRPT2Label2.setText("Instance of SLSRPT2 file:");
jSLSRPT2Label2.setBounds(new Rectangle(15, 110, 438, 23));
jPanelStructures.add(jSLSRPT2Label2, null);
jSLSRPT2TextField2.setText("SLSRPT.edi");
jSLSRPT2TextField2.setBounds(new Rectangle(15, 135, 438, 23));
jSLSRPT2TextField2.setEditable(false);
jPanelStructures.add(jSLSRPT2TextField2, null);
jSLSRPTTRANLabel3.setText("Source instance of SLSRPTTRAN file:");
jSLSRPTTRANLabel3.setBounds(new Rectangle(15, 160, 438, 23));
jPanelStructures.add(jSLSRPTTRANLabel3, null);
jSLSRPTTRANTextField3.setText("D:/Documents and Settings/maxisadm/My Documents/Projects/EDIFACT/MapForce Designs/New Formats/CSV/SLSRPTTRAN_TESTCASE3.csv");
jSLSRPTTRANTextField3.setBounds(new Rectangle(15, 185, 438, 23));
jSLSRPTTRANTextField3.setEditable(false);
jPanelStructures.add(jSLSRPTTRANTextField3, null);
jSLSRPT3Label4.setText("Source instance of SLSRPT3.xsd:");
jSLSRPT3Label4.setBounds(new Rectangle(15, 210, 438, 23));
jPanelStructures.add(jSLSRPT3Label4, null);
jSLSRPT3TextField4.setText("D:/Documents and Settings/maxisadm/My Documents/Projects/EDIFACT/MapForce Designs/New Formats/Constants/SLSRPT.xml");
jSLSRPT3TextField4.setBounds(new Rectangle(15, 235, 438, 23));
jSLSRPT3TextField4.setEditable(false);
jPanelStructures.add(jSLSRPT3TextField4, null);
jSLSRPT4Label5.setText("Instance of SLSRPT4 file:");
jSLSRPT4Label5.setBounds(new Rectangle(15, 260, 438, 23));
jPanelStructures.add(jSLSRPT4Label5, null);
jSLSRPT4TextField5.setText("SLSRPT.edi");
jSLSRPT4TextField5.setBounds(new Rectangle(15, 285, 438, 23));
jSLSRPT4TextField5.setEditable(false);
jPanelStructures.add(jSLSRPT4TextField5, null);
jPanelStructures.setLayout(null);
jPanelStructures.setPreferredSize(new Dimension(285, 500));
jPanelStructures.setSize(new Dimension(285, 500));
jPanelStructures.setMinimumSize(new Dimension(285, 500));
jPanelStructures.setMaximumSize(new Dimension(285, 500));
}
Step 4:
We, also see that in the bottom of frame there is a text box with vertical scroll pane. Below is the code for creating that:
First, we create the text area:
jTraceTextArea.setBackground(Color.white);
jTraceTextArea.setForeground(Color.black);
jTraceTextArea.setToolTipText("");
jTraceTextArea.setText("");
jTraceTextArea.setRows(20);
Secondly, we create the scroll pane:
jTraceScrollPane.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
jTraceScrollPane.setViewportBorder(null);
jTraceScrollPane.setAutoscrolls(true);
jTraceScrollPane.setBorder(BorderFactory.createLineBorder(Color.black));
jTraceScrollPane.setDebugGraphicsOptions(0);
jTraceScrollPane.setToolTipText("");
jTraceScrollPane.setVerifyInputWhenFocusTarget(true);
jTraceScrollPane.getViewport().add(jTraceTextArea, null);
jTraceScrollPane.setPreferredSize(new Dimension(0, 200));
Step 5:
We create the component “Start” button.
jStartButton.setFont(new java.awt.Font("Dialog", 0, 11));
jStartButton.setText("Start");
jStartButton.addActionListener(new SLSRPTFrame_jStartButton_actionAdapter(this));
jButtonPane.add(Box.createHorizontalStrut(5));
jButtonPane.add(jStartButton);
jButtonPane.add(Box.createGlue());
Step 6:
Finally, we add all components to the frame:
contentPane = (JPanel)this.getContentPane();
titledBorder1 = new TitledBorder("");
contentPane.setLayout(new BoxLayout(contentPane, BoxLayout.Y_AXIS));
this.setSize(new Dimension(603, 511));
this.setTitle("Mapforce Application");
contentPane.add(jHeader, 0);
contentPane.add(jScrollPaneStructures, 1);
contentPane.add(jButtonPane, 2);
contentPane.add(jTraceScrollPane, 3);