Hi Everyone,
I am putting a number of JTextAreas and JLabels (some with ImageIcons) onto a JPanel. I then put the JPanel onto the contentpane of a JFrame and make the end product the client of a scrollpane. This is all done in an effort to make a vertically scrollable help menu within my program.
At the moment I am still playing around with the program because the size of the container is way to big. I am using BoxLayout with the components (JTextAreas and JLabels) and have discovered that the only way to control the size of components with this layout manager is to use the set size methods one the containers components. To this end I have used setMaximumSize to set the maximum size of components (in the code below): however this has had limited success.
Strangely the JTextAreas have responded to my size requests however the JLabels appear to be ignoring those same requests completely.
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class HelpGUI extends JFrame implements ActionListener
{
HelpGUI()
{
Toolkit tk = Toolkit.getDefaultToolkit();
Dimension sSize = tk.getScreenSize();
/* Fonts created here. */
Font mainhead = new Font("Tahoma",Font.BOLD,22);
Font subhead = new Font("Arial Black",Font.PLAIN,16);
/* Images and there associated accomodations (labels) */
ImageIcon pic1 = new ImageIcon("Images/launch1a.jpg"
;
ImageIcon pic2 = new ImageIcon("Images/launch1b.jpg"
;
ImageIcon pic3 = new ImageIcon("Images/launch1c.jpg"
;
ImageIcon pic5 = new ImageIcon("Images/launch1e.jpg"
;
ImageIcon pic6 = new ImageIcon("Images/launch1f.jpg"
;
ImageIcon pic7 = new ImageIcon("Images/launch1g.jpg"
;
ImageIcon pic8 = new ImageIcon("Images/launch1h.jpg"
;
ImageIcon pic9 = new ImageIcon("Images/launch1i.jpg"
;
ImageIcon pic10 = new ImageIcon("Images/launch1j.jpg"
;
ImageIcon pic11 = new ImageIcon("Images/launch1k.jpg"
;
ImageIcon pic12 = new ImageIcon("Images/launch1l.jpg"
;
ImageIcon pic13 = new ImageIcon("Images/launch1m.jpg"
;
JLabel icon1 = new JLabel(pic1);
icon1.setMaximumSize(new Dimension(sSize.width*13/17,sSize.height*1/2));
//icon1.setAlignmentX(Component.LEFT_ALIGNMENT);
//icon1.setPreferredSize(new Dimension(sSize.width*13/17,sSize.height*1/2));
System.out.println(" "+icon1.getWidth()+" "+icon1.getHeight());//INTERESTING
//RESULT.
JLabel icon2 = new JLabel(pic2);
icon2.setMaximumSize(new Dimension(sSize.width*13/17,sSize.height*1/2));
JLabel icon3 = new JLabel(pic3);
icon3.setMaximumSize(new Dimension(sSize.width*13/17,sSize.height*2/3));
JLabel icon5 = new JLabel(pic5);
icon5.setMaximumSize(new Dimension(sSize.width*13/17,sSize.height*1));
JLabel icon6 = new JLabel(pic6);
icon6.setMaximumSize(new Dimension(sSize.width*13/17,sSize.height*1));
JLabel icon7 = new JLabel(pic7);
icon7.setMaximumSize(new Dimension(sSize.width*13/17,sSize.height*1));
JLabel icon8 = new JLabel(pic8);
icon8.setMaximumSize(new Dimension(sSize.width*13/17,sSize.height*1));
JLabel icon9 = new JLabel(pic9);
icon9.setMaximumSize(new Dimension(sSize.width*13/17,sSize.height*1));
JLabel icon10 = new JLabel(pic10);
icon10.setMaximumSize(new Dimension(sSize.width*13/17,sSize.height*1));
JLabel icon11 = new JLabel(pic11);
icon11.setMaximumSize(new Dimension(sSize.width*13/17,sSize.height*1));
JLabel icon12 = new JLabel(pic12);
icon12.setMaximumSize(new Dimension(sSize.width*13/17,sSize.height*1));
//Labels containing strings.
JLabel heading = new JLabel("INSTRUCTIONS",JLabel.LEFT);
heading.setFont(mainhead);
//heading.setAlignmentX(Component.LEFT_ALIGNMENT);
//heading.setForeground(Color.white); changes writing color.
//heading.setBackground(Color.white); does nothing visible.
heading.setMaximumSize(new Dimension(sSize.width*13/17,sSize.height*1/10));
JLabel addhead = new JLabel("Adding a shortcut to the toolbar."
;
addhead.setFont(subhead);
addhead.setMaximumSize(new Dimension(sSize.width*13/17,sSize.height*1/10));
JLabel delhead = new JLabel("Deleting a shortcut from the toolbar."
;
delhead.setFont(subhead);
delhead.setMaximumSize(new Dimension(sSize.width*13/17,sSize.height*1/10));
/* Text blocks describing program operation, used in conjunction with the
labels and images instantiated above. */
JTextArea intro = new JTextArea(
"This toolbar contains two established shortcut buttons. It also contains "+
"three empty shortcut buttons. The toolbar can be customised to your exact "+
"requirements. Please follow the instructions below if you are uncertain of "+
"how to enter a shortcut. Alternatively you can delete shortcuts from this "+
"toolbar by following instructions provided further down this page."
;
intro.setLineWrap(true);
intro.setWrapStyleWord(true);
intro.setEditable(false);
intro.setMaximumSize(new Dimension(sSize.width*13/17,sSize.height*1/10));
//intro.setBackground(this.getBackground());
JTextArea inst1 = new JTextArea(
"1). On the File menu click New."
;
inst1.setLineWrap(true);
inst1.setWrapStyleWord(true);
inst1.setEditable(false);
inst1.setMaximumSize(new Dimension(sSize.width*13/17,sSize.height*1/10));
//inst1.setSize(sSize.width*14/17,sSize.height*1/20);
//intro.setBackground(this.getBackground());
JTextArea inst2 = new JTextArea(
"2). In the Please Enter Shortcut Details window enter a Shortcut Name "+
"for the new shortcut."
;
inst2.setLineWrap(true);
inst2.setWrapStyleWord(true);
inst2.setEditable(false);
inst2.setMaximumSize(new Dimension(sSize.width*13/17,sSize.height*1/10));
//inst2.setBackground(this.getBackground());
JTextArea inst3 = new JTextArea(
"3). Click on the Open button. This presents the file browser."+
"4). Select a relevant subdirectory and click the Open button."
;
inst3.setLineWrap(true);
inst3.setWrapStyleWord(true);
inst3.setEditable(false);
inst3.setMaximumSize(new Dimension(sSize.width*13/17,sSize.height*1/10));
//inst3.setBackground(this.getBackground());
JTextArea inst5 = new JTextArea(
"5). Repeat step 4 if necessary."
;
inst5.setLineWrap(true);
inst5.setWrapStyleWord(true);
inst5.setEditable(false);
inst5.setMaximumSize(new Dimension(sSize.width*13/17,sSize.height*1/10));
//inst5.setBackground(this.getBackground());
JTextArea inst6 = new JTextArea(
"6). Repeat step 4 if necessary."
;
inst6.setLineWrap(true);
inst6.setWrapStyleWord(true);
inst6.setEditable(false);
inst6.setMaximumSize(new Dimension(sSize.width*13/17,sSize.height*1/10));
//inst6.setBackground(this.getBackground());
JTextArea inst7 = new JTextArea(
"7). Select the required executable file and press the Open button."
;
inst7.setLineWrap(true);
inst7.setWrapStyleWord(true);
inst7.setEditable(false);
inst7.setMaximumSize(new Dimension(sSize.width*13/17,sSize.height*1/10));
//inst7.setBackground(this.getBackground());
JTextArea inst8 = new JTextArea(
"8). Press the Ok button."
;
inst8.setLineWrap(true);
inst8.setWrapStyleWord(true);
inst8.setEditable(false);
inst8.setMaximumSize(new Dimension(sSize.width*13/17,sSize.height*1/10));
//inst8.setBackground(this.getBackground());
JButton exitbtn = new JButton("Exit"
;
JPanel panel = new JPanel();
panel.setLayout(new BoxLayout(panel,BoxLayout.Y_AXIS));
//panel.setSize(sSize.width*14/17,sSize.height*9/10);
panel.setBackground(Color.CYAN);
panel.add(heading); //"INSTRUCTIONS" heading.
panel.add(icon1); //First picture of toolbar.
panel.add(intro); //First text block.
panel.add(addhead); //"Adding a shortcut ..." heading.
panel.add(inst1); //First instruction 1).
panel.add(icon2); //Picture for 1).
panel.add(inst2); //Second instruction 2).
panel.add(icon5); //Picture of 2).
panel.add(inst3); //3). and 4).
panel.add(icon6); //Picture for 3 and 4.
panel.add(inst5); //5).
panel.add(icon7); //Picture for instruction 5).
panel.add(inst6); //6).
panel.add(icon8); //Picture for instruction 6).
panel.add(inst7); //7).
panel.add(icon9); //Pic 7).
panel.add(inst8); //8).
panel.add(icon10); //Pic 8).
Container c = getContentPane();
JScrollPane scrollPane = new JScrollPane(panel);
scrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
scrollPane.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
//scrollPane.setSize(sSize.width*14/17,sSize.height*2);
c.add(scrollPane);
//c.setSize(sSize.width*14/17,sSize.height*9/10);
setLocation(sSize.width*18/100,sSize.height*1/20);
setSize(sSize.width*14/17,sSize.height*9/10);
setTitle("Java QuickLaunch Toolbar"
;
setVisible(true);
}
public void actionPerformed(ActionEvent e)
{
}
}
If anybody has any ideas about why the JTextAreas are sizing correctly and the JLabels are not they will be greatly appreciated.
Thanks
I am putting a number of JTextAreas and JLabels (some with ImageIcons) onto a JPanel. I then put the JPanel onto the contentpane of a JFrame and make the end product the client of a scrollpane. This is all done in an effort to make a vertically scrollable help menu within my program.
At the moment I am still playing around with the program because the size of the container is way to big. I am using BoxLayout with the components (JTextAreas and JLabels) and have discovered that the only way to control the size of components with this layout manager is to use the set size methods one the containers components. To this end I have used setMaximumSize to set the maximum size of components (in the code below): however this has had limited success.
Strangely the JTextAreas have responded to my size requests however the JLabels appear to be ignoring those same requests completely.
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class HelpGUI extends JFrame implements ActionListener
{
HelpGUI()
{
Toolkit tk = Toolkit.getDefaultToolkit();
Dimension sSize = tk.getScreenSize();
/* Fonts created here. */
Font mainhead = new Font("Tahoma",Font.BOLD,22);
Font subhead = new Font("Arial Black",Font.PLAIN,16);
/* Images and there associated accomodations (labels) */
ImageIcon pic1 = new ImageIcon("Images/launch1a.jpg"
ImageIcon pic2 = new ImageIcon("Images/launch1b.jpg"
ImageIcon pic3 = new ImageIcon("Images/launch1c.jpg"
ImageIcon pic5 = new ImageIcon("Images/launch1e.jpg"
ImageIcon pic6 = new ImageIcon("Images/launch1f.jpg"
ImageIcon pic7 = new ImageIcon("Images/launch1g.jpg"
ImageIcon pic8 = new ImageIcon("Images/launch1h.jpg"
ImageIcon pic9 = new ImageIcon("Images/launch1i.jpg"
ImageIcon pic10 = new ImageIcon("Images/launch1j.jpg"
ImageIcon pic11 = new ImageIcon("Images/launch1k.jpg"
ImageIcon pic12 = new ImageIcon("Images/launch1l.jpg"
ImageIcon pic13 = new ImageIcon("Images/launch1m.jpg"
JLabel icon1 = new JLabel(pic1);
icon1.setMaximumSize(new Dimension(sSize.width*13/17,sSize.height*1/2));
//icon1.setAlignmentX(Component.LEFT_ALIGNMENT);
//icon1.setPreferredSize(new Dimension(sSize.width*13/17,sSize.height*1/2));
System.out.println(" "+icon1.getWidth()+" "+icon1.getHeight());//INTERESTING
//RESULT.
JLabel icon2 = new JLabel(pic2);
icon2.setMaximumSize(new Dimension(sSize.width*13/17,sSize.height*1/2));
JLabel icon3 = new JLabel(pic3);
icon3.setMaximumSize(new Dimension(sSize.width*13/17,sSize.height*2/3));
JLabel icon5 = new JLabel(pic5);
icon5.setMaximumSize(new Dimension(sSize.width*13/17,sSize.height*1));
JLabel icon6 = new JLabel(pic6);
icon6.setMaximumSize(new Dimension(sSize.width*13/17,sSize.height*1));
JLabel icon7 = new JLabel(pic7);
icon7.setMaximumSize(new Dimension(sSize.width*13/17,sSize.height*1));
JLabel icon8 = new JLabel(pic8);
icon8.setMaximumSize(new Dimension(sSize.width*13/17,sSize.height*1));
JLabel icon9 = new JLabel(pic9);
icon9.setMaximumSize(new Dimension(sSize.width*13/17,sSize.height*1));
JLabel icon10 = new JLabel(pic10);
icon10.setMaximumSize(new Dimension(sSize.width*13/17,sSize.height*1));
JLabel icon11 = new JLabel(pic11);
icon11.setMaximumSize(new Dimension(sSize.width*13/17,sSize.height*1));
JLabel icon12 = new JLabel(pic12);
icon12.setMaximumSize(new Dimension(sSize.width*13/17,sSize.height*1));
//Labels containing strings.
JLabel heading = new JLabel("INSTRUCTIONS",JLabel.LEFT);
heading.setFont(mainhead);
//heading.setAlignmentX(Component.LEFT_ALIGNMENT);
//heading.setForeground(Color.white); changes writing color.
//heading.setBackground(Color.white); does nothing visible.
heading.setMaximumSize(new Dimension(sSize.width*13/17,sSize.height*1/10));
JLabel addhead = new JLabel("Adding a shortcut to the toolbar."
addhead.setFont(subhead);
addhead.setMaximumSize(new Dimension(sSize.width*13/17,sSize.height*1/10));
JLabel delhead = new JLabel("Deleting a shortcut from the toolbar."
delhead.setFont(subhead);
delhead.setMaximumSize(new Dimension(sSize.width*13/17,sSize.height*1/10));
/* Text blocks describing program operation, used in conjunction with the
labels and images instantiated above. */
JTextArea intro = new JTextArea(
"This toolbar contains two established shortcut buttons. It also contains "+
"three empty shortcut buttons. The toolbar can be customised to your exact "+
"requirements. Please follow the instructions below if you are uncertain of "+
"how to enter a shortcut. Alternatively you can delete shortcuts from this "+
"toolbar by following instructions provided further down this page."
intro.setLineWrap(true);
intro.setWrapStyleWord(true);
intro.setEditable(false);
intro.setMaximumSize(new Dimension(sSize.width*13/17,sSize.height*1/10));
//intro.setBackground(this.getBackground());
JTextArea inst1 = new JTextArea(
"1). On the File menu click New."
inst1.setLineWrap(true);
inst1.setWrapStyleWord(true);
inst1.setEditable(false);
inst1.setMaximumSize(new Dimension(sSize.width*13/17,sSize.height*1/10));
//inst1.setSize(sSize.width*14/17,sSize.height*1/20);
//intro.setBackground(this.getBackground());
JTextArea inst2 = new JTextArea(
"2). In the Please Enter Shortcut Details window enter a Shortcut Name "+
"for the new shortcut."
inst2.setLineWrap(true);
inst2.setWrapStyleWord(true);
inst2.setEditable(false);
inst2.setMaximumSize(new Dimension(sSize.width*13/17,sSize.height*1/10));
//inst2.setBackground(this.getBackground());
JTextArea inst3 = new JTextArea(
"3). Click on the Open button. This presents the file browser."+
"4). Select a relevant subdirectory and click the Open button."
inst3.setLineWrap(true);
inst3.setWrapStyleWord(true);
inst3.setEditable(false);
inst3.setMaximumSize(new Dimension(sSize.width*13/17,sSize.height*1/10));
//inst3.setBackground(this.getBackground());
JTextArea inst5 = new JTextArea(
"5). Repeat step 4 if necessary."
inst5.setLineWrap(true);
inst5.setWrapStyleWord(true);
inst5.setEditable(false);
inst5.setMaximumSize(new Dimension(sSize.width*13/17,sSize.height*1/10));
//inst5.setBackground(this.getBackground());
JTextArea inst6 = new JTextArea(
"6). Repeat step 4 if necessary."
inst6.setLineWrap(true);
inst6.setWrapStyleWord(true);
inst6.setEditable(false);
inst6.setMaximumSize(new Dimension(sSize.width*13/17,sSize.height*1/10));
//inst6.setBackground(this.getBackground());
JTextArea inst7 = new JTextArea(
"7). Select the required executable file and press the Open button."
inst7.setLineWrap(true);
inst7.setWrapStyleWord(true);
inst7.setEditable(false);
inst7.setMaximumSize(new Dimension(sSize.width*13/17,sSize.height*1/10));
//inst7.setBackground(this.getBackground());
JTextArea inst8 = new JTextArea(
"8). Press the Ok button."
inst8.setLineWrap(true);
inst8.setWrapStyleWord(true);
inst8.setEditable(false);
inst8.setMaximumSize(new Dimension(sSize.width*13/17,sSize.height*1/10));
//inst8.setBackground(this.getBackground());
JButton exitbtn = new JButton("Exit"
JPanel panel = new JPanel();
panel.setLayout(new BoxLayout(panel,BoxLayout.Y_AXIS));
//panel.setSize(sSize.width*14/17,sSize.height*9/10);
panel.setBackground(Color.CYAN);
panel.add(heading); //"INSTRUCTIONS" heading.
panel.add(icon1); //First picture of toolbar.
panel.add(intro); //First text block.
panel.add(addhead); //"Adding a shortcut ..." heading.
panel.add(inst1); //First instruction 1).
panel.add(icon2); //Picture for 1).
panel.add(inst2); //Second instruction 2).
panel.add(icon5); //Picture of 2).
panel.add(inst3); //3). and 4).
panel.add(icon6); //Picture for 3 and 4.
panel.add(inst5); //5).
panel.add(icon7); //Picture for instruction 5).
panel.add(inst6); //6).
panel.add(icon8); //Picture for instruction 6).
panel.add(inst7); //7).
panel.add(icon9); //Pic 7).
panel.add(inst8); //8).
panel.add(icon10); //Pic 8).
Container c = getContentPane();
JScrollPane scrollPane = new JScrollPane(panel);
scrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
scrollPane.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
//scrollPane.setSize(sSize.width*14/17,sSize.height*2);
c.add(scrollPane);
//c.setSize(sSize.width*14/17,sSize.height*9/10);
setLocation(sSize.width*18/100,sSize.height*1/20);
setSize(sSize.width*14/17,sSize.height*9/10);
setTitle("Java QuickLaunch Toolbar"
setVisible(true);
}
public void actionPerformed(ActionEvent e)
{
}
}
If anybody has any ideas about why the JTextAreas are sizing correctly and the JLabels are not they will be greatly appreciated.
Thanks