I have written a little test applet and start the applet via Eclipse appletviewer.
I have noted tag at the begginig of the code, but appletviewer doesn't see it. It starts the applet in standart window with the same size every time.
I use JDK 1.7, Eclipse Kepler
import java.awt.Container;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JApplet;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
/*<applet code="TestApplet" width=200 height=40>
</applet>
*/
public class TestApplet extends JApplet{
private Container cp;
private JPanel mainPanel, testPanel1;
private JLabel testLabel1 = new JLabel("Test1"),
testLabel2 = new JLabel("Test2");
private JTextField testTextField1 = new JTextField(),
testTextField2 = new JTextField();
private JButton testButton1 = new JButton("TestButton1"),
testButton2 = new JButton("TestButton2");
public void init(){
cp = getContentPane();
testPanel1 = new JPanel(new GridLayout(3,1));
cp.add(testPanel1);
testPanel1.add(testLabel1);
testPanel1.add(testTextField1);
testPanel1.add(testButton1);
}
public void start(){
}
public void stop(){
}
public void destroy(){
}
}
How can I change size of the applet window when I start it using Eclipse?
You can change it in Run Configurations in Eclipse:
Run -> Run Configurations -> Java Applet -> New Configuration. the default size can be changed in the Parameters tab.
Note that this is only for testing and the actual applet your user sees is depends on how the applet is launched for them (Which is done typically via a <applet>, <object> or <embed> tags in a webpage. all of these tags support size attributes.)
Related
I am trying to create an application with several tabbed panes, and to keep the code manageable, I wanted to have the content for these panes in separate classes, in separate .java files.
I have 3 files currently
(i) TestLayout.java
package testlayout;
public class TestLayout
{
public static void main(String[] args)
{
MainFrame mainFrameObject = new MainFrame();
mainFrameObject.displayMainFrame();
}
}
(ii) MainFrame.java
package testlayout;
import java.awt.Color;
import java.awt.Component;
import java.awt.GridLayout;
import javax.swing.BorderFactory;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTabbedPane;
import javax.swing.SwingConstants;
public class MainFrame
{
JFrame masterFrame = new JFrame("JAVA 1.1");
JTabbedPane tabbedPane = new JTabbedPane();
public void displayMainFrame()
{
masterFrame.setSize(1000, 600);
masterFrame.setVisible(true);
masterFrame.setResizable(false);
masterFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
masterFrame.add(tabbedPane);
DisplayReadMe drmObj = new DisplayReadMe();
drmObj.showReadMe();
//showReadMe();
}
/*
public void showReadMe()
{
JPanel panelReadMe = new JPanel(new GridLayout(10,1,8,8));
panelReadMe.setBorder(BorderFactory.createEmptyBorder(4, 4, 4, 4));
tabbedPane.addTab("Read Me", null, panelReadMe, "First Tab");
String testreadMeTestMessage = "This is a test message";
JLabel testreadMeLabel = new JLabel(testreadMeTestMessage, SwingConstants.LEFT);
testreadMeLabel.setBorder(BorderFactory.createLineBorder(Color.orange,3));
panelReadMe.add(testreadMeLabel);
}
*/
}
and
(iii) DisplayReadMe.java
package testlayout;
import java.awt.Color;
import java.awt.GridLayout;
import javax.swing.BorderFactory;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.SwingConstants;
public class DisplayReadMe extends MainFrame
{
public DisplayReadMe()
{
}
public void showReadMe()
{
System.out.println("method showReadMe begins");
JPanel panelReadMe = new JPanel(new GridLayout(10,1,8,8));
panelReadMe.setBorder(BorderFactory.createEmptyBorder(4, 4, 4, 4));
tabbedPane.addTab("Read Me", null, panelReadMe, "First Tab");
String testreadMeTestMessage = "This is a test message";
JLabel testreadMeLabel = new JLabel(testreadMeTestMessage, SwingConstants.LEFT);
testreadMeLabel.setBorder(BorderFactory.createLineBorder(Color.orange,3));
panelReadMe.add(testreadMeLabel);
System.out.println("method showReadMe ends");
}
}
My query is, when I uncomment the //showReadMe(); and showReadMe method in MainFrame, it works. The tab is added to the JFrame and the test message shows in the box.
But should the
DisplayReadMe drmObj = new DisplayReadMe();
drmObj.showReadMe();
code, not do the same thing? Am I not calling the showReadMe method from the DisplayReadMe class, akin to showReadMe().
I have tried revalidate, repaint and threading and nothing seems to call the method and show the tab and message ?
Any guidance would be gratefully appreciated
Many Thanks
PG
The method is actually working, but the tabbedPane instance in drmObj is different with respect to the JTabbedPane class member in MainFrame. Try to add tabbedPane as a parameter in showReadMe() and refer to that instance whenever adding elements. It should work.
public void showReadMe(JTabbedPane tabbedPane);
...
drmObj.showReadMe(this.tabbedPane);
Hope it helps!
you may not have duplicated method with same parameters, check if you have another showReadMe method who expects nothing as parameter.
if you make an overwrite of the showReadMe, remember that it will make showReadMe of the main class, then will make showReadMe inherited since it cannot go more up.
i donno if i explained it well at all.
I have a closed source third party application whose windows I am trying to control from within my java program. I manage to run the main method of the third-party application and intercept the window events it generates using an instance of AWTEventListener. Then I iterate through the components of the windows it generates in order to find and manipulate the necessary swing controls. Finding the components, clicking on buttons, activating toggle buttons, and modifying text fields works fine but there is a hyperlink inside a JTextPane that I have not been able to trigger programmatically, nor have I found any information online on how to do it successfully. The suggestions here and here looked promising but I was not able to trigger the hyperlink with the MouseEvent. I should also point out that the hyperlink is not to a URL but to an internal function. When I invoke the JTextPane getText() method, I get:
<html>
<head>
</head>
<body>
Expand Window
</body>
</html>
My question: Is there is a way to directly reach the target of a hyperlink inside a JTextPane (in this case, to expand the window) as opposed to trying to have a MouseEvent simulate a click on it?
I don't know but probably this example will help you.
import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.util.Collection;
import java.util.LinkedHashSet;
import javax.swing.AbstractAction;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTextPane;
import javax.swing.WindowConstants;
import javax.swing.text.AttributeSet;
import javax.swing.text.Element;
import javax.swing.text.html.HTML;
import javax.swing.text.html.HTMLDocument;
public class TextPaneTest {
public static void main(String[] args) {
final JFrame frm = new JFrame("Editor pane test");
final JTextPane pane = new JTextPane();
pane.setContentType("text/html");
pane.setText("<html>Here is the text with a link</html>");
frm.add(new JScrollPane(pane));
final JButton btn = new JButton(new AbstractAction("Find link") {
#Override
public void actionPerformed(ActionEvent e) {
final HTMLDocument doc = (HTMLDocument) pane.getDocument();
final Collection<String> links = new LinkedHashSet<String>();
// probably exists a better way to iterate over elements
for (int i = 0; i < doc.getLength(); i++) {
final Element el = doc.getCharacterElement(i);
final AttributeSet a = el.getAttributes();
final AttributeSet anchor = (AttributeSet)a.getAttribute(HTML.Tag.A);
if (anchor != null) {
links.add((String)anchor.getAttribute(HTML.Attribute.HREF));
}
}
System.out.println("Links found: " + links);
}
});
frm.add(btn, BorderLayout.EAST);
frm.pack();
frm.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
frm.setVisible(true);
}
}
In my Java applet I have JDialog with couple of radioButtons.
The problem is that this applet is of dimensions 700x300 and the JDialog appears in the center of screen. I want this JDialog to appear in the center of JApplet layout. My constructor invocation looks like this:
final JDialog dialog = new JDialog(
SwingUtilities.windowForComponent(GUIComponentContainer.getInstance().getDocumentTable()),
I18nCommonsImpl.constants.selectCertificate(), ModalityType.APPLICATION_MODAL);
This method:
GUIComponentContainer.getInstance().getDocumentTable()
returns JTable component which is a child of my JApplet.
I also used JDialog "setLocationRelativeTo" method:
dialog.setLocationRelativeTo(GUIComponentContainer.getInstance().getApplet());
None of these seem to work. Is there any other way to accomplish my goal?
I searched along SO for similar questions, but didn't see any working solutions.
Thanks in advance.
Getting the Window for the applet works for me, at least when the applet is launched in Eclipse. For example:
import java.awt.Dimension;
import java.awt.Window;
import java.awt.Dialog.ModalityType;
import java.awt.event.ActionEvent;
import javax.swing.AbstractAction;
import javax.swing.Box;
import javax.swing.JApplet;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
#SuppressWarnings("serial")
public class AppletCentering extends JApplet {
#Override
public void init() {
final JPanel panel = new JPanel();
panel.add(new JButton(new AbstractAction("Press Me") {
#Override
public void actionPerformed(ActionEvent e) {
Window win = SwingUtilities.getWindowAncestor(panel);
System.out.println("win class: " + win.getClass().getCanonicalName());
JDialog dialog = new JDialog(win, "My Dialog", ModalityType.APPLICATION_MODAL);
dialog.add(Box.createRigidArea(new Dimension(200, 200)));
dialog.pack();
dialog.setLocationRelativeTo(win);
dialog.setVisible(true);
}
}));
add(panel);
}
}
But having said this, your question begs the question: why are you even coding for an applet? These have been out of favor for years, and just recently has lost support from Oracle who has decided to finally drop applet browser plug in support.
I have this project and it allows users to kind of create there own projects within it and save it off and do much more. Im doing this all in Java using the program Eclipse. Today I mainly wanted to know how would i go about displaying a Hierarchy? Ill be a little more specific, when the user creates a project it ask them where they want to have there project folder. Lets say they choose a folder name JavaProjects and its in there Desktop (I use windows so excuse me if it isnt the same on Mac and Linux) and within that folder they have a Scripts folder and an Art Folder and within there art folder they have a Texture folder and a Logo Folder (Im also coming up with these folders in my head as i make this) How can i have it where in my JPanelEast it display a format kind of like the Package Explorer In Eclipse? Would I be able to just scan the folder they put in and have all the folders and files neatly laid out like that? Or would i have to do something much more out of my knowledge?
If it helps here is my code, minus a menu bar and action listeners
package Engine;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.BorderFactory;
import javax.swing.ButtonGroup;
import javax.swing.JButton;
import javax.swing.JCheckBoxMenuItem;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JPanel;
import javax.swing.JRadioButtonMenuItem;
import javax.swing.SwingConstants;
#SuppressWarnings("serial")
public class TestProjectBuilder extends JFrame {
JPanel jPanelNorth = new JPanel();
JPanel jPanelSouth = new JPanel();
JPanel jPanelEast = new JPanel();
JPanel jPanelCenter = new JPanel();
JButton jButtonDebug = new JButton("Debug");
JButton jButtonPause = new JButton("Pause");
JButton jButtonRun = new JButton("Run");
JLabel jLabelHeir = new JLabel("");
GridLayout gridLayout1 = new GridLayout(4,1);
public TestProjectBuilder() {
setTitle("Test Project Builder");
setSize(1400, 800);
jPanelNorth.setBackground(Color.DARK_GRAY);
jPanelNorth.setBorder(BorderFactory.createRaisedBevelBorder());
jPanelNorth.setPreferredSize(new Dimension(14, 40));
jPanelNorth.setToolTipText("North Panel");
jPanelNorth.add(jButtonDebug);
jButtonDebug.setHorizontalAlignment(SwingConstants.CENTER);
jPanelNorth.add(jButtonPause);
jButtonPause.setHorizontalAlignment(SwingConstants.CENTER);
jPanelNorth.add(jButtonRun);
jButtonRun.setHorizontalAlignment(SwingConstants.CENTER);
jPanelSouth.setBackground(Color.DARK_GRAY);
jPanelSouth.setBorder(BorderFactory.createTitledBorder(""));
jPanelSouth.setPreferredSize(new Dimension(10,200));
jPanelSouth.setToolTipText("South Panel");
jPanelEast.setBackground(Color.DARK_GRAY);
jPanelEast.setBorder(BorderFactory.createEtchedBorder());
jPanelEast.setPreferredSize(new Dimension(300,10));
jPanelEast.setToolTipText("East Panel");
jPanelCenter.setBackground(Color.GRAY);
jPanelCenter.setBorder(BorderFactory.createEtchedBorder());
jPanelCenter.setPreferredSize(new Dimension(56,10));
jPanelCenter.setToolTipText("Center Panel");
this.getContentPane().add(jPanelNorth, BorderLayout.NORTH);
this.getContentPane().add(jPanelSouth, BorderLayout.SOUTH);
this.getContentPane().add(jPanelEast, BorderLayout.EAST);
this.getContentPane().add(jPanelCenter, BorderLayout.CENTER);
jPanelCenter.setLayout(gridLayout1);
}
public static void main(String[] args) {
TestProjectBuilder tpb = new TestProjectBuilder();
tpb.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
tpb.setVisible(true);
}
}
Thanks for all Help in advance
You might start with the code for File Browser GUI.
The Java Tutorial has a section on using TreeViews for this.
http://docs.oracle.com/javase/tutorial/uiswing/components/tree.html
I post the link here, because this a bit to complicated for a simple answer.
Why is it that my code is not showing the image that I inserted? there's no compilation error or Syntax error but why is it like that?
import java.awt.FlowLayout;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.SwingConstants;
public class FirstUI extends JFrame{
private JLabel firstlabel;
private JLabel secondLabel;
private JLabel pie;
public FirstUI(){
super("Tittle");
setLayout(new FlowLayout());
firstlabel = new JLabel("Hello World");
firstlabel.setToolTipText("Hello World");
String path = "pie.png";
Icon pie = new ImageIcon(path);
secondLabel = new JLabel("Text with icon",pie,SwingConstants.LEFT);
add(secondLabel);
add(firstlabel);
}
}
main class
import javax.swing.JFrame;
public class FirstUiTest {
public static void main(String[] args){
FirstUI MyUI = new FirstUI();
MyUI.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
MyUI.setSize(320,280);
MyUI.setVisible(true);
}
}
if the "pie.png" is in the same Path of FirstUI.class try to use:
Icon pie = new ImageIcon(ImageIO.read( FirstUI.class.getResourceAsStream( "pie.png" ) ) );
I tried this exact code, and it worked. It looks like pie.png cannot be found. If you're using eclipse, put it in the project root (The same folder that has /bin and /src). Otherwise, put it in the same directory where you run the java command from.