How to create a click event search button in Eclipse using? - eclipse

How to create a click event search button in Eclipse?? Can someone help me. This is the code im working with.
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_search);
listContent1 = (ListView)findViewById(R.id.lFoodlist1);
mySQLiteAdapter = new SQLiteAdapter(this);
mySQLiteAdapter.openToRead();
Cursor cursor = mySQLiteAdapter.queueAll();
startManagingCursor(cursor);
String[] from = new String[]{SQLiteAdapter.KEY_FOODNAME,SQLiteAdapter.KEY_CALORIES};
int[] to = new int[]{R.id.tv1, R.id.tv2};
SimpleCursorAdapter cursorAdapter =
new SimpleCursorAdapter(this, R.layout.row, cursor, from, to);
listContent1.setAdapter(cursorAdapter);
//listContent.setOnItemClickListener(listContentOnItemClickListener);*/
}

This code was taken from Here and was modified/commented to help the poseter with his scenario.
You can implement swing to help with the GUI aspect of your application. Others may prefer AWT
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
public class myTest {
public static void main(String[] args) {
//This will create the JFrame/JPanel/JButton
final JFrame frame = new JFrame();
JPanel panel = new JPanel();
JButton button1 = new JButton();
//Add your panel and button and make them visable
frame.add(panel);
panel.add(button1);
frame.setVisible(true);
//This adds the actionListener to the Button
button1.addActionListener(new ActionListener() {
//When the button is clicked this action will be performed
public void actionPerformed(ActionEvent arg0) {
//Add your code here.
}
});
}
}

Related

ControlsFX HiddenSidesPane hides When clicked

I am using ControlsFX - HiddenSidesPane where i add some link(ToggleButtons) to be clicked for navigation.
The problem i have is whenever anything is clicked, the HiddenSiddesPane hides.
The desired behavior is when anything inside it is clicked it should not close/hide, unless cursor hovers out of it.
SSCCE to demonstrate undesired behavior
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.ListView;
import javafx.scene.control.TableView;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;
import org.controlsfx.control.HiddenSidesPane;
public class MyHiddenSidesPaneDemo extends Application{
public static void main(String[] args) { Application.launch(args); }
#Override
public void start(Stage primaryStage) throws Exception {
VBox root = new VBox();
TableView tv = new TableView();
HiddenSidesPane hiddenSidesPane = new HiddenSidesPane();
hiddenSidesPane.setContent(tv);
hiddenSidesPane.setLeft(new ListView());
root.getChildren().addAll(hiddenSidesPane);
primaryStage.setTitle("HiddenSidesPane Example Demo");
Scene scene = new Scene(root);
primaryStage.setScene(scene);
primaryStage.show();
}
}
Try the following:
ListView listView = new ListView();
hiddenSidesPane.setLeft(listView);
listView.setOnMouseEntered(e->hiddenSidesPane.setPinnedSide(Side.LEFT)); //Keep left side pinned
listView.setOnMouseExited(e->hiddenSidesPane.setPinnedSide(null)); //unpin when mouse exits

Multiple RadioButtons Simulator

I'm trying to make a sort of simple soccer simulator. This is the code I created after watching tutorials and i know its pretty bad. All i want to do is add a value to the team, like 1 for the best team and 10 for the worst, and when i click simulate a pop up would show up telling me which team would win given the teams value. But i cant figure out how to do it.
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.border.TitledBorder;
public class sim extends JPanel {
public sim() {
// JFrame constructor
super(true);
JRadioButton chelsea, arsenal, chelsea2, arsenal2;
this.setLayout(new GridLayout(3,0));
ButtonGroup group = new ButtonGroup();
ButtonGroup group2 = new ButtonGroup();
// takes image and saves it the the variable
Icon a = new ImageIcon(getClass().getResource("a.PNG"));
Icon c = new ImageIcon(getClass().getResource("c.JPG"));
chelsea = new JRadioButton("Chelsea",c);
chelsea.setHorizontalTextPosition(AbstractButton.CENTER);
chelsea.setVerticalTextPosition(AbstractButton.BOTTOM);
arsenal = new JRadioButton("Arsenal",a);
arsenal.setHorizontalTextPosition(AbstractButton.CENTER);
arsenal.setVerticalTextPosition(AbstractButton.BOTTOM);
group.add(chelsea);
group.add(arsenal);
JLabel label = new JLabel("");
TitledBorder titled = new TitledBorder("Team 1");
label.setBorder(titled);
chelsea.setBorder(titled);
arsenal.setBorder(titled);
JButton button = new JButton("Simulate");
button.setHorizontalAlignment(JButton.CENTER);
add(button, BorderLayout.CENTER);
chelsea2 = new JRadioButton("Chelsea",c);
chelsea2.setHorizontalTextPosition(AbstractButton.CENTER);
chelsea2.setVerticalTextPosition(AbstractButton.BOTTOM);
arsenal2 = new JRadioButton("Arsenal",a);
arsenal2.setHorizontalTextPosition(AbstractButton.CENTER);
arsenal2.setVerticalTextPosition(AbstractButton.BOTTOM);
group2.add(chelsea2);
group2.add(arsenal2);
JLabel label2 = new JLabel("");
TitledBorder titled2 = new TitledBorder("Team 2");
label2.setBorder(titled2);
chelsea2.setBorder(titled);
arsenal2.setBorder(titled);
add(label);
add(chelsea);
add(arsenal);
add(button);
add(chelsea2);
add(arsenal2);
HandlerClass handler = new HandlerClass();
chelsea.addActionListener(handler);
}
private class HandlerClass implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
//JOptionPane.showMessageDialog(null, String.format("%s", e.getActionCommand()));
}
}
public static void main(String[] args) {
JFrame frame = new JFrame("Final");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(1920, 1080);
frame.setContentPane(new sim());
frame.setVisible(true);
}
}
Put the ActionListener into the sim class.
public void actionPerformed(ActionEvent e)
{
JButton clicked = (JButton)e.getSource();
if(clicked == button)
{
JOptionPane.showMessageDialog(null, "this team will win");
}
}
You need to decrees the amount of code to just amount needed you dont need to show us all the raidio

How do I add a button and label to my frame?

Ok basically my problem is I don't know how to get my buttons and my labels to appear in the frame that I just created. I tried using frame.add(new JButton("VOTE1")); but that doesn't work and it says it's missing an identifier. Here is my code so far, your help is much appreciated.
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class VotingMachine extends JFrame implements ActionListener {
public static void main(String[] args) {
JFrame frame = new JFrame("Voting Machine");
frame.setSize(400, 300);
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
private JLabel Candidate1 = new JLabel("My Name");
private JLabel Candidate2 = new JLabel("Jennifer Lawrence");
private JLabel Candidate3 = new JLabel("Cristiano Ronaldo");
private JButton VOTE1 = new JButton("VOTE FOR Bishoy Morcos");
private JButton VOTE2 = new JButton("VOTE FOR Jennifer Lawrence");
private JButton VOTE3 = new JButton("VOTE FOR Cristiano Ronaldo");
int countcandidate1;
int countcandidate2;
int countcandidate3;
public void actionPerformed(ActionEvent e) {
Object o = e.getSource();
if(o == VOTE1)
countcandidate1++;
else if(o == VOTE2)
countcandidate2++;
else if(o == VOTE3)
countcandidate3++;
}
}
First you need to create a JPanel
JPanel welcomePanel = new JPanel();
Then add the buttons and labels to that panel
welcomePanel.add(Candidate1);
welcomePanel.add(VOTE1);
ETC.... for all of your buttons and labels
Lastly, add that JPanel to your JFrame
frame.add(welcomePanel);
If this answers your question, please mark it as answered so other viewers will be informed.

previous numbers entered being overwritten JFRAME

I am currently trying to create a simple access panel which allows the user to click on the numbers and it will appear in the textfield above but when the button is clicked the textfield is being populated but the previous number is being overwritten! Below shows the code I currently have. It's btn1 & btn2 I am focusing on at the minute:
package securitySystem;
import java.awt.*;
import javax.swing.*;
import java.sql.*;
import java.awt.event.*;
public class accessPanel extends JFrame {
public static void main (String args[]){
accessPanel gui= new accessPanel ();
gui.setSize (360, 400);
gui.setLocationRelativeTo(null);
gui.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
gui.setVisible(true);
gui.setTitle("Access Panel");
gui.setLayout(new BorderLayout());
gui.setBackground(Color.BLACK);
}
JButton btn1= new JButton("1");
JButton btn2= new JButton("2");
JButton btn3= new JButton("3");
JButton btn4= new JButton("4");
JButton btn5= new JButton("5");
JButton btn6= new JButton("6");
JButton btn7= new JButton("7");
JButton btn8= new JButton("8");
JButton btn9= new JButton("9");
JButton btn0= new JButton("0");
JTextField pin = new JTextField();
public accessPanel (){
setLayout(null);
pin.setBounds(0,0,340,40);
add(pin);
btn1.setBounds(0,40,100,70);
add(btn1);
btn2.setBounds(120,40,100,70);
add(btn2);
btn3.setBounds(240,40,100,70);
add(btn3);
btn4.setBounds(0,120,100,70);
add(btn4);
btn5.setBounds(120,120,100,70);
add(btn5);
btn6.setBounds(240,120,100,70);
add(btn6);
btn7.setBounds(0,200,100,70);
add(btn7);
btn8.setBounds(120,200,100,70);
add(btn8);
btn9.setBounds(240,200,100,70);
add(btn9);
btn0.setBounds(120,280,100,70);
add(btn0);
}
public void calcButtons()
{
btn1.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
pin.setText(btn1.getText());
}
});
btn2.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
pin.setText(btn2.getText());
}
});
}
}
Have a global String that you can add the numbers to every time a button is clicked
public class accessPanel extends JFrame {
String word = "";
...
btn2.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
word += btn2.getText();
pin.setText(word);
}
});
}
If you have a clear button, just set the string to "" in the its actionPerformed

windowbuilder tutorial not functioning

I am trying to do a tutorial that allows one to Add and Remove stocks and witness their price and change. This tutorial demonstrates how to use the GUI builder, GWT Designer, to create and design a Stock Watcher application based on the GWT tutorial.
http://code.google.com/webtoolkit/tools/gwtdesigner/tutorials/stockwatcher.html#design_ui
So far I have SW.java:
package edu.gatech.client;
import java.util.ArrayList;
import com.google.gwt.core.client.EntryPoint;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.event.dom.client.KeyCodes;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.RootPanel;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.google.gwt.user.client.ui.FlexTable;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.event.dom.client.KeyPressHandler;
import com.google.gwt.event.dom.client.KeyPressEvent;
/**
* Entry point classes define <code>onModuleLoad()</code>.
*/
public class SW implements EntryPoint {
private RootPanel rootPanel;
private FlexTable stocksFlexTable;
private HorizontalPanel addPanel;
private VerticalPanel mainWindow;
private TextBox newSymbolTextBox;
private Button addButton;
private Label lastUpdatedLabel;
private ArrayList <String> stocks = new ArrayList<String>(); //Add this line
public void onModuleLoad() {
rootPanel = RootPanel.get();
mainWindow = new VerticalPanel();
rootPanel.add(mainWindow, 10, 10);
mainWindow.setSize("267px", "175px");
FlexTable stocksFlexTable = new FlexTable();
//Add these lines
stocksFlexTable.setText(0, 0, "Symbol");
stocksFlexTable.setText(0, 1, "Price");
stocksFlexTable.setText(0, 2, "Change");
stocksFlexTable.setText(0, 3, "Remove");
mainWindow.add(stocksFlexTable);
addPanel = new HorizontalPanel();
rootPanel.add(addPanel, 10, 200);
addPanel.setSize("267px", "68px");
newSymbolTextBox = new TextBox();
newSymbolTextBox.addKeyPressHandler(new KeyPressHandler() {
public void onKeyPress(KeyPressEvent event) {
if (event.getCharCode() == KeyCodes.KEY_ENTER){
addStock();
}
}
});
addPanel.add(newSymbolTextBox);
newSymbolTextBox.setWidth("211px");
addButton = new Button("Add");
addButton.addClickHandler(new ClickHandler() {
public void onClick(ClickEvent event) {
addStock();
}
});
addPanel.add(addButton);
lastUpdatedLabel = new Label("New Label");
rootPanel.add(lastUpdatedLabel, 48, 274);
}
private void addStock() {
final String symbol = newSymbolTextBox.getText().toUpperCase().trim();
newSymbolTextBox.setFocus(true);
// Stock code must be between 1 and 10 chars that are numbers, letters, or dots.
if (!symbol.matches("^[0-9A-Z\\.]{1,10}$")) {
Window.alert("'" + symbol + "' is not a valid symbol.");
newSymbolTextBox.selectAll();
return;
}
newSymbolTextBox.setText("");
// don't add the stock if it's already in the watch list
if (stocks.contains(symbol))
return;
// add the stock to the list
int row = stocksFlexTable.getRowCount();
stocks.add(symbol);
stocksFlexTable.setText(row, 0, symbol);
// add button to remove this stock from the list
Button removeStock = new Button("x");
removeStock.addClickHandler(new ClickHandler() {
public void onClick(ClickEvent event) {
int removedIndex = stocks.indexOf(symbol);
stocks.remove(removedIndex);
stocksFlexTable.removeRow(removedIndex + 1);
}
});
stocksFlexTable.setWidget(row, 3, removeStock);
}
}
When I run the web application, I cannot Add a stock. The program does, however, distinguish between bad stock names and acceptable ones. Instead I get an "uncaught exception escaped" error and the program doesn't really do anything. How do I troubleshoot this?
Use the debugger and single step through the code. Set a breakpoint on addStock's first line and find which line crashes. Once you find which line you then instrument the line to find out what aspect is causing the problem - assuming you can't deduce the problem by looking at the line.