textPane cannot be resolved - jscrollpane

I am writing a program in eclipse that generates reports based on inputs from a user. I have a JTextPane in a JScrollPane to allow the user to enter a description of the text, then add it to the system by pressing a button, but I am getting the error "textPaneDescription cannot be resolved".
The code(excluding imports)
public class GUI{
private JFrame frame;
private JTextField textFieldHoursWorked;
private JTextField textFieldParking;
private JTextField textFieldCongestion;
private JTextField textFieldDays;
private JTextField textFieldSmallTools;
private JTextField textFieldHireCharges;
private JTextField textFieldMaterials;
private JTextField textFieldSubContracted;
private JTextField textFieldSupplyOnlyWorks;
private JTextField textFieldConsultants;
private int counter = 0;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
GUI window = new GUI();
window.frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the application.
*/
public GUI() {
initialize();
}
/**
* Initialize the contents of the frame.
*/
private void initialize() {
final ArrayList<Operative> tierOneArray = new ArrayList();
final ArrayList<Operative> tierTwoArray = new ArrayList();
final ArrayList<Operative> semiSkilledArray = new ArrayList();
final ArrayList<Operative> supervisionArray = new ArrayList();
frame = new JFrame();
frame.setBounds(100, 100, 780, 555);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().setLayout(null);
JScrollPane scrollPaneHistory = new JScrollPane();
scrollPaneHistory.setBounds(353, 0, 411, 516);
frame.getContentPane().add(scrollPaneHistory);
final JTextArea textAreaHistory = new JTextArea();
textAreaHistory.setEditable(false);
scrollPaneHistory.setViewportView(textAreaHistory);
final JComboBox comboBoxOperativeType = new JComboBox();
comboBoxOperativeType.setModel(new DefaultComboBoxModel(new String[] {"Skilled Operative (Tier 1)", "Skilled Operative (Tier 2)", "Semi-skilled Operative", "Supervision"}));
comboBoxOperativeType.setBounds(139, 52, 182, 20);
frame.getContentPane().add(comboBoxOperativeType);
JLabel lblOperativeType = new JLabel("Operative Type");
lblOperativeType.setFont(new Font("Tahoma", Font.PLAIN, 14));
lblOperativeType.setBounds(35, 52, 99, 20);
frame.getContentPane().add(lblOperativeType);
JLabel lblHoursWorked = new JLabel("Hours Worked");
lblHoursWorked.setFont(new Font("Tahoma", Font.PLAIN, 14));
lblHoursWorked.setBounds(40, 114, 89, 20);
frame.getContentPane().add(lblHoursWorked);
textFieldHoursWorked = new JTextField();
textFieldHoursWorked.setBounds(139, 114, 182, 20);
frame.getContentPane().add(textFieldHoursWorked);
textFieldHoursWorked.setColumns(10);
final JComboBox comboBoxWorkingHours = new JComboBox();
comboBoxWorkingHours.setModel(new DefaultComboBoxModel(new String[] {"In", "Out"}));
comboBoxWorkingHours.setBounds(139, 145, 182, 20);
frame.getContentPane().add(comboBoxWorkingHours);
JLabel lblNewLabelWorkingHours = new JLabel("In/Out of Hours");
lblNewLabelWorkingHours.setFont(new Font("Tahoma", Font.PLAIN, 14));
lblNewLabelWorkingHours.setBounds(35, 145, 99, 20);
frame.getContentPane().add(lblNewLabelWorkingHours);
final JRadioButton rdbtnEmergency = new JRadioButton("Emergency?");
rdbtnEmergency.setFont(new Font("Tahoma", Font.PLAIN, 14));
rdbtnEmergency.setForeground(new Color(0, 0, 0));
rdbtnEmergency.setBounds(135, 233, 20, 23);
frame.getContentPane().add(rdbtnEmergency);
JLabel lblParking = new JLabel("Parking");
lblParking.setFont(new Font("Tahoma", Font.PLAIN, 14));
lblParking.setBounds(84, 176, 45, 18);
frame.getContentPane().add(lblParking);
textFieldParking = new JTextField();
textFieldParking.setColumns(10);
textFieldParking.setBounds(139, 176, 182, 20);
frame.getContentPane().add(textFieldParking);
JLabel lblCongestion = new JLabel("Congestion");
lblCongestion.setFont(new Font("Tahoma", Font.PLAIN, 14));
lblCongestion.setBounds(60, 205, 69, 18);
frame.getContentPane().add(lblCongestion);
textFieldCongestion = new JTextField();
textFieldCongestion.setColumns(10);
textFieldCongestion.setBounds(139, 206, 182, 20);
frame.getContentPane().add(textFieldCongestion);
JLabel lblEmergency = new JLabel("Emergency?");
lblEmergency.setFont(new Font("Tahoma", Font.PLAIN, 14));
lblEmergency.setBounds(51, 235, 83, 18);
frame.getContentPane().add(lblEmergency);
JButton btnAddOne = new JButton("Add");
btnAddOne.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
counter ++;
Labourer t1Operative = new Labourer("Tier 1", 29.75, 39.67, 39.67, 52.89, 45.00, 85.00);
Labourer t2Operative = new Labourer("Tier 2", 32.98, 43.97, 43.97, 58.63, 45.00, 85.00);
Labourer semiSkilledOperative = new Labourer("Semi Skilled", 25.50, 34.00, 34.00, 45.33, 45.00, 85.00);
Labourer supervisionOperative = new Labourer("Tier 2", 34.00, 45.33, 45.33, 60.44, 65.00, 100.00);
if(comboBoxOperativeType.getSelectedIndex() == 0)
{
tierOneArray.add(new Operative(textPaneDescription.getText(), (String)comboBoxOperativeType.getSelectedItem(), textFieldDays.getText(), textFieldHoursWorked.getText(), (String)comboBoxWorkingHours.getSelectedItem(),textFieldParking.getText(), textFieldCongestion.getText(), rdbtnEmergency.isSelected(), t1Operative));
}
else if(comboBoxOperativeType.getSelectedIndex() == 1)
{
tierTwoArray.add(new Operative(textPaneDescription.getText(), (String)comboBoxOperativeType.getSelectedItem(), textFieldDays.getText(), textFieldHoursWorked.getText(), (String)comboBoxWorkingHours.getSelectedItem(),textFieldParking.getText(), textFieldCongestion.getText(), rdbtnEmergency.isSelected(), t2Operative));
}
else if(comboBoxOperativeType.getSelectedIndex() == 2)
{
semiSkilledArray.add(new Operative(textPaneDescription.getText(), (String)comboBoxOperativeType.getSelectedItem(), textFieldDays.getText(), textFieldHoursWorked.getText(), (String)comboBoxWorkingHours.getSelectedItem(),textFieldParking.getText(), textFieldCongestion.getText(), rdbtnEmergency.isSelected(), semiSkilledOperative));
}
else if(comboBoxOperativeType.getSelectedIndex() == 3)
{
supervisionArray.add(new Operative(textPaneDescription.getText(), (String)comboBoxOperativeType.getSelectedItem(), textFieldDays.getText(), textFieldHoursWorked.getText(), (String)comboBoxWorkingHours.getSelectedItem(),textFieldParking.getText(), textFieldCongestion.getText(), rdbtnEmergency.isSelected(), supervisionOperative));
}
textAreaHistory.append(counter + ": " + (String)comboBoxOperativeType.getSelectedItem() + " / " + textFieldDays.getText()+ "day(s) / " + textFieldHoursWorked.getText() + "hours(s) / " + (String)comboBoxWorkingHours.getSelectedItem() + " / £" + textFieldParking.getText() + " / £" + textFieldCongestion.getText() + " / " + rdbtnEmergency.isSelected() + "\n");
}
});
btnAddOne.setBounds(231, 235, 89, 23);
frame.getContentPane().add(btnAddOne);
JButton btnDone = new JButton("Done");
btnDone.setBounds(231, 480, 89, 23);
btnDone.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
Logic logic = new Logic(tierOneArray, tierTwoArray, semiSkilledArray, supervisionArray);
}
});
frame.getContentPane().add(btnDone);
JLabel labelDays = new JLabel("Days Worked");
labelDays.setFont(new Font("Tahoma", Font.PLAIN, 14));
labelDays.setBounds(45, 83, 89, 20);
frame.getContentPane().add(labelDays);
textFieldDays = new JTextField();
textFieldDays.setColumns(10);
textFieldDays.setBounds(139, 83, 182, 20);
frame.getContentPane().add(textFieldDays);
JSeparator separator = new JSeparator();
separator.setBounds(0, 270, 352, 2);
frame.getContentPane().add(separator);
JLabel lblSmallTools = new JLabel("Small Tools and Incidental Costs");
lblSmallTools.setBounds(8, 308, 219, 14);
frame.getContentPane().add(lblSmallTools);
textFieldSmallTools = new JTextField();
lblSmallTools.setLabelFor(textFieldSmallTools);
textFieldSmallTools.setBounds(199, 305, 141, 20);
frame.getContentPane().add(textFieldSmallTools);
textFieldSmallTools.setColumns(10);
JLabel lblHireCharges = new JLabel("Hire Charges");
lblHireCharges.setBounds(118, 283, 128, 14);
frame.getContentPane().add(lblHireCharges);
textFieldHireCharges = new JTextField();
textFieldHireCharges.setColumns(10);
textFieldHireCharges.setBounds(199, 280, 141, 20);
frame.getContentPane().add(textFieldHireCharges);
JLabel lblMaterials = new JLabel("Material Purchase Costs");
lblMaterials.setBounds(51, 333, 181, 14);
frame.getContentPane().add(lblMaterials);
textFieldMaterials = new JTextField();
textFieldMaterials.setColumns(10);
textFieldMaterials.setBounds(199, 333, 141, 20);
frame.getContentPane().add(textFieldMaterials);
JLabel lblSubcontracted = new JLabel("Specialist Subcontraced Works");
lblSubcontracted.setBounds(10, 361, 212, 14);
frame.getContentPane().add(lblSubcontracted);
textFieldSubContracted = new JTextField();
textFieldSubContracted.setColumns(10);
textFieldSubContracted.setBounds(199, 358, 141, 20);
frame.getContentPane().add(textFieldSubContracted);
JLabel lblSupplyOnlyWorks = new JLabel("Supply Only Works");
lblSupplyOnlyWorks.setBounds(80, 389, 116, 17);
frame.getContentPane().add(lblSupplyOnlyWorks);
textFieldSupplyOnlyWorks = new JTextField();
textFieldSupplyOnlyWorks.setColumns(10);
textFieldSupplyOnlyWorks.setBounds(199, 387, 141, 20);
frame.getContentPane().add(textFieldSupplyOnlyWorks);
JLabel lblConsultants = new JLabel("Consultants and Designers");
lblConsultants.setBounds(35, 417, 206, 14);
frame.getContentPane().add(lblConsultants);
textFieldConsultants = new JTextField();
textFieldConsultants.setColumns(10);
textFieldConsultants.setBounds(199, 414, 141, 20);
frame.getContentPane().add(textFieldConsultants);
JButton btnAddTwo = new JButton("Add");
btnAddTwo.setBounds(232, 446, 89, 23);
frame.getContentPane().add(btnAddTwo);
JLabel lblDescription = new JLabel("Description");
lblDescription.setFont(new Font("Tahoma", Font.PLAIN, 14));
lblDescription.setBounds(60, 23, 69, 20);
frame.getContentPane().add(lblDescription);
JScrollPane scrollPaneDescription = new JScrollPane();
scrollPaneDescription.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
scrollPaneDescription.setBounds(139, 23, 182, 20);
frame.getContentPane().add(scrollPaneDescription);
JTextPane textPaneDescription = new JTextPane();
scrollPaneDescription.setViewportView(textPaneDescription);
}
}

It looks like you are accessing the variable textPaneDescription before you actually declared it.
You access the textPaneDescription when adding an action listener.
tierOneArray.add(new Operative(textPaneDescription.getText() ... );
While not only till later in the code you declare the variable, thus it can not be resolved.

Related

how to make shorter hard coded Jframe?

I'm just a beginner in coding. and we're using Net-beans.
here is the sample output he is expecting or want us to code.
sample output
Our prof. wants us to create a program without using any short-cut or other class(1 class and 1 main method). here is my codes and i want to make it short. is there a possible way?
package chuajframe;
import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class ChuaJFrame {
static double price;
static double subTotal = 0;
static double subTotal1 = 0;
static int qty;
static double total = 0;
static double change;
static double bill;
public static void main(String s[]) {
JFrame z = new JFrame("Chua JFrame");
JLabel label = new JLabel("Welcome");
label.setBounds(160, 0, 100, 20);
JLabel a = new JLabel("Descrition Price
Quantity"); a.setBounds(10, 10, 400, 50);
//JLabel a1 = new JLabel("Price"); a1.setBounds(120,10,100, 50);
//JLabel a2 = new JLabel("Quantity"); a2.setBounds(210,10,100, 50);
JTextField b = new JTextField(); b.setBounds(10,50, 70, 20);
JTextField b1= new JTextField(); b1.setBounds(120,50, 70, 20);
JTextField b2= new JTextField(); b2.setBounds(210,50, 70, 20);
JButton b3 = new JButton("Add"); b3.setBounds(300,50,70, 20);
JTextField c = new JTextField(); c.setBounds(10,80, 70, 20);
JTextField c1= new JTextField(); c1.setBounds(120,80, 70, 20);
JTextField c2= new JTextField(); c2.setBounds(210,80, 70, 20);
JButton c3 = new JButton("Add"); c3.setBounds(300,80,70, 20);
JLabel label1 = new JLabel("Your about to purchase the following");label1.setBounds(90, 100, 300,30);
JLabel label2 = new JLabel("Subtotal"); label2.setBounds(280, 120, 300,30);
JLabel d = new JLabel(); d.setBounds(10, 150, 100, 20);
JLabel d1 = new JLabel(); d1.setBounds(120,150,100, 20);
JLabel d2 = new JLabel(); d2.setBounds(210,150,100, 20);
JLabel d3 = new JLabel(); d3.setBounds(300,150,100, 20);
JLabel e = new JLabel(); e.setBounds(10, 180, 100, 20);
JLabel e1 = new JLabel(); e1.setBounds(120,180,100, 20);
JLabel e2 = new JLabel(); e2.setBounds(210,180,100, 20);
JLabel e3 = new JLabel(); e3.setBounds(300,180,100, 20);
JLabel f = new JLabel("Total"); f.setBounds(210, 210, 100, 20);
JLabel f1 = new JLabel(); f1.setBounds(300, 210, 100, 20);
JTextField g = new JTextField(); g.setBounds(250,240,100,20);
JButton g1 = new JButton("Payment"); g1.setBounds(140,240,100, 20);
JLabel h = new JLabel(); h.setBounds(140,270,200, 20);
JLabel h1 = new JLabel(); h1.setBounds(300,270,80, 20);
JLabel i = new JLabel(); i.setBounds(140,285,200, 20);
b3.addActionListener(new ActionListener(){
#Override
public void actionPerformed(ActionEvent e){
d.setText(b.getText());
d1.setText(b1.getText());
d2.setText(b2.getText());
price = Double.parseDouble(d1.getText());
qty = Integer.parseInt(d2.getText());
subTotal = price * qty;
d3.setText(Double.toString(subTotal));
total = subTotal + subTotal1;
f1.setText(Double.toString(total));
b.setText(" ");
b1.setText(" ");
b2.setText(" ");
}});
c3.addActionListener(new ActionListener(){
#Override
public void actionPerformed(ActionEvent ae){
e.setText(c.getText());
e1.setText(c1.getText());
e2.setText(c2.getText());
price = Double.parseDouble(e1.getText());
qty = Integer.parseInt(e2.getText());
subTotal1 = price * qty;
e3.setText(Double.toString(subTotal1));
total = subTotal + subTotal1;
f1.setText(Double.toString(total));
c.setText(" ");
c1.setText(" ");
c2.setText(" ");
}});
g1.addActionListener(new ActionListener(){
#Override
public void actionPerformed(ActionEvent e){
bill = Double.parseDouble(g.getText());
if(bill>total)
{
change = bill - total;
h.setText("Change");
h1.setText(Double.toString(change));
i.setText("Thank you for purchasing.");
}
else{
h.setText("Sorry insufficient payment.");
i.setText("Please enter your payment again!!");
}
}});
z.add(label);
z.add(a); //z.add(a1); z.add(a2);
z.add(b); z.add(b1); z.add(b2); z.add(b3);
z.add(c); z.add(c1); z.add(c2); z.add(c3);
z.add(label1); z.add(label2);
z.add(d); z.add(d1); z.add(d2); z.add(d3);
z.add(e); z.add(e1); z.add(e2); z.add(e3);
z.add(f); z.add(f1);
z.add(g); z.add(g1);
z.add(h); z.add(h1);
z.add(i);
z.setSize(400,350);
z.setLayout(null);
z.setVisible(true);
z.setLocationRelativeTo(null);
z.setResizable(false);
z.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
You can do something called compound instantiation with most of your fields.
For example, turn this:
static double price;
static double subTotal = 0;
static double subTotal1 = 0;
static int qty;
static double total = 0;
static double change;
static double bill;
Into this:
static double price = 0, subTotal = 0, subTotal1 = 0, total = 0, change = 0, bill = 0;
static int qty;
Or this:
JTextField b = new JTextField();
JTextField b1= new JTextField();
JTextField b2= new JTextField();
JButton b3 = new JButton("Add");
Into this:
JTextField b = new JTextField(), b1= new JTextField(), b2= new JTextField(), b3 = new JButton("Add");
And then use a for loop to add all the things to z that you need to add at the bottom of your class

How to pan/scale the contents inside a GUI area?

I want to have a zoom effect inside an area in a EditorWindow, something like a zoomable scrollview.
The following snippet deals only with the panning effect but it exemplifies the issue I'm having when the contents inside the zoomable area and the clipping rect can't be handled independently of each other through hSliderValue1 (clipping) and hSliderValue2 (panning).
using System;
using System.Collections.Generic;
using UnityEngine;
using UnityEditor;
public class ZoomTestWindow : EditorWindow
{
private static float kEditorWindowTabHeight = 20;
private static Matrix4x4 _prevGuiMatrix;
public static float hSliderValue1 = 0;
public static float hSliderValue2 = 0;
Rect[] wr = new Rect[]{
new Rect(0, 0, 100, 100),
new Rect(50, 50, 100, 100),
new Rect(100, 100, 100, 100)
};
[MenuItem("Window/Zoom Test #%w")]
private static void Init()
{
ZoomTestWindow window = EditorWindow.GetWindow<ZoomTestWindow>("Zoom Test", true, new System.Type[] {
typeof(UnityEditor.SceneView),
typeof(EditorWindow).Assembly.GetType("UnityEditor.SceneHierarchyWindow")});
window.Show();
EditorWindow.FocusWindowIfItsOpen<ZoomTestWindow>();
}
public static Rect BeginZoomArea()
{
GUI.EndGroup(); //End the group that Unity began so we're not bound by the EditorWindow
GUI.BeginGroup(new Rect(hSliderValue1, 0, 200, 200));
_prevGuiMatrix = GUI.matrix;
GUI.matrix = Matrix4x4.TRS(new Vector2(hSliderValue2, 0), Quaternion.identity, Vector3.one);;
return new Rect();
}
public static void EndZoomArea()
{
GUI.matrix = _prevGuiMatrix;
GUI.EndGroup();
GUI.BeginGroup(new Rect(0.0f, kEditorWindowTabHeight, Screen.width, Screen.height - (kEditorWindowTabHeight + 3)));
}
public void OnGUI()
{
BeginZoomArea();
BeginWindows();
wr[0] = GUI.Window(0, wr[0], DrawWindow, "hello");
wr[1] = GUI.Window(1, wr[1], DrawWindow, "world");
wr[2] = GUI.Window(2, wr[2], DrawWindow, "!");
EndWindows();
EndZoomArea();
hSliderValue1 = GUI.HorizontalSlider(new Rect(200, 5, 100, 30), hSliderValue1, 0, 100);
hSliderValue2 = GUI.HorizontalSlider(new Rect(200, 25, 100, 30), hSliderValue2, 0, 100);
}
void DrawWindow(int id)
{
GUI.Button(new Rect(0, 30, 100, 50), "Wee!");
GUI.DragWindow();
}
}
Is there a way to do this, maybe by using a scroll view?
Embed the group controlled by hSliderValue1 inside a new group.
using System;
using System.Collections.Generic;
using UnityEngine;
using UnityEditor;
public class ZoomMoveTestWindow: EditorWindow
{
private static float kEditorWindowTabHeight = 20;
private static Matrix4x4 _prevGuiMatrix;
public static float hSliderValue1 = 0;
public static float hSliderValue2 = 0;
Rect[] wr = new Rect[]{
new Rect(0, 0, 100, 100),
new Rect(50, 50, 100, 100),
new Rect(100, 100, 100, 100)
};
[MenuItem("Window/Zoom Test #%w")]
private static void Init()
{
ZoomMoveTestWindow window = EditorWindow.GetWindow<ZoomMoveTestWindow>("Zoom Test", true, new System.Type[] {
typeof(UnityEditor.SceneView),
typeof(EditorWindow).Assembly.GetType("UnityEditor.SceneHierarchyWindow")});
window.Show();
EditorWindow.FocusWindowIfItsOpen<ZoomMoveTestWindow>();
}
public static Rect BeginZoomArea(Rect rect)
{
GUI.BeginGroup(rect);
GUI.BeginGroup(new Rect(hSliderValue1, 0, 200, 200));
_prevGuiMatrix = GUI.matrix;
GUI.matrix = Matrix4x4.TRS(new Vector2(hSliderValue2, 0), Quaternion.identity, Vector3.one);
return new Rect();
}
public static void EndZoomArea()
{
GUI.EndGroup();
GUI.matrix = _prevGuiMatrix;
GUI.EndGroup();
GUI.BeginGroup(new Rect(0.0f, kEditorWindowTabHeight, Screen.width, Screen.height - (kEditorWindowTabHeight + 3)));
}
public void OnGUI()
{
GUI.EndGroup(); //End the group that Unity began so we're not bound by the EditorWindow
BeginZoomArea(new Rect(10,10, 200, 200));
BeginWindows();
wr[0] = GUI.Window(0, wr[0], DrawWindow, "hello");
wr[1] = GUI.Window(1, wr[1], DrawWindow, "world");
wr[2] = GUI.Window(2, wr[2], DrawWindow, "!");
EndWindows();
EndZoomArea();
hSliderValue1 = GUI.HorizontalSlider(new Rect(250, 5, 100, 30), hSliderValue1, 0, 100);
hSliderValue2 = GUI.HorizontalSlider(new Rect(250, 35, 100, 30), hSliderValue2, 0, 100);
}
void DrawWindow(int id)
{
GUI.Button(new Rect(0, 30, 100, 50), "Wee!");
GUI.DragWindow();
}
}
It works but I don't know why. Because the interaction of GUI.matrix and BeginGroup(rect) is unknown.
PS: This post may help you.

How to add working JScrollPane to JTextArea?

I have a problem with this code. At " //Jūsu piedāvātais preču sortiments" I'm trying to add JScrollPane into JTextArea. I have tried lots for methods, but no one of them works. Only thing what I can do is to add it but it will not work what ever I do. So, if someone have any idea how to do it, it would be greatly appreciate.
Here is the code:
import java.awt.*;
import javax.swing.*;
public class otraForma {
public static void main(String[] args) {
JFrame frame = new JFrame("Pieteikuma forma");
frame.setSize(new Dimension(355, 500));
frame.setResizable(false);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel panel = new JPanel();
frame.add(panel);
komponenti(panel);
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
private static void komponenti(JPanel panel) {
panel.setLayout(null);
/*Šeit uzskaita visu formu
* Tai skaitaa ari izmerus. Tos droši vien jāliek, lai autmātiski maina,
* bet nezinu kā to izdarīt. Vēlāk arī pievinos krāsas vai dizainu.
*/
//------------JTextField-------------//
//Vārds
JLabel vards = new JLabel("Vārds"); //ar JLabel izveido pāsu textu
vards.setBounds(10, 10, 80, 25); //ievieto mērogu (tas būs jāmaina)
panel.add(vards); //pievieno viņu formai
JTextField vards_texts = new JTextField(20); //tiek izveidots ievades laukums
vards_texts.setBounds(100, 10, 240, 25);
panel.add(vards_texts);
//------------JTextField-------------//
//Uzvārds
JLabel uzvards = new JLabel("Uzvārds");
uzvards.setBounds(10, 40, 80, 25);
panel.add(uzvards);
JTextField uzvars_texts = new JTextField(20);
uzvars_texts.setBounds(100, 40, 240, 25);
panel.add(uzvars_texts);
//------------JTextField-------------//
//E-pasts
JLabel epasts = new JLabel("E-pasts");
epasts.setBounds(10, 70, 80, 25);
panel.add(epasts);
JTextField epasts_texts = new JTextField(30);
epasts_texts.setBounds(100, 70, 240, 25);
panel.add(epasts_texts);
//------------JComboBox-------------//
//Vēlamais apmeklējuma datums
JLabel datums = new JLabel("Apmeklējums");
datums.setBounds(10, 100, 80, 25);
panel.add(datums);
JComboBox datums_drop_menesi = new JComboBox();
datums_drop_menesi.addItem("Janvāris");
datums_drop_menesi.addItem("Februāris");
datums_drop_menesi.addItem("Marts");
datums_drop_menesi.addItem("Aprīlis");
datums_drop_menesi.addItem("Maijs");
datums_drop_menesi.addItem("Jūnis");
datums_drop_menesi.addItem("Jūlijs");
datums_drop_menesi.addItem("Augusts");
datums_drop_menesi.addItem("Septembris");
datums_drop_menesi.addItem("Oktobris");
datums_drop_menesi.addItem("Novembris");
datums_drop_menesi.addItem("Decembris");
//datums_drop_menesi.setMaximumRowCount(datums_drop_menesi.getModel().getSize());
datums_drop_menesi.setBounds(100, 100, 115, 25); //(* , *, garums, *)
panel.add(datums_drop_menesi);
JComboBox datums_drop_dienas = new JComboBox();
for(int i=1;i<=31;i++) {
datums_drop_dienas.addItem(new Integer(i));
}
datums_drop_dienas.setBounds(225, 100, 115, 25);
panel.add(datums_drop_dienas);
//------------JCheckBox-------------//
//Vai nepieciešams pavilijons?
JLabel pavilijons = new JLabel("Vai nepieciešams pavilijons?");
pavilijons.setBounds(10, 130, 170, 25);
panel.add(pavilijons);
JCheckBox pavilijons_check1 = new JCheckBox();
pavilijons_check1.setText("Jā");
pavilijons_check1.setBounds(225, 130, 40, 25);
panel.add(pavilijons_check1);
JCheckBox pavilijons_check2 = new JCheckBox();
pavilijons_check2.setText("Nē");
pavilijons_check2.setBounds(300, 130, 40, 25);
panel.add(pavilijons_check2);
ButtonGroup group1 = new ButtonGroup(); //nodrošina to, ka tikai, ka viens check box
group1.add(pavilijons_check1); //ir nospiests vienlaiciigi
group1.add(pavilijons_check2);
//------------JCheckBox-------------//
//Vai nepieciešams tirgus galds?
JLabel galds = new JLabel("Vai nepieciešams tirgus galds?");
galds.setBounds(10, 160, 180, 25);
panel.add(galds);
JCheckBox galds_check1 = new JCheckBox();
galds_check1.setText("Jā");
galds_check1.setBounds(225, 160, 40, 25);
panel.add(galds_check1);
JCheckBox galds_check2 = new JCheckBox();
galds_check2.setText("Nē");
galds_check2.setBounds(300, 160, 40, 25);
panel.add(galds_check2);
ButtonGroup group2 = new ButtonGroup();
group2.add(galds_check1);
group2.add(galds_check2);
//------------JCheckBox-------------//
//Vai nepieciešams elektrības pieslēgums?
JLabel elektriba = new JLabel("Vai nepieciešama elektrība?");
elektriba.setBounds(10, 190, 180, 25);
panel.add(elektriba);
JCheckBox elektriba_check1 = new JCheckBox();
elektriba_check1.setText("Jā");
elektriba_check1.setBounds(225, 190, 40, 25);
panel.add(elektriba_check1);
JCheckBox elektriba_check2 = new JCheckBox();
elektriba_check2.setText("Nē");
elektriba_check2.setBounds(300, 190, 40, 25);
ButtonGroup group3 = new ButtonGroup();
group3.add(elektriba_check1);
group3.add(elektriba_check2);
panel.add(elektriba_check2);
//------------JTextField-------------//
//Jūsu piedāvātais preču sortiments
JLabel piedavajums = new JLabel("Jūsu piedāvātais preču sortiments.");
piedavajums.setBounds(10, 220, 325, 25);
panel.add(piedavajums);
JTextArea piedavajums_texts = new JTextArea();
piedavajums_texts.setBounds(10, 250, 300, 100);
piedavajums_texts.setWrapStyleWord(true);
piedavajums_texts.setLineWrap(true);
JScrollPane sp = new JScrollPane(piedavajums_texts);
sp.getVerticalScrollBar();
sp.setBounds(300, 250, 20, 100);
panel.add(sp);
panel.add(piedavajums_texts);
//------------JButton-------------//
//Beigt
JButton atpakal = new JButton("Info");
atpakal.setBounds(10, 425, 80, 25); // x-ass, y-ass. texkta bloka garums/augstums
panel.add(atpakal);
//Importēt
JButton imports = new JButton("Importēt");
imports.setBounds(123, 425, 100, 25);
panel.add(imports);
//Beigt
JButton beigt = new JButton("Beigt");
beigt.setBounds(255, 425, 80, 25);
panel.add(beigt);
}
}

problems with rowspan while generating a PDF using iText

Below is my sample application where I am getting an error on line
Add2CellRow(table, "Age", 1, "Gender", 2);
I think the rowspan value is creating the issue as the next line is not building.
Can someone help on the same?
The code is for Arabic Culture so need to set
Thread.CurrentThread.CurrentCulture = new CultureInfo("ar-SA");
Thread.CurrentThread.CurrentUICulture = new CultureInfo("ar-SA");
private static void Add3CellRow(PdfPTable table, string cell1Value, string cell2Value, int rowspan, string imgPath)
{
PdfPCell new1Cell = new PdfPCell(new Phrase(cell1Value, tableFont));
new1Cell.VerticalAlignment = Element.ALIGN_MIDDLE;
new1Cell.MinimumHeight = 25f;
new1Cell.Padding = 6f;
table.AddCell(new1Cell);
PdfPCell new2Cell = new PdfPCell(new Phrase(cell2Value, tableFont));
new2Cell.VerticalAlignment = Element.ALIGN_MIDDLE;
new2Cell.MinimumHeight = 25f;
new2Cell.Padding = 6f;
new2Cell.Colspan = 2;
table.AddCell(new2Cell);
PdfPCell new3Cell = null;
if (!String.IsNullOrWhiteSpace(imgPath))
{
Image img = Image.GetInstance(imgPath);
img.ScaleToFit(150, 150);
new3Cell = new PdfPCell(img);
}
else
{
new3Cell = new PdfPCell(new Phrase(String.Empty));
}
new3Cell.VerticalAlignment = Element.ALIGN_MIDDLE;
new3Cell.HorizontalAlignment = Element.ALIGN_CENTER;
new3Cell.MinimumHeight = 25f;
new3Cell.Rowspan = rowspan;
new3Cell.Colspan = 2;
new3Cell.Padding = 6f;
table.AddCell(new3Cell);
}
private static void Add2CellRow(PdfPTable table, string cell1Value, int colspanCell1, string cell2Value, int colspanCell2 = 2)
{
PdfPCell new1Cell = new PdfPCell(new Phrase(cell1Value, tableFont));
new1Cell.VerticalAlignment = Element.ALIGN_MIDDLE;
new1Cell.MinimumHeight = 25f;
new1Cell.Colspan = colspanCell1;
table.AddCell(new1Cell);
PdfPCell new2Cell = new PdfPCell(new Phrase(cell2Value, tableFont));
new2Cell.VerticalAlignment = Element.ALIGN_MIDDLE;
new2Cell.Colspan = colspanCell2;
new2Cell.MinimumHeight = 25f;
new2Cell.Padding = 6f;
table.AddCell(new2Cell);
}
public static MemoryStream Generate()
{
var doc = new Document(PageSize.A4, 10, 10, 90, 110);
MemoryStream memoryStream = new MemoryStream();
PdfWriter writer = PdfWriter.GetInstance(doc, memoryStream);
doc.Open();
PdfPTable table = new PdfPTable(5);
table.RunDirection = PdfWriter.RUN_DIRECTION_RTL;
table.WidthPercentage = 89.5f;
table.DefaultCell.VerticalAlignment = Element.ALIGN_MIDDLE;
table.DefaultCell.Padding = 6f;
table.DefaultCell.UseAscender = true;
table.DefaultCell.UseDescender = true;
Add3CellRow(table,"Name","Harshit Verma",4,"");
Add2CellRow(table, "Age", 1, "Gender", 2);
Add2CellRow(table, "Age", 1, "Gender", 2);
Add2CellRow(table, "Age", 1, "Gender", 2);
table.SplitLate = false;
Paragraph p = new Paragraph("");
doc.Add(p);
table.SpacingBefore = 30f;
doc.Add(table);
writer.CloseStream = false;
doc.Close();
memoryStream.Position = 0;
return memoryStream;
}

How to use " 9-sliced" image type in non-GUI elements?

In my game, I am using simple textures on quads. I could see Unity GUI allows us to slice the image by setting "image type" option as sliced.
I would like to do the same to my other textures I use in quads.
In simple words, I dont want the edges of my textures to be scaled when the texture itself is scaled.
Thanks.
Looks like you have to generate the mesh yourself, not so hard as it sounds. The mesh has to look like this:
Each textured mesh is defined by vertices (red dots on the picture) and (in this case) each vertex has two parameters:
A Vector3 with position. Only x and y used here, as a quad is flat (so, z = 0).
A Vector2 with the UV, that is how the material is put on the mesh. This are the texture coordinates: u and v.
To configure this mesh, I added parameters:
b - for border - how big is the border of the gameobject (in 3d units)
w, h - for width and height of the gameobject (in 3d units)
m - how big is the margin in the image (in float 0f to 0.5f)
How to do this in Unity3d:
Create an GameObject, add a MeshRenderer with the material you want and an empty MeshFilter.
Add a script that creates a mesh. Look at Unity3d docs for Mesh, first example.
Note that for unity3d you have to create this with triangles, not quads (so for each quad make two triangles).
Chanibal's solutions worked like a charm for me, but since I wasn't an expert in the matter, it wasn't trivial to me to implement this. I will let this code here in case somebody could find it useful.
using UnityEngine;
public class SlicedMesh : MonoBehaviour
{
private float _b = 0.1f;
public float Border
{
get
{
return _b;
}
set
{
_b = value;
CreateSlicedMesh();
}
}
private float _w = 1.0f;
public float Width
{
get
{
return _w;
}
set
{
_w = value;
CreateSlicedMesh();
}
}
private float _h = 1.0f;
public float Height
{
get
{
return _h;
}
set
{
_h = value;
CreateSlicedMesh();
}
}
private float _m = 0.4f;
public float Margin
{
get
{
return _m;
}
set
{
_m = value;
CreateSlicedMesh();
}
}
void Start()
{
CreateSlicedMesh();
}
void CreateSlicedMesh()
{
Mesh mesh = new Mesh();
GetComponent<MeshFilter>().mesh = mesh;
mesh.vertices = new Vector3[] {
new Vector3(0, 0, 0), new Vector3(_b, 0, 0), new Vector3(_w-_b, 0, 0), new Vector3(_w, 0, 0),
new Vector3(0, _b, 0), new Vector3(_b, _b, 0), new Vector3(_w-_b, _b, 0), new Vector3(_w, _b, 0),
new Vector3(0, _h-_b, 0), new Vector3(_b, _h-_b, 0), new Vector3(_w-_b, _h-_b, 0), new Vector3(_w, _h-_b, 0),
new Vector3(0, _h, 0), new Vector3(_b, _h, 0), new Vector3(_w-_b, _h, 0), new Vector3(_w, _h, 0)
};
mesh.uv = new Vector2[] {
new Vector2(0, 0), new Vector2(_m, 0), new Vector2(1-_m, 0), new Vector2(1, 0),
new Vector2(0, _m), new Vector2(_m, _m), new Vector2(1-_m, _m), new Vector2(1, _m),
new Vector2(0, 1-_m), new Vector2(_m, 1-_m), new Vector2(1-_m, 1-_m), new Vector2(1, 1-_m),
new Vector2(0, 1), new Vector2(_m, 1), new Vector2(1-_m, 1), new Vector2(1, 1)
};
mesh.triangles = new int[] {
0, 4, 5,
0, 5, 1,
1, 5, 6,
1, 6, 2,
2, 6, 7,
2, 7, 3,
4, 8, 9,
4, 9, 5,
5, 9, 10,
5, 10, 6,
6, 10, 11,
6, 11, 7,
8, 12, 13,
8, 13, 9,
9, 13, 14,
9, 14, 10,
10, 14, 15,
10, 15, 11
};
}
}
Here is another impementation of the same mesh as described above(just add Tile9Mesh script to empty GameObject and it should work
):
using UnityEngine;
namespace Util {
[ExecuteInEditMode]
[RequireComponent(typeof(MeshRenderer), typeof(MeshFilter))]
public class Tile9Mesh : MonoBehaviour {
public float width = 1;
public float height = 1;
[Range(0, 0.5f)]
public float uvLeft = 0.2f;
[Range(0, 0.5f)]
public float uvRight = 0.2f;
[Range(0, 0.5f)]
public float uvTop = 0.2f;
[Range(0, 0.5f)]
public float uvBottom = 0.2f;
public float uvToWorldScaleX = 1;
public float uvToWorldScaleY = 1;
private Mesh mesh;
private Vector3[] vertices;
private Vector2[] uv;
private void Start() {
vertices = new Vector3[16];
uv = new Vector2[16];
mesh = new Mesh {
name = "Tile9Mesh"
};
FillGeometry();
FillMesh();
mesh.triangles = new[] {
0, 1, 12, 0, 12, 11,
1, 2, 13, 1, 13, 12,
2, 3, 4, 2, 4, 13,
13, 4, 5, 13, 5, 14,
14, 5, 6, 14, 6, 7,
15, 14, 7, 15, 7, 8,
10, 15, 8, 10, 8, 9,
11, 12, 15, 11, 15, 10,
12, 13, 14, 12, 14, 15
};
RecalculateMesh();
gameObject.GetComponent<MeshFilter>().mesh = mesh;
}
public void UpdateMesh() {
if (mesh != null) {
FillGeometry();
FillMesh();
mesh.RecalculateBounds();
mesh.RecalculateNormals();
}
}
private void FillGeometry() {
{
float w = width;
float h = height;
float l = uvLeft * uvToWorldScaleX;
float r = width - uvRight * uvToWorldScaleX;
float t = height - uvTop * uvToWorldScaleY;
float b = uvBottom * uvToWorldScaleY;
vertices[0] = new Vector3(0, 0, 0);
vertices[1] = new Vector3(0, b, 0);
vertices[2] = new Vector3(0, t, 0);
vertices[3] = new Vector3(0, h, 0);
vertices[4] = new Vector3(l, h, 0);
vertices[5] = new Vector3(r, h, 0);
vertices[6] = new Vector3(w, h, 0);
vertices[7] = new Vector3(w, t, 0);
vertices[8] = new Vector3(w, b, 0);
vertices[9] = new Vector3(w, 0, 0);
vertices[10] = new Vector3(r, 0, 0);
vertices[11] = new Vector3(l, 0, 0);
vertices[12] = new Vector3(l, b, 0);
vertices[13] = new Vector3(l, t, 0);
vertices[14] = new Vector3(r, t, 0);
vertices[15] = new Vector3(r, b, 0);
}
{
const float w = 1;
const float h = 1;
float l = uvLeft;
float r = 1 - uvRight;
float t = 1 - uvTop;
float b = uvBottom;
uv[0] = new Vector2(0, 0);
uv[1] = new Vector2(0, b);
uv[2] = new Vector2(0, t);
uv[3] = new Vector2(0, h);
uv[4] = new Vector2(l, h);
uv[5] = new Vector2(r, h);
uv[6] = new Vector2(w, h);
uv[7] = new Vector2(w, t);
uv[8] = new Vector2(w, b);
uv[9] = new Vector2(w, 0);
uv[10] = new Vector2(r, 0);
uv[11] = new Vector2(l, 0);
uv[12] = new Vector2(l, b);
uv[13] = new Vector2(l, t);
uv[14] = new Vector2(r, t);
uv[15] = new Vector2(r, b);
}
}
private void FillMesh() {
mesh.vertices = vertices;
mesh.uv = uv;
}
private void RecalculateMesh() {
mesh.RecalculateBounds();
mesh.RecalculateNormals();
}
#if UNITY_EDITOR
private void OnValidate() {
if (mesh != null) {
UpdateMesh();
}
}
#endif
}
}