I want to add a button to a RCP client. The window needs to display my barChart as well as 3 buttons.
When i add the line:
panel.add(button);
it returns an error:
The method add(Component) in the type Container is not applicable for the arguments (Button)
Please help :)
#Override
protected void createWindows(final Shell shell) throws Exception {
shell.setLayout(new FillLayout());
final Composite composite = new Composite(shell, SWT.EMBEDDED);
final Frame frame = SWT_AWT.new_Frame(composite);
final StaticBarSketch barGraph = new StaticBarSketch();
final Button button = new Button(composite, SWT.PUSH);
button.setText("Press");
Panel panel = new Panel();
panel.add(barGraph);
frame.add(panel);
barGraph.init();
composite.addListener(SWT.Resize, new Listener() {
#Override
public void handleEvent(Event event) {
barGraph.resized(composite.getSize().x, composite.getSize().y);
}
});
Instead of using a Panel, use Composite. Panel is from Swing and you are mixing Swing with RCP/SWT, which is not wise.
The Button that you are using is from SWT and you are adding that to Panel which is a Swing component and you can only a Swing component to Panel. You can either change the Button to AWT's Button or Swings JButton. Otherwise as stated earlier, change the Panel to Composite.
Related
Hi I am new to eclipse and I wanted to add a Swing component like JCombobox to my existing code in SWT. Is there any ways to do it through available API's in SWT or Swing?
I have used SWT_AWT.new_Frame(composite) API which was suggested. Here is my code.
public static void main(String[] args) {
final Display display = new Display();
final Shell shell = new Shell(display);
shell.setLayout(new FillLayout());
Composite composite = new Composite(shell, SWT.NO_BACKGROUND);
Frame myframe = SWT_AWT.new_Frame(composite);
Panel mypanel = new Panel(new BorderLayout()) {
#Override
public void update(java.awt.Graphics g) {
paint(g);
}
};
myframe.add(mypanel);
JRootPane root = new JRootPane();
mypanel.add(root);
java.awt.Container contentPane = root.getContentPane();
String languages[]={"C","C++","C#","Java","PHP"};
final JComboBox cb=new JComboBox(languages);
JScrollPane scrollPane = new JScrollPane(cb);
contentPane.setLayout(new BorderLayout());
contentPane.add(scrollPane);
shell.open();
while(!shell.isDisposed()) {
if (!display.readAndDispatch()) display.sleep();
}
display.dispose();
}
I get below exception.
Exception in thread "main" java.lang.IllegalArgumentException: Argument not valid
at org.eclipse.swt.SWT.error(SWT.java:4533)
at org.eclipse.swt.SWT.error(SWT.java:4467)
at org.eclipse.swt.SWT.error(SWT.java:4438)
at org.eclipse.swt.awt.SWT_AWT.new_Frame(SWT_AWT.java:129)
You have used the proper API actually. But you missed to add the feature like Embedding the AWT widgets into SWT while creating the Composite. SWT.EMBEDDED
Composite composite = new Composite(shell, SWT.NO_BACKGROUND | SWT.EMBEDDED);
Frame frame = SWT_AWT.new_Frame(composite);
Please go through Eclipse help this link for more on this API usage.
I am a beginner in coding and I am learning Java. I'm busy making a log in system, and I have made a JFrame, but when I add a JButton, it takes up the whole JFrame.
public class LogInSystem extends Application{
#Override
public void start(Stage primaryStage)
{
// Setting the JFrame
JFrame frame = new JFrame("Log in System");
frame.setSize(2000, 2000);
frame.setVisible(true);
// Setting the button
JPanel panel = new JPanel();
Button btn1 = new Button();
btn1.setText("1");
btn1.setBounds(50, 150, 100, 30);
frame.add(btn1);
frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
}
It seems like you declare a panel and not adding button into it as well as not adding the panel to the frame. You can try to add your button into panel and panel into frame,
panel.add(btn1);
frame.add(panel);
You can also use some useful layout for a particular panel. For example, BoxLayout, GridLayout and etc. By default, everything is set to be FlowLayout.
Hi guys I have a question about Applets. I have an game applet that I would like to embed in a webpage. However I would like to add a "Start Screen" to the applet which comes up first and has a few parameter buttons and a start button. The "Game Screen" should load when the start button is pressed. What would be the best way to go about implementing this? Here is a simple 1-screen Applet as an example.
public class AppletExample extends Applet implements ActionListener{
Button okButton;
Button cancelButton;
TextField _textField;
public void init(){
okButton = new Button("Press");
cancelButton = new Button("Cancel");
_textField = new TextField("Ready", 10);
okButton.addActionListener(this);
cancelButton.addActionListener(this);
add(okButton);
add(_textField);
add(cancelButton);
}
public void actionPerformed(ActionEvent arg0) {
if(arg0.getSource() == okButton){
_textField.setText("Running...");
}
else { _textField.setText("Cancelled");
}
}
}
You could use CardLayout to manage navigation between panels.
Have a look also at using the lightweight Swing JApplet rather the old AWT applet. The start panel could be a JPanel containing the necessary components. Use next, previous or show as appropriate to navigate between game panels.
public void init() {
setLayout(new CardLayout());
JPanel startPanel = new JPanel();
okButton = new JButton("Press");
startPanel.add(okButton);
...
add(startPanel, "Card 1");
...
}
I have four composites aligned in order.
Each composite has a check box, Label, and 2 Buttons. Now these composites are aligned one after another.
I want to enable focus on these items, i.e. when I use tab to go from one composite to other, the current composite should look highlighted. Ideally I want it to behave like a list, when you choose an item then that gets highlighted. Is this possible?
I understand that composite acts as a container for others widgets, control. My requirement is that I have a list of 5 entries, and that each item in the list has a check box, label, and two Buttons. I would also want it to be focused on when they are selected.
Also please let me know alternative solutions for the same UI that I have described above.
To make tab go from composite to composite, set the tab list for each composite to be one control that you want to have focus after a tab. For example, the check box:
composite.setTabList(new Control[]{checkButton});
To make the highlight, your imagination is the limit. You can change background, add some border, you name it. You just have to update it whenever one of the controls from the composite get focus.
Here is a full example:
public static void main(String[] args) {
Display display = new Display();
Shell shell = new Shell(display);
shell.setLayout(new FillLayout(SWT.VERTICAL));
createElement(shell);
createElement(shell);
createElement(shell);
createElement(shell);
shell.pack();
shell.open();
while (!shell.isDisposed()) {
if (!display.readAndDispatch()) {
display.sleep();
}
}
display.dispose();
}
private static void createElement(final Composite parent) {
final Composite composite = new Composite(parent, SWT.BORDER);
composite.setLayout(new GridLayout(4, false));
final Button checkButton = new Button(composite, SWT.CHECK);
new Label(composite, SWT.NONE);
final Button button1 = new Button(composite, SWT.PUSH);
final Button button2 = new Button(composite, SWT.PUSH);
Listener listener = new Listener() {
#Override
public void handleEvent(Event event) {
for (Control control : parent.getChildren()) {
control.setBackground(null);
}
composite.setBackground(composite.getDisplay().getSystemColor(SWT.COLOR_RED));
if (event.widget == button1 || event.widget == button2) {
checkButton.setFocus();
}
}
};
checkButton.addListener(SWT.FocusIn, listener);
button1.addListener(SWT.FocusIn, listener);
button2.addListener(SWT.FocusIn, listener);
composite.setTabList(new Control[]{checkButton});
}
how can i add components dynamically in a jpanel?
I am having add button when i click the button the components should be added to the JPanel.
my question is that adding a textfield and button to jpanel when i click on the add button the user can click on the add button any number of times according to that i have to add them to the jpanel. i have added to scrollerpane to my jpanel,and jpanel layout manager is set to null.
Just as you always do, except that you have to call:
panel.revalidate();
when you are done, since the container is already realized.
Use an ActionListener, you can use an anonymous class like this:
JPanel myJPanel = new JPanel();
...
b = new Button("Add Component");
b.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
JLabel someLabel = new JLabel("Some new Label");
myJPanel.add(someLabel);
myJPanel.revalidate();
}
});