I'm having trouble embedding Swing components inside SWT (such as eclipse plugin..)
Currently what I have:
public void createPartControl(Composite parent) {
java.awt.Frame f = SWT_AWT.new_Frame(parent);
JPanel panel = new JPanel(new BorderLayout());
JButton button = new JButton("Swing button");
JLabel label = new JLabel("Swing label");
panel.add(label,BorderLayout.NORTH);
panel.add(button,BorderLayout.CENTER);
f.add(panel);
}
This code snippet fails to load, the plugin crashes on the first line...
Any idea how to incorporate these components?
Thanks!
http://www.eclipse.org/articles/article.php?file=Article-Swing-SWT-Integration/index.html
Minimally, embedding an AWT frame inside an SWT composite is just two simple lines of code
Composite composite = new Composite(parent, SWT.EMBEDDED | SWT.NO_BACKGROUND);
Frame frame = SWT_AWT.new_Frame(composite);
Since your code is failing at the first line then please first make sure that the parent Composite is created using SWT.EMBEDDED. If it is not then create a child composite using the SWT.EMBEDDED and then call
java.awt.Frame f = SWT_AWT.new_Frame(newChildComposite);
An instance of
org.eclipse.swt.Composite is created
with the SWT.EMBEDDED style. This
style signals that an AWT frame is to
be embedded inside the Composite. The
call to the static new_Frame method
creates and returns such a frame. The
frame may then be populated with AWT
and/or Swing components.
Taken from Article-Swing-SWT-Integration
Related
I'm trying out intellij, and am trying to do some swing development. I am running into an issue I have never experienced on eclipse, and I am wondering if I have something set up wrong.
Here is my GUI class that is run by my driver:
package view;
import javax.swing.*;
import java.awt.*;
public class View {
private JPanel panel;
public void run() {
JFrame frame = new JFrame("Vending Machine");
frame.setContentPane(new View().panel);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.pack();
frame.setVisible(true);
}
{
// GUI initializer generated by IntelliJ IDEA GUI Designer
// >>> IMPORTANT!! <<<
// DO NOT EDIT OR ADD ANY CODE HERE!
$$$setupUI$$$();
}
/**
* Method generated by IntelliJ IDEA GUI Designer
* >>> IMPORTANT!! <<<
* DO NOT edit this method OR call it in your code!
*
* #noinspection ALL
*/
private void $$$setupUI$$$() {
final JPanel panel1 = new JPanel();
panel1.setLayout(new GridBagLayout());
}
}
as far as I can tell, my run() method is as straightforward as it gets. However, upon compiling, this is the error I receive:
Exception in thread "main" java.awt.IllegalComponentStateException: contentPane cannot be set to null.
at javax.swing.JRootPane.setContentPane(JRootPane.java:621)
at javax.swing.JFrame.setContentPane(JFrame.java:698)
at view.View.run(View.java:13)
at model.VendingMachine.<init>(VendingMachine.java:14)
at controller.Driver.main(Driver.java:14)
For whatever reason, the intellij autocreated code that does not properly initialize the JPanel, which is why it's null.
I've tried instantiating it myself inside run (panel = new JPanel();) but that has not helped.
Is this something obvious? I've never run into this issue when getting started with swing in eclipse.
You are setting as JFrame' s content pane the panel 'panel' but the JPanel you are creating is called 'panel1'. To fix this problem change JPanel' s name to 'panel' instead of 'panel1'.
Try enabling "UI Designer" plugin in Intellij IDEA, it helped in my case.
File -> Settings -> Plugins -> UI Designer -> Restart IDE
You need to make sure to to set the field name of the panel. You are possibly misunderstanding the following line:
frame.setContentPane(new View().panel);
In this code, new View().panel is really trying to initialize the object with the field name. So if the panel doesn't have that name...obviously you are trying to instantiate something that doesn't exist.
I named my JPanel MainPanel under the field name property in the jform editor and wrote:
frame.setContentPane(new View().MainPanel);
I am trying to make a JTabbedPane in Java 7 on OSX that has tabs positioned to the left with their text horizontal (instead of vertical). However, with the code:
import javax.swing.*;
import java.awt.Dimension;
class Probs extends JDialog {
JTabbedPane options = new JTabbedPane();
Probs(JFrame owner) {
//main constructor
super(owner, "User Preferences", true);
//set the tabs to be left aligned
options.setTabPlacement(JTabbedPane.LEFT);
//construct the authorization panel
JPanel authorization = new JPanel();
authorization.add(new JLabel("test"));
options.addTab("test", authorization);
add(options);
setSize(new Dimension(300,300)); //should use pack here
setResizable(false);
setLocationRelativeTo(null);
setVisible(true);
}
public static void main(String[] args) {
JFrame test = new JFrame();
new Probs(test);
test.dispose();
}
}
I get a dialog box which looks like: this image
I would like the tab text to be horizontal (the 'test' title on the tab be oriented horizontally instead of vertically).
I searched around on Google for a while and have only run into occurrences wherein people wanted to achieve vertical text on their tabs, I could not manage to locate any in which people wanted to have horizontal text (what I am trying to achieve).
In particular, I am trying to achieve something which exactly looks like the image mentioned in the first post of this question. It is basically the exact opposite of that question because the person in that tab started with what I am trying to achieve (I believe). Basically, I am trying to determine how to create the image displayed in the first post of that question.
Can someone please tell me how to have left-oriented tabs while preserving horizontal tab titles (as opposed to vertical)?
Thank you for your time and assistance.
Again, since I can't replicate the problem, Try this suggestion:
JPanel authorization = new JPanel();
authorization.add(new JLabel("test"));
options.addTab("", authorization);
JLabel labTab2 = new JLabel("test"); // create a label
options.setTabComponentAt(0, labTab2); // set it to the component
The alignment is determined by your operating system. If you want to change the alignment of the tab text, you have to change the look and feel of your swing application. This worked for me. See here.
The system look and feel at MacOSX didn't support what you want in JTabbedPane. You must create a customized JComponent to do this or to set the look and feel of your application to cross platform (java metal) as stated before by #MonkeySupersonic.
I suggest the readings:
Apple Java Development Guide (section: User Interface Toolkits for Java) - https://developer.apple.com/library/content/documentation/Java/Conceptual/Java14Development/04-JavaUIToolkits/JavaUIToolkits.html
mac User Interface Guidelines - https://developer.apple.com/macos/human-interface-guidelines
This snippet code I got from https://stackoverflow.com/a/6868039/2240900
how to add the internal2 to desktoppane1 using a button placed somewhere in internal1.
In the ActionListener added to your button you can use code like the following to get a reference to the desktop pane:
Container container = SwingUtilities.getAncestorOfClass(JDesktopPane.class, (Component)event.getSource());
if (container != null)
{
JDesktopPane desktop = (JDesktopPane)container;
JInternalFrame frame = new JInternalFrame(...);
desktop.add( frame );
}
My question is how to add another JInternalFrame if the button reside in another JInternalFrame? ex: add internalX to desktoppane1 using a button placed somewhere in internal2/internal3/internalX, where each internal was created using a button inside internalX not using a menubar.
Any help will be appreciated. Thanks.
I accidentally find out that we can use a method of JInternalFrame that is getDesktopPane().
As mention in javadoc:
getDesktopPane
public JDesktopPane getDesktopPane()
Convenience method that searches the ancestor hierarchy for a JDesktop instance. If JInternalFrame finds none, the desktopIcon tree is searched.
Returns:
the JDesktopPane this internal frame belongs to, or null if none is found
So we can just use a command like:
JDesktopPane desktopPane = internalFrame.getDesktopPane();
desktopPane.add(internalX);
or if the class extends JInternalFrame simply use
JDesktopPane desktopPane = this.getDesktopPane();
desktoppane.add(internalX);
to get the JDesktopPane to add another JInternalFrame in a nested JInternalFrame.
Externalize the listener into it's own class, with proper parameters if needed. Then, you can instantiate this listener every time you create a new frame and apply it to its button.
I am trying to create a child frame to exist inside my applet and it should be bound to a JPanel. I found this and that on the internet but nothing that worked. I think something went wrong during the process and the darn thing is hidden or something. Can someone please give me some help on this issue.
My source code follows...
public class EnableFrame {
public void init() {
EnableFrame theframe = new EnableFrame();
theframe.setSize(550, 300);
theframe.setVisible(true);
}
public EnableFrame() {
JPanel containall = new JPanel();
JInternalFrame iframe = new JInternalFrame("New Frame",true,true);
iframe.setBounds(10,10,150,150);
iframe.getContentPane().add(containall);
iframe.show(true);
}
}
Thanks in advance
-Roland
A JInternal is normally associated with a JDesktopPane.
I order for the internal frame to appear on the screen, you must have added the frame to an appropriate container, such as a JDesktopPane
You may find How to Use Internal Frames of some use.
my view only the comment
even is possible there could be caused with some side_effect for mouse and focus event betweens heavyweight (J)Applet and lightweight JInternalFrames that complicated this idea, and heavyweight (J)Applet can jumping toFront()
you'd don't do that and to use JDesktopPane from JFrame rather than for (J)Applet
I have a fairly simple question. I have a JPanel on a JFrame. I have a JLabel on the JPanel. How, I wonder, do i FULLY REMOVE the JLabel from the JPanel during runtime?
ImageIcon image7= new ImageIcon("archmageanim.gif");
JLabel label7 = new JLabel("", image7, JLabel.CENTER);
p.add( label7, "0 , 6" ); //This coordinate has to do with a layout manager I'm using - it
//I'm using - it works fine.
I have looked for this solution...but everyone says "the easiest way" is to set setVisible(false)...but that doesn't truly remove the object -_-. How can I REMOVE it?
Can't you just use this to find the parent Container of the JLabel and then use the remove method?
Container parent = label7.getParent();
parent.remove(label7);
parent.validate();
parent.repaint();
That should remove the label altogether and then refresh the parent Container.
It's this.
jpanel.remove(label7);
jpanel.revalidate();
jpanel.repaint();
jpanel.remove(component);
This is all you need to call to remove a component.