How to print a JTable header in two lines? - printing

Program is to print a JTabel and used function is
JTabel jt=new JTable();
MessageFormat headerFormat= new MessageFormat("My World Tomorrow");
MessageFormat footerFormat = new MessageFormat("Page {0}");
jt.Print(JTabel.Format,headerFormat,footerFormat);
Query is: How to print the header in two lines that is
My World
Tomorrow
Tired following solutions:
new MessageFormat("My world \n Tomorrow");
new MessageFormat("My world \r\n Tomorrow");
new MessageFormat("My world" System.getProperty("line.separator")+"Tomorrow"
);
Nothing works.

try this.
PrinterJob job = PrinterJob.getPrinterJob();
MessageFormat[] header = new MessageFormat[3];
header[0] = new MessageFormat("");
header[1] = new MessageFormat("line 1");
header[2] = new MessageFormat("line 2");
MessageFormat[] footer = new MessageFormat[2];
footer[0] = new MessageFormat("footer 1");
footer[1] = new MessageFormat("footer 2");
job.setPrintable(new MyTablePrintable(tblmunim, PrintMode.FIT_WIDTH, header, footer));
if (job.printDialog())
try {
System.out.println("Calling PrintJob.print()");
job.print();
System.out.println("End PrintJob.print()");
}
catch (PrinterException pe) {
System.out.println("Error printing: " + pe);
}
originally posted here http://sandeepsharma11.blogspot.com/2010/12/java-how-to-print-table-with-multi-line.html

Related

Want to check IF File.Exists to define StreamReader, to then searching for specific line

I don't write code often and my knowledge is still low-level. I need help for something that I can't figure out
public static void SearchScriptEnd()
{
int counter = 0;
string line;
Report.Log(ReportLevel.Info, "Read Log", "Starting to find: Test Suite Ended");
var text = "Test Suite Ended";
if (File.Exists(ConfigController.Home + TestSuite.Current.Parameters["LogPath"]))
{
StreamReader file =
new StreamReader(ConfigController.Home + TestSuite.Current.Parameters["LogPath"]);
}else{
StreamReader file =
new StreamReader(TestSuite.Current.Parameters["LogPath"]);
}
while ((line = file.ReadLine()) != null)
{
if (line.Contains(text))
{
Report.Log(ReportLevel.Info, "Read Log", "[Success] Script End String has been found");
Report.Log(ReportLevel.Info, "Read Log", string.Format("Line number: '{0}'", counter));
return;
}
counter++;
}
Report.Log(ReportLevel.Failure, "Read Log", "[Missing] Anvil Script End String NOT found");
file.Close();
}
At first, the while was in both statement and was working well, but I want to use the While outside of that statement, but I'm getting The name 'file' does not exist in the current context (CS0103) and I don't know how to get the value of file out of my If statement.
You need to get the variable out of the if-scope. Try this:
StreamReader file;
if (File.Exists(ConfigController.Home + TestSuite.Current.Parameters["LogPath"]))
{
file = new StreamReader(ConfigController.Home + TestSuite.Current.Parameters["LogPath"]);
}else{
file = new StreamReader(TestSuite.Current.Parameters["LogPath"]);
}
That way you have a value in file for sure. Happy coding.

want to get rid of Overwriting in filehandling

My code is perfect but still my cursor is on the beginning of text my and keep overwriting previous input whenever I give new input
String username=Cuser.getText();
String password=Cpass.getText();
FileWriter filewriter;
try{
filewriter=new FileWriter("Record.txt");
try (BufferedWriter bufferedWriter = new BufferedWriter(filewriter)) {
bufferedWriter.write("Username"+" :" + username+" Password"+" :" + password+"\n");
// bufferedWriter.write();
JOptionPane.showMessageDialog(null, "Account Created Successfully!");
bufferedWriter.close();
}
} catch (IOException ex){
JOptionPane.showMessageDialog(null, "Error!");
System.out.println("Error! ");
}
So I found out my error
try (BufferedWriter bufferedWriter = new BufferedWriter(filewriter) Visible )
that visible was at end was not in my program which was causing to create new data each timing

ASP MVC Return SP result to View

Using VS 2013 and writing my first ASP MVC app. I have a controller:
// GET: CreateBundlesAndCartons
public ActionResult CreateBandC(Int32 id)
{
string ReturnMessage;
ReturnMessage = "";
using (SqlConnection connection = new SqlConnection())
{
//string connectionStringName = this.DataWorkspace.CooperData.Details.Name;
connection.ConnectionString =
ConfigurationManager.ConnectionStrings["PSAContext"].ConnectionString;
string procedure = "PSA.dbo.CreateBundlesAndCartons";
using (SqlCommand command = new SqlCommand(procedure, connection))
{
command.CommandType = CommandType.StoredProcedure;
command.CommandTimeout = 300;
command.Parameters.Add(
new SqlParameter("#JobID", id));
SqlParameter ErrorString = new SqlParameter("#ErrorString", ReturnMessage);
ErrorString.Direction = ParameterDirection.Output;
ErrorString.Size = 4000;
command.Parameters.Add(ErrorString);
connection.Open();
command.ExecuteNonQuery();
// Save Outout Param
ReturnMessage = ErrorString.Value.ToString();
}
}
return Content("You requested the to create bundles and cartons for job ID " + id.ToString() + "<br />Result: " + ReturnMessage + "<br /> ");
}
I want to display the results to the user and them give them ability to return to the jobs view.
I tried this as my return value:
return Content("You requested the to create bundles and cartons for job ID " + id.ToString() + "Result: " + ReturnMessage + " Return to Jobs");
This displays the results and the link:
But the link points to http://localhost:59971/Jobs/CreateBandC/~/Jobs/ instead of http://localhost:59971/Jobs/
How can I fix that?
Is there a better way to return the results?
I'm under some time pressure, so this approach would do for now, but I'd like to actually figure out how to return a more complex type and nicer view
Thanks
mark
Looks like this requires database changes to fix the link. This is typically a result of poor design and tight coupling. So, go in the database and change the a tag in your stored procedure to get the desired result.

Is it possible to subcribe to a sentence of an epl module?

I have deployed an epl module with the code:
InputStream inputFile = this.getClass().getClassLoader().getResourceAsStream("Temperature.epl");
if (inputFile == null) {
inputFile = this.getClass().getClassLoader().getResourceAsStream("etc/Temperature.epl");
}
if (inputFile == null) {
throw new RuntimeException("Failed to find file 'Temperature.epl' in classpath or relative to classpath");
}
try {
epService.getEPAdministrator().getDeploymentAdmin().readDeploy(inputFile, null, null, null);
// subscribers Ok, tested before whith epService.getEPAdministrator().createEPL ()
// sentences ok, printed
EPStatement statement;
statement = epService.getEPAdministrator().getStatement("Monitor");
System.out.println(statement.getText() + ";");
statement.setSubscriber(new MonitorEventSubscriber());
statement = epService.getEPAdministrator().getStatement("Warning");
System.out.println(statement.getText() + ";");
statement.setSubscriber(new WarningEventSubscriber());
statement = epService.getEPAdministrator().getStatement("Error");
System.out.println(statement.getText() + ";");
statement.setSubscriber(new ErrorEventSubscriber());
}
catch (Exception e) {
throw new RuntimeException("Error deploying EPL from 'Temperature.epl': " + e.getMessage(), e);
}
I can get the sentences by statement.getText(), but the subscribers are not activated. What it's wrong?
I'm working with Esper 5.0.0
Seeing that your code uses the current classloader, you'd want to make sure the classloader is the same else you can get different engine instances.
Also have your code actually send an event to see if it matches since this code doesn't send events.

OleDbConnection to Excel File in MOSS 2007 Shared Documents

I need to programmatically open an Excel file that is stored in a MOSS 2007 Shared Documents List. I’d like to use an OleDbConnection so that I may return the contents of the file as a DataTable. I believe this is possile since a number of articles on the Web imply this is possible. Currently my code fails when trying to initialize a new connection (oledbConn = new OleDbConnection(_connStringName); The error message is:
Format of the initialization string does not conform to specification starting at index 0.
I believe I am just not able to figure the right path to the file. Here is my code:
public DataTable GetData(string fileName, string workSheetName, string filePath)
{
// filePath == C:\inetpub\wwwroot\wss\VirtualDirectories\80\MySpWebAppName\Shared Documents\FY12_FHP_SPREADSHEET.xlsx
// Initialize global vars
_connStringName = DataSource.Conn_Excel(fileName, filePath).ToString();
_workSheetName = workSheetName;
dt = new DataTable();
//Create the connection object
if (!string.IsNullOrEmpty(_connStringName))
{
SPSecurity.RunWithElevatedPrivileges(delegate()
{
oledbConn = new OleDbConnection(_connStringName);
try
{
oledbConn.Open();
//Create OleDbCommand obj and select data from worksheet GrandTotals
OleDbCommand cmd = new OleDbCommand("SELECT * FROM " + _workSheetName + ";", oledbConn);
//create new OleDbDataAdapter
OleDbDataAdapter oleda = new OleDbDataAdapter();
oleda.SelectCommand = cmd;
oleda.Fill(dt);
}
catch (Exception ex)
{
System.Diagnostics.Debug.WriteLine(ex.Message);
}
finally
{
oledbConn.Close();
}
});
}
return dt;
}
public static OleDbConnection Conn_Excel(string ExcelFileName, string filePath)
{
// filePath == C:\inetpub\wwwroot\wss\VirtualDirectories\80\MySpWebAppName\Shared Documents\FY12_FHP_SPREADSHEET.xlsx
OleDbConnection myConn = new OleDbConnection();
myConn.ConnectionString = string.Format(#"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + filePath + ";Extended Properties=Excel 12.0");
return myConn;
}
What am I doing wrong, or is there a better way to get the Excel file contents as a DataTable?
I ended up using the open source project Excel Data Reader

Resources