I have following code for my java swing application which is executing fine in eclipse IDE but when I am embedding it in HTML then not executing in browser,just showing blank box.
Java swing code:
import javax.swing.*;
import java.applet.*;
import java.awt.*;
public class Form extends JApplet{
public void init()
{
JFrame frame = new JFrame("Form");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setResizable(false);
JPanel panel = new JPanel();
JLabel label1 = new JLabel("");
JTextField field = new JTextField(20);
//JButton button1 = new JButton("OK");
//JButton button2 = new JButton("Cancel");
Container c;
c=frame.getContentPane();
c.setLayout(null);
JLabel name=new JLabel("Name :");
JLabel compcode=new JLabel("Company Code :");
JLabel cardno=new JLabel("Card Number: ");
JLabel cardtype=new JLabel("Card Type :");
JLabel pin=new JLabel("Pin :");
JLabel bldgrp=new JLabel("Blood Group :");
JLabel empcode=new JLabel("Employee Code :");
JLabel dob=new JLabel("DOB :");
JLabel valupto=new JLabel("Valid Upto :");
JLabel jdate=new JLabel("Joining Date :");
JLabel dept=new JLabel("Department :");
JLabel uid=new JLabel("UID :");
JTextField nametxt=new JTextField(10);
JComboBox compcodetxt=new JComboBox();
JTextField cardnumtxt=new JTextField(10);
JTextField cardtypetxt=new JTextField(10);
JTextField pintxt=new JTextField(10);
JComboBox bldgrptxt=new JComboBox();
JTextField empcodetxt=new JTextField(10);
JTextField dobtxt=new JTextField(10);
JTextField valuptotxt=new JTextField(10);
JTextField jdatetxt=new JTextField(10);
JTextField depttxt=new JTextField(10);
JTextField uidtxt=new JTextField(10);
name.setBounds(10, 10, 100, 25);
nametxt.setBounds(110, 10, 100, 25);
compcode.setBounds(10, 40, 100, 25);
compcodetxt.setBounds(110, 40, 100, 25);
cardno.setBounds(10, 70, 100, 25);
cardnumtxt.setBounds(110, 70, 100, 25);
pin.setBounds(10, 110, 100, 25);
pintxt.setBounds(110, 110, 100, 25);
bldgrp.setBounds(10, 140, 100, 25);
bldgrptxt.setBounds(110, 140, 100, 25);
empcode.setBounds(10, 170, 100, 25);
empcodetxt.setBounds(110, 170, 100, 25);
dob.setBounds(10, 200, 100, 25);
dobtxt.setBounds(110, 200, 100, 25);
valupto.setBounds(10, 230, 100, 25);
valuptotxt.setBounds(110, 230, 100, 25);
jdate.setBounds(10, 260, 100, 25);
jdatetxt.setBounds(110, 260, 100, 25);
dept.setBounds(10, 290, 100, 25);
depttxt.setBounds(110, 290, 100, 25);
uid.setBounds(10, 320, 100, 25);
uidtxt.setBounds(110, 320, 100, 25);
//button1.setBounds(10, 50, 75, 25);
//button2.setBounds(10, 70, 75, 25);
c.add(name); c.add(nametxt);
c.add(compcode); c.add(compcodetxt);
c.add(cardno); c.add(cardnumtxt);
c.add(pin); c.add(pintxt);
c.add(bldgrp); c.add(bldgrptxt);
c.add(empcode); c.add(empcodetxt);
c.add(dob); c.add(dobtxt);
c.add(valupto); c.add(valuptotxt);
c.add(jdate); c.add(jdatetxt);
c.add(dept); c.add(depttxt);
c.add(uid); c.add(uidtxt);
//panel.add(button1);
//panel.add(button2);
//frame.add(panel);
frame.setSize(350,400);
//frame.pack();
frame.setVisible(true);
}
}
HTML code for embedding it in is as follows:
<html>
<head>
<title>Test Page</title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
</head>
<applet Archive ="Form.jar" Code="com.vms.util.Form" WIDTH="250" HEIGHT="300" >
</applet>
</html>
I generated JAR for my swing class Form.jar with package com.vms.util
I kept it in my D: drive form directory and put all html and jar in form directory.
I am able to run my swing application in HTML, how do I run it?
When I am running above code using appletviewer I am getting following error
D:\form>appletviewer Form2.html
java.security.AccessControlException: access denied (java.lang.RuntimePermission
exitVM.0)
at java.security.AccessControlContext.checkPermission(AccessControlConte
xt.java:374)
at java.security.AccessController.checkPermission(AccessController.java:
546)
at java.lang.SecurityManager.checkPermission(SecurityManager.java:532)
at java.lang.SecurityManager.checkExit(SecurityManager.java:744)
at javax.swing.JFrame.setDefaultCloseOperation(JFrame.java:372)
at Form.init(Form.java:10)
at sun.applet.AppletPanel.run(AppletPanel.java:424)
at java.lang.Thread.run(Thread.java:662)
comment following line
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Applets are not allowed (except by modifying directly the Java Security Policy on the client machine) to perform some critical calls. Even if using signed applets.
In your case, at javax.swing.JFrame.setDefaultCloseOperation is triggering the exception
it seems to be a small mistake, just enable java console and run the application.
it will help you to know what is the issue
PS: to enable java console on windows
go to control panel
select java
go to advance tab
Select applet lifecycle exception under Debugging section
Select Show console under Java Console Section.
Hope this will help you
Related
I want to display a Multiline JLabel into JPanel.
So I have this code but I'm not able to show the multiline JLabel.
public class NotificationFrame extends JFrame{
public NotificationFrame(){
JPanel panelBody = new JPanel();
panelBody.setBackground(Color.white);
GridBagConstraints GBC2 = new GridBagConstraints();
Container CR2 = new Container();
GridBagLayout GBL2 = new GridBagLayout();
CR2.setLayout(GBL2);
panelBody.add(CR2);
GBC2 = new GridBagConstraints();
CR2.add(labelTesto);
GBC2.gridx=0;
GBC2.gridy=0;
GBC2.insets.left = 10;
GBC2.insets.top=0;
GBL2.setConstraints(labelTesto,GBC2);
panelBody.setLayout(new FlowLayout(FlowLayout.CENTER));
add(panelBody,BorderLayout.CENTER);
}
}
If I change the last line of code in
add(labelTest,BorderLayout.CENTER);
I can show that I want. But it is not correct because I want to set a padding on JLabel
EDIT
I have use this code now:
JPanel panelBody = new JPanel();
panelBody.setBackground(Color.white);
SpringLayout layout = new SpringLayout();
panelBody.setLayout(layout);
panelBody.add(labelTesto);
layout.putConstraint(SpringLayout.NORTH, labelTesto, 15, SpringLayout.NORTH, panelBody);
add(panelBody,BorderLayout.CENTER);
This is the layout:
This is the all test that I should see:
"Il 31 Dicembre scadrĂ l'assistenza, ricorda di rinnovare l'assistenza per ricevere sempre assistenza ed aggiornamenti."
Multiline it's not padding. For achieve multiline text in JLabel or JButton you need to use HTML.
To wrap text (or multiline):
btn.setText("<html><p>Il 31 Dicembre scadrĂ l'assistenza, ricorda di rinnovare l'assistenza per ricevere sempre assistenza ed aggiornamenti</p></html>");
label.setText("<html>Date:<br></html>"+getDateFromSomeWhere);
To set padding:
label.setBorder(new EmptyBorder(0,10,0,0));//top,left,bottom,right
Is there any way to set font of the title of the titled border in java swing?
JPanel panelDOB = new JPanel();
panelDOB.setBorder(new TitledBorder(new LineBorder(new Color(171, 173, 179)), "DATE OF BIRTH", TitledBorder.LEADING, TitledBorder.TOP, null, new Color(109, 109, 109)));
panelDOB.setBackground(Color.WHITE);
panelDOB.setFont(new Font("Tw Cen MT", Font.PLAIN, 12));
sl_panelFac.putConstraint(SpringLayout.NORTH, panelDOB, 6, SpringLayout.SOUTH, txtNationality);
sl_panelFac.putConstraint(SpringLayout.WEST, panelDOB, 10, SpringLayout.WEST, panelFac);
sl_panelFac.putConstraint(SpringLayout.SOUTH, panelDOB, 61, SpringLayout.SOUTH, txtNationality);
sl_panelFac.putConstraint(SpringLayout.EAST, panelDOB, 196, SpringLayout.WEST, panelFac);
panelFac.add(panelDOB);
panelDOB.setLayout(new SpringLayout());
This is answered by simply looking up TitledBorder in the Java API.
There you'll find two constructors (here's one),
public TitledBorder(Border border,
String title,
int titleJustification,
int titlePosition,
Font titleFont)
that take a font as well as a method for setting the font,
public void setTitleFont(Font titleFont)
Alternatively, there are two methods in the BorderFactory that creates a TitledBorder with your selected font, again available through the Java API.
public static TitledBorder createTitledBorder(Border border,
String title,
int titleJustification,
int titlePosition,
Font titleFont)
I'm frankly a little surprised that you didn't look into the API before coming here.
I can't figure out why this doesn't work... It hides the JTextArea.
JTextArea _area=new JTextArea();
_area.setBounds(10, 10, 465, 250);
add(_area);
JScrollPane scroller=new JScrollPane(_area);
scroller.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
add(scroller);
EDIT:
Shuffle the bound values set the bounds only of ScrollPane and not of TextArea.
JTextArea _area = new JTextArea();
// _area.setBounds(10, 10, 400, 250);
add(_area);
JScrollPane scroller = new JScrollPane(_area);
scroller.setBounds(10, 10, 400, 250);
scroller.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
add(scroller);
These images show a chat box that needs to be resized with the application frame. I'm using GroupLayout to do so. The hierarchy of the frame goes something like this
chatDiv(JPanel, GroupLayout)->chatTabbedPane(JTabbedPane, GroupLayout)
->chatPanel(extended JPanel, GroupLayout)
But, these individual chat_tabs(chatPanel) are added to the chatTabbedPane dynamically during application runtime.
Problem is: These chatPanel(s) are expanding with the expansion of application
frame but they are not shrinking with the shrink in width of application frame.
initialized frame
and when I expand and again shrink the application frame this happens (size of chatPanel doesn't shrink leading to invisible overflown region)..
expanded and again shrinked frame
Here is the class chatPanel that extends JPanel:
class chatPanel extends JPanel{
private static final long serialVersionUID = 1L;
GroupLayout gl_panel;
JTextArea textArea;
JTextField textField;
JButton sendTextButton;
JButton closeChatButton;
int replyRef;
public chatPanel(int ref){
//this.setLayout(null);
closeChatButton = new JButton("");
sendTextButton = new JButton("SEND");
textField = new JTextField();
textArea = new JTextArea();
replyRef = ref;
textArea.setWrapStyleWord(true);
textArea.setEditable(false);
textArea.setLineWrap(true);
//textArea.setBounds(0, 0, 370, 99);
this.add(textArea);
//textField.setBounds(0, 101, 290, 20);
//textField.setColumns(10);
this.add(textField);
sendTextButton.setFont(new Font("Tahoma", Font.PLAIN, 10));
//sendTextButton.setBounds(290, 101, 60, 19);
this.add(sendTextButton);
closeChatButton.setIcon(new ImageIcon("files/close.png"));
closeChatButton.setSelectedIcon(null);
closeChatButton.setToolTipText("CLOSE");
closeChatButton.setFont(new Font("Tahoma", Font.PLAIN, 5));
//closeChatButton.setBounds(350, 101, 20, 19);
this.add(closeChatButton);
gl_panel = new GroupLayout(this);
gl_panel.setHorizontalGroup(
gl_panel.createParallelGroup(Alignment.TRAILING)
.addGroup(gl_panel.createSequentialGroup()
.addGap(1)
.addComponent(textField, GroupLayout.DEFAULT_SIZE, 286, Short.MAX_VALUE)
.addGap(1)
.addComponent(sendTextButton, GroupLayout.PREFERRED_SIZE, 60, GroupLayout.PREFERRED_SIZE)
.addGap(1)
.addComponent(closeChatButton, GroupLayout.PREFERRED_SIZE, 20, GroupLayout.PREFERRED_SIZE)
.addGap(1))
.addComponent(textArea, GroupLayout.DEFAULT_SIZE, 370, Short.MAX_VALUE)
);
gl_panel.setVerticalGroup(
gl_panel.createParallelGroup(Alignment.LEADING)
.addGroup(gl_panel.createSequentialGroup()
.addComponent(textArea, GroupLayout.PREFERRED_SIZE, 100, GroupLayout.PREFERRED_SIZE)
.addGroup(gl_panel.createParallelGroup(Alignment.LEADING)
.addGroup(gl_panel.createSequentialGroup()
.addGap(1)
.addGroup(gl_panel.createParallelGroup(Alignment.TRAILING, false)
.addGroup(gl_panel.createSequentialGroup()
.addComponent(sendTextButton, GroupLayout.PREFERRED_SIZE, 19, GroupLayout.PREFERRED_SIZE)
.addPreferredGap(ComponentPlacement.RELATED))
.addGroup(gl_panel.createSequentialGroup()
.addComponent(closeChatButton, GroupLayout.PREFERRED_SIZE, 19, GroupLayout.PREFERRED_SIZE)
.addPreferredGap(ComponentPlacement.RELATED)))
.addGap(0, 0, Short.MAX_VALUE))
.addGroup(gl_panel.createSequentialGroup()
.addGap(1)
.addComponent(textField, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
.addGap(1))))
);
this.setLayout(gl_panel);
}
}
and this is how I add these chatPanel(s) to chatTabbedPane
chatPanel newChat = new chatPanel(chatRef);
chatTabbedPane.addTab("title", null, newChat, null);
I want to shrink the size of chatPanel with shrink in size of application frame.
I've just run into this issue as well, and an answer like "just use another layout manager" isn't an answer I'm willing to accept.
To answer your question, the line wrap is messing things up.
textArea.setLineWrap(true);
Comment that line out and test again to see it will resize down with the window/frame like expected.
However, the side effect is obviously no line wraps. What's not so obvious is when your text length becomes greater than the textarea's width, when you resize the window the textarea's width will lock to the text's length. I still haven't found a fix for this new problem yet.
I'm pretty new to swing and am having some troubles.
Heres my code. I'm getting the error
Exception in thread "main" java.lang.IllegalArgumentException: adding container's parent to itself
at java.awt.Container.checkAddToSelf(Container.java:472)
at java.awt.Container.addImpl(Container.java:1083)
at java.awt.Container.add(Container.java:410)
at UMSL.Eval.createUserInterface(Eval.java:126)
at UMSL.Eval.(Eval.java:95)
at UMSL.Eval.main(Eval.java:56)
Java Result: 1
Can anyone explain to me what this error message is trying to tell me and how I can make things work.
private void createUserInterface()
{
JPanel contentPane;
contentPane = new JPanel();
JPanel instructorPanel = new JPanel();
instructorPanel.setBounds(40, 20, 276, 48);
instructorPanel.setBorder (BorderFactory.createEtchedBorder() );
instructorPanel.setLayout( null) ;
instructorPanel = new JPanel();
contentPane.add(instructorPanel);
// set up Instructor Label
JLabel instructorLabel = new JLabel();
instructorLabel.setBounds (25, 15, 100, 20);
instructorLabel.setText("Instructor:");
instructorLabel.add (instructorLabel);
Like #Reimeus said, you're trying to add a label to itself with the line
instructorLabel.add (instructorLabel);
I'm assuming that you're trying to add the label to the instructor panel which would be
instructorPanel.add(instructorLabel);