I am making a project that fill a text with information entered previously by a user.
For that, I am using the library JFrame.
So I have the form in the left of the panel and I want to put the filled text in the right when I click on the submit button.
My problem is that I don't undertand how to put the information in the right place of the text.
And for the moment the text doesn't appear at all in the frame on the click of the submit.
Can someone help me ?
Thanks a lot !
Here is my complete code :
public class LemurienAgileForm extends MainApplication {
public LemurienAgileForm(){
leLemurienAgileWindow();
}
public static void leLemurienAgileWindow() {
JFrame f = new JFrame("Le lémurien agile");
//set size of frame
f.setSize(1200, 1000);
f.setBackground(Color.black);
//make sure it quits when x is clicked
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JLabel labelNameFP = new JLabel("Nom (f.pl) : ");
labelNameFP.setBounds(50, 50, 200, 30);
JTextField nameFP = new JTextField();
//set size of the text box
nameFP.setBounds(165, 50, 200, 30);
JLabel labelAdjectiveFP = new JLabel("Adj. (f.pl) : ");
labelAdjectiveFP.setBounds(50, 100, 200, 30);
JTextField adjectiveFP = new JTextField();
adjectiveFP.setBounds(165, 100, 200, 30);
JLabel labelNumber = new JLabel("Nombre : ");
labelNumber.setBounds(50, 150, 200, 30);
JTextField number = new JTextField();
number.setBounds(165, 150, 200, 30);
JButton submitButton = new JButton("Submit");
submitButton.setBounds(130, 720, 70, 50);
submitButton.setBackground(Color.black);
submitButton.setForeground(Color.white);
submitButton.setSize(150, 50);
submitButton.setFont(new Font("Times New Roman", Font.PLAIN, 25));
submitButton.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
JTextArea textArea = new JTextArea();
textArea.setText("Text " + labelAdjectiveMS.getText() + " Text");
JPanel panel = new JPanel();
panel.setBounds(300, 50, 400, 200);
panel.setBackground(Color.black);
panel.add(textArea);
}
});
//add elements to the frame
f.add(labelNameFP);
f.add(nameFP);
f.add(labelAdjectiveFP);
f.add(adjectiveFP);
f.add(labelNumber);
f.add(number);
f.add(submitButton);
f.setLayout(null);
f.setVisible(true);
}
}
Related
I'm trying to give a functions to an array of jbuttons with ActionListener
but when I pass 'i' as variable it gives erorr :
App.java:45: error: local variables referenced from an inner class must be final or effectively final
the error is on line 45, 58, and 71.
Here's the code :
import java.awt.event.*;
import javax.swing.*;
import java.awt.*;
public class App extends JFrame {
App(){
JFrame calcFrame = new JFrame();
calcFrame.setSize(320, 420);
calcFrame.setVisible(true);
calcFrame.setTitle("Calculator");
calcFrame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
calcFrame.setResizable(false);
calcFrame.getContentPane().setBackground(new Color(0x123456));
//add panel
JTextField textField = new JTextField();
textField.setBounds(35, 20, 200, 100);
textField.setBackground(new Color(0x000000));
calcFrame.add(textField);
JButton cbtn = new JButton(String.valueOf('c')); // clear button
cbtn.setBounds(240, 140, 50, 50);
calcFrame.add(cbtn);
JButton pbtn = new JButton(String.valueOf('^')); // power button
pbtn.setBounds(240, 200, 50, 50);
calcFrame.add(pbtn);
/*Add array of buttuns from 0 ... 9 */
JButton nums[] = new JButton[10];
calcFrame.getContentPane().setLayout(null);
// organzie using if statment
for(int i = 0; i < 4; i++)
{
nums[i] = new JButton(String.valueOf(i)); // organzie first line 0 ... 3
nums[i].setBounds(i * 60, 140, 50, 50);
nums[i].addActionListener(new ActionListener(){
//give buttons a function
#Override
public void actionPerformed(ActionEvent e) {
textField.setText(textField.getText().concat(String.valueOf(i)));
}
});
calcFrame.add(nums[i]);
}
for(int i = 4, j = 0; i < 8; i++, j++)//organzie second line 4 ... 7
{
nums[i] = new JButton(String.valueOf(i));
nums[i].setBounds(j * 60, 200, 50, 50);
nums[i].addActionListener(new ActionListener(){
//give buttons a function
#Override
public void actionPerformed(ActionEvent e) {
textField.setText(textField.getText().concat(String.valueOf(i)));
}
});
calcFrame.add(nums[i]);
}
for(int i = 8, j = 0; i < 10; i++, j++)//last numbers line 8 ... 9
{
nums[i] = new JButton(String.valueOf(i));
nums[i].setBounds(j * 60, 260, 50, 50);
nums[i].addActionListener(new ActionListener(){
//give buttons a function
#Override
public void actionPerformed(ActionEvent e) {
textField.setText(textField.getText().concat(String.valueOf(i)));
}
});
calcFrame.add(nums[i]);
}
//End of numbers 0 ... 9
JButton dotbtn = new JButton();
JButton eqlbtn = new JButton();
dotbtn = new JButton(String.valueOf('.'));// dot button for a value less than 1
dotbtn.setBounds(120, 260, 50, 50);
calcFrame.add(dotbtn);
eqlbtn = new JButton(String.valueOf('=')); //equal button
eqlbtn.setBounds(180, 260, 50, 50);;
calcFrame.add(eqlbtn);
String[] arthmetictools = {"+", "-", "x", "/"};
JButton[] artbtns = new JButton[4]; // array of arthmetic buttons
for (int i = 0; i <= 4; i++) {
artbtns[i] = new JButton(String.valueOf(arthmetictools[i]));
artbtns[i].setBounds( 60 * i, 320, 50, 50);
calcFrame.add(artbtns[i]);
}
}
public static void main(String[] args) {
System.out.println("Hello, World!");
/* make the frame call it calc frame */
App calc = new App();
}
}
Thank you.
Tried to set text field to string of i from for loop.
I have a loop that adds a grid to a StackLayout for each patient (about 70 records). The grid has a column for a picture (the source of which is a url) and a column for the name. I thought it was the pictures that were causing the out of memory condition, but after hours of stepping through the code, I stumbled on what seems to be the culprit. If I wrap the grids in a frame, that seems to be an issue. I am using Xamarin 4.2.1.62.
I really do want a frame around each patient so I can have a border around each one. Anyone have any ideas?
For a reproducible example, create a page with a single StackLayout
<StackLayout VerticalOptions="FillAndExpand" x:Name="StackPatientList"></StackLayout>
Then, right after your InitializeComponent() call in the page, call this function:
private void InitPatients()
{
ObservableCollection<Patient> PatientsList = new ObservableCollection<Patient>();
for (int i = 0; i < 100; i++)
{
PatientsList.Add(new Patient() { FirstName = "First " + i.ToString(), LastName = "Last " + i.ToString() });
}
StackLayout oStack = new StackLayout() { };
this.StackPatientList.Children.Clear();
foreach (Patient oPat in PatientsList)
{
Grid oGrid = new Grid();
oGrid.RowDefinitions.Add(new RowDefinition { Height = new GridLength(100, GridUnitType.Auto) });
oGrid.RowDefinitions.Add(new RowDefinition { Height = new GridLength(1, GridUnitType.Auto) });
oGrid.ColumnDefinitions.Add(new ColumnDefinition { Width = new GridLength(100, GridUnitType.Absolute) });
oGrid.ColumnDefinitions.Add(new ColumnDefinition { Width = new GridLength(1, GridUnitType.Star) });
Image oPatImage = new Image() { Source = oPat.Picture, WidthRequest = 100, HeightRequest = 100 };
Label oPatLabel = new Label() { Text = oPat.FullName, FontSize = 15, VerticalTextAlignment = TextAlignment.Center };
oGrid.Children.Add(oPatLabel, 0, 0);
oGrid.Children.Add(oPatImage, 1, 0);
Frame oFrame = new Frame() { OutlineColor = Color.Black, Padding = 5, VerticalOptions = LayoutOptions.FillAndExpand };
oFrame.Content = oGrid;
//this.StackPatientList.Children.Add(oGrid); // Using this works fine
this.StackPatientList.Children.Add(oFrame); // This causes an out of memory error
}
}
Using this as your Patient class:
public class Patient
{
public string LastName { get; set; }
public string FirstName { get; set; }
public string FullName { get { return LastName + ", " + FirstName; } }
public string Picture { get; set; }
}
Run it on an android device (I use an LG-G3) and you will get an out of memory error. Then comment out the line that adds the frame and un-comment the line that just adds the grid and it will work fine.
I've just begun coding in Java and was wondering how to achieve this. What I want is for the user to be able to select what sub topic they would like to study, and then a new panel with text opens as the sub-topic is clicked. I've been trying everything i know but nothing seems to work. Any help would be appreciated. Thanks.
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
public class FinalPanel extends JPanel {
private JLabel jcomp1;
private JComboBox jcomp2;
private JComboBox jcomp3;
private JComboBox jcomp4;
private JComboBox jcomp5;
private JLabel jcomp6;
public FinalPanel() {
//construct preComponents
String[] jcomp2Items = {"Introduction", "Summary ", "Terms"};
String[] jcomp3Items = {"Review Test"};
String[] jcomp4Items = {"Conversion Factors", "Calculations", "Problems a"};
String[] jcomp5Items = {"Limiting ", "Problems 1", "Percent Yield", "Problems 2",
"Energy Changes", "Problems 3", "Heat of Reaction", "Problems 4"};
//construct components
jcomp1 = new JLabel ("Welcome to the LifeSaving Chemistry Study guide!");
jcomp2 = new JComboBox (jcomp2Items);
jcomp3 = new JComboBox (jcomp3Items);
jcomp4 = new JComboBox (jcomp4Items);
jcomp5 = new JComboBox (jcomp5Items);
jcomp6 = new JLabel ("Stoichiometry");
//adjust size and set layout
setPreferredSize (new Dimension (644, 332));
setLayout (null);
//add components
add (jcomp1);
add (jcomp2);
add (jcomp3);
add (jcomp4);
add (jcomp5);
add (jcomp6);
//set component bounds (only needed by Absolute Positioning)
jcomp1.setBounds (155, 20, 370, 20);
jcomp2.setBounds (90, 140, 175, 25);
jcomp3.setBounds (370, 205, 175, 30);
jcomp4.setBounds (90, 210, 175, 25);
jcomp5.setBounds (370, 140, 175, 25);
jcomp6.setBounds (265, 55, 100, 25);
}
public static void main (String[] args) {
JFrame frame = new JFrame ("MyPanel");
frame.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);
frame.getContentPane().add (new FinalPanel());
frame.pack();
frame.setVisible (true);
}
public void actionPerformed (ActionEvent e){
JFrame frame2 = new JFrame ("Clicked");
frame2.setVisible(true);
frame2.setSize(500,600);
JLabel label= new JLabel("Summary");
JLabel panel= new JLabel();
frame2.add(panel);
panel.add(label);
}
}
I am using a thread to draw some animations, so I need to repaint the label for every frame. To do this without flickering I am updating the label with my backbuffer graphics object (using the lbl.update(bufferedGraphics); method), but when I do this, the label gets repainted in the top left of the Graphics object, and not where setLocation has specified.
How do I specify the location of the label within the graphics, instead of within the panel that owns the label?
Here is an SSCCE:
import javax.swing.*;
import java.awt.*;
public class LabelSSCCE extends JApplet implements Runnable {
JPanel pnl;
JLabel lbl;
Image buffer;
Graphics bufferedGraphics;
Thread t;
public void init (){
pnl = new JPanel (null);
lbl = new JLabel ();
lbl.setText ("Text");
lbl.setOpaque(true);
add(pnl);
pnl.add (lbl);
lbl.setLocation(100, 100);
lbl.setBounds (100, 100, 200, 20);
buffer = createImage (500, 500);
bufferedGraphics = buffer.getGraphics ();
t = new Thread (this, "Label");
t.start ();
}// init method
public void paint (Graphics g){
if (g != null)
g.drawImage (buffer, 0, 0, this);
}//paint
public void update (Graphics g){
paint (g);
}//update
public void render (){
bufferedGraphics.setColor (Color.WHITE);
bufferedGraphics.fillRect (0, 0, 500, 500);
lbl.update (bufferedGraphics);
update(getGraphics());
}//render
public void run (){
while (true){
try{
render ();
t.sleep (20);
} catch (InterruptedException e){
e.printStackTrace ();
}//catch
}//while
}//run
}//LabelSSCCE
First, convert the JLabel to a BufferedImage:
public BufferedImage componentToImage(Component component)
{
BufferedImage img = new BufferedImage(component.getWidth(), component.getHeight(), BufferedImage.TYPE_INT_ARGB_PRE);
Graphics g = img.getGraphics();
g.setColor(component.getForeground());
g.setFont(component.getFont());
component.paintAll(g);
Rectangle region = new Rectangle(0, 0, img.getWidth(), img.getHeight());
return img.getSubimage(region.x, region.y, region.width, region.height);
}
Then, change the render method to something like this:
public void render() {
bufferedGraphics.setColor(Color.WHITE);
bufferedGraphics.fillRect(0, 0, 500, 500);
BufferedImage bi = componentToImage(lbl);
bufferedGraphics.drawImage(bi, lbl.getX(), lbl.getY(), null);
update(getGraphics());
}
I've implemented some custom fields and would like to keep the look-and-feel consistent with the current Blackberry theme. So I would like the highlighting color of the fields to be consistent with the highlight color used throughout the BB apps.
How can I get the this color?
Edit: Apparently, there's no way to get those kinds of colors from any API. So is there a work-around way to getting these colors?
What we can do is test colors on drawFocus method of some custom text field:
class TestField extends TextField {
TestThemeListener mListener;
public TestField(TestThemeListener listener) {
super(EDITABLE | FOCUSABLE);
setText("Hello this is a color test");
setSelection(0, true, 10);
mListener = listener;
}
protected void drawFocus(Graphics g, boolean on) {
drawHighlightRegion(g, HIGHLIGHT_FOCUS, true, 0, 0, 50, 20);
Bitmap bmp = new Bitmap(50, 20);
Display.screenshot(bmp, 0, 0, 50, 20);
int[] argbData = new int[1];
bmp.getARGB(argbData, 0, 1, 0, 0, 1, 1);
int focusColor = argbData[0];
drawHighlightRegion(g, HIGHLIGHT_SELECT, true, 50, 0, 50, 20);
Display.screenshot(bmp, 50, 0, 50, 20);
argbData = new int[1];
bmp.getARGB(argbData, 0, 1, 0, 0, 1, 1);
int selectionColor = argbData[0];
if (null != mListener) {
mListener.themeTested(focusColor, selectionColor);
mListener = null;
}
}
}
interface TestThemeListener {
void themeTested(int focusColor, int selectionColor);
}
And use it on the screen:
class Scr extends MainScreen implements TestThemeListener {
LabelField mSelectionColorName;
LabelField mFocusColorName;
public Scr() {
add(new TestField(this));
}
public void themeTested(int focusColor, int selectionColor) {
add(new LabelField("Theme colors (AARRGGBB)"));
add(new LabelField("Focus : " + focusColor));
Bitmap bmpF = new Bitmap(100, 20);
Graphics gF = new Graphics(bmpF);
gF.setColor(focusColor);
gF.fillRect(0, 0, 100, 20);
add(new BitmapField(bmpF));
add(new LabelField("Selection : " + selectionColor));
Bitmap bmpS = new Bitmap(100, 20);
Graphics gS = new Graphics(bmpS);
gS.setColor(selectionColor);
gS.fillRect(0, 0, 100, 20);
add(new BitmapField(bmpS));
}
}
color test app screenshot http://img188.imageshack.us/img188/8943/830001.png
This works fine but there is a problem. The application must be signed and it displays a confirmation to the user to take the screen shots like "Application xxx wants to take a screen shot. Allow?" This could be annoying to the user. This is caused by using a secure API in the device class.
Instead, try creating a new Graphics from a Bitmap, then draw on it.
class TestField : extends Field {
int getHighlightColor() {
Bitmap image = new Bitmap(getWidth(), getHeight());
Graphics g = new Graphics(image).
drawHighlightRegion(g, Field.HIGHLIGHT_FOCUS, true, 0, 0, getWidth(), getHeight());
paint(
argbData = new int[1];
image.getARGB(argbData, 0, 1, 0, 0, 1, 1);
return argbData[0];
}
}