I try to retrieve data from database and populate it in the Jlist. After that, when I click the Jlist, the data will be populated to 3 textfields. Two of them will be populated to a Jeditorpane as a combination in the form of a html file. I have tried the following method but it creates an error.
String meaning1 = txtMeanings.getText();
String source1 = txtSources.getText();
String htmlText = "<html>"
+ "<body>"
+ "<div class='content'>{meaning1}</div>"
+ "<div class='footer'>{source1}</div>"
+"</body>"
+ "</html>";
JScrollPane scrollPane_1 = new JScrollPane();
scrollPane_1.setBounds(10, 40, 529, 387);
panel_2.add(scrollPane_1);
HTMLEditorKit hed = new HTMLEditorKit();
StyleSheet ss = hed.getStyleSheet();
ss.addRule("BODY {...}");
ss.addRule("h1{...}");
ss.addRule("p{...}");
ss.addRule("blockquote{...}");
ss.addRule("#title{...}");
ss.addRule("hr{...}");
ss.addRule("#content{ ...}");
ss.addRule("#footer{...}");
Document doc = hed.createDefaultDocument();
editorPane = new JEditorPane();
editorPane.setContentType("text/html");
editorPane.setEditorKit(hed);
editorPane.setDocument(doc);
editorPane.setEditable(false);
scrollPane_1.setViewportView(editorPane);
ListboxEntry.addListSelectionListener(new ListSelectionListener() {
public void valueChanged(ListSelectionEvent arg0) {
try
{
String query ="select EntryLists, Meanings, Sources from Entry where EntryLists like ? ";
PreparedStatement pst=Connection.prepareStatement(query);
pst.setString(1,(String)ListboxEntry.getSelectedValue());
ResultSet rs=pst.executeQuery();
while(rs.next())
{
txtEntry.setText(rs.getString("EntryLists"));
txtMeanings.setText(rs.getString("Meanings"));
txtSources.setText(rs.getString("Sources"));
}
pst.close();
rs.close();
} catch (Exception e) {
e.printStackTrace();}
editorPane.setText(htmlText);
}
});
What should I do? Is is possible to populate two data (meaning and source) directly to Jeditorpane without populating it first in the two textfield? In the VB.Net, I solve the problem simply by putting this simple code:
WebBrowser1.DocumentText = String.Format("<html><head><style><!--body{{....}}--></style></head></head><body><div id=content>{0}</div><div id=footer><b>Notes:<br></b>{1} </div></body></html>", txtMeanings.Text, txtSources.Text)
How to do that in Java?
I solve my own problem by changing to the following code:
ListboxEntry.addListSelectionListener(new ListSelectionListener() {
public void valueChanged(ListSelectionEvent arg0) {
try
{
String query ="select EntryLists, Meanings, Sources from Entry where EntryLists like ? ";
PreparedStatement pst=Connection.prepareStatement(query);
pst.setString(1,(String)ListboxEntry.getSelectedValue());
ResultSet rs=pst.executeQuery();
while(rs.next())
{
txtEntry.setText(rs.getString("EntryLists"));
txtMeanings.setText(rs.getString("Meanings"));
txtSources.setText(rs.getString("Sources"));
}
pst.close();
rs.close();
} catch (Exception e) {
e.printStackTrace();}
String htmlText = "<html>"
+ "<body>"
+ "<div class='content'>"+ txtMeanings.getText() +"</div>"
+ "<div class='footer'>"+ txtSources.getText() +"</div>"
+"</body>"
+ "</html>";
editorPane.setText(htmlText);
}
});
But, if anyone wants to improve it, your suggestion is welcome.
Related
The following code was working fine. I then transported the files to another location (another instance of VS2019 Community). Now, the exception doesn't get caught any more and the code breaks during debug. How can this be explained? Thanks
private List<string> getMembers(string objectName)
{
List<string> result = new List<string>();
List<string> listMembers = new List<string>();
try
{
PrincipalContext pc = new PrincipalContext(ContextType.Domain, domain);
UserPrincipal up = UserPrincipal.FindByIdentity(pc, IdentityType.SamAccountName, objectName);
string groupList = "";
foreach (var group in up.GetGroups())
{
groupList += group.SamAccountName + "|";
}
result.Add(objectName + tab + groupList);
}
catch (Exception ex)
{
result.Add(objectName + tab + "Error: " + ex.Message);
}
return result;
}
Case of corrupted/missing pdb files. Nothing wrong with code.
hi guys i've two questions !
i am using NetBeans 8.2 and SceneBuilder for gui
t've a JFXTableView field by data from SQL i want to add a column Action contain two buttons edit and delete for each row
and i want to edit rows and update data in my sql , i use this for update but i don't know how to implent them
public static void modifierElement(int id, String nom, int prix, int qnt) {
try {
String query = "UPDATE element SET element='" + nom
+ "', prix=" + prix
+ ", quantite=" + qnt
+ " WHERE id=" + id;
cnx = connecterDB();
st = cnx.createStatement();
st.executeUpdate(query);
System.out.println("Produit bien modifié");
} catch (SQLException e) {
System.out.println(e.getMessage());
}
}
public static Connection connecterDB() {
try {
Class.forName("com.mysql.jdbc.Driver");
//System.out.println("Driver oki");
String url = "jdbc:mysql://127.0.0.1:3306/taxiphone";
String user = "root";
String password = "";
Connection cnx = DriverManager.getConnection(url, user, password);
//System.out.println("Connexion bien établié");
return cnx;
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
i tried to use this
TableElement.setEditable(true);
clmID.setCellFactory(TextFieldTableCell.forTableColumn());
clmELement.setCellFactory(TextFieldTableCell.forTableColumn());
clmPrix.setCellFactory(TextFieldTableCell.forTableColumn());
clmQuantite.setCellFactory(TextFieldTableCell.forTableColumn());
it change just instance it didnt't saved in my SQL Database thank you guys
For action column you can fill each cell by using HBox (containing delete and update buttons). For each button in HBox you can set listener and perform database operations accordingly
actionCol.setCellValueFactory(new Callback<TableColumn.CellDataFeatures<Person,HBox>, ObservableValue<HBox>>() {
#Override
public ObservableValue<HBox> call(CellDataFeatures<Person, HBox> currentRow)
{
Button updateButton = new Button("update");
Button deleteButton = new Button("delete");
updateButton.setOnAction(e->{
//Database operation to update record goes here
});
deleteButton.setOnAction(e->{
//Database operation to deleted record goes here
});
ObservableValue<HBox> s = new SimpleObjectProperty<>(new HBox(updateButton,deleteButton));
return s;
}
});
Good Day I am completely new to coding. I am building an app which uses a combobox besides other library items. The problem I am facing is that while attempting to populate combobox items from a Mysql Db the item values get duplicated each time the drop down is clicked.
How I can keep this from happening ? I do understand that my approach itself could be erroneous.
#FXML
public void getStation() {
String sqlStationName = " select * from station ";
try {
conn = (Connection) DBConnection.connect();
PreparedStatement pstStn = conn.prepareStatement(sqlStationName);
ResultSet stnRS = pstStn.executeQuery(sqlStationName);
while (stnRS.next()) {
comboBoxStation.getItems().add(stnRS.getString("stationName"));
}
stnRS.close();
pstStn.close();
conn.close();
} catch (SQLException ex) {
System.err.println("ERR" + ex);
}
}
Ok so I moved the function to the initialize() method in the controller and created an Observabale list called station
private ObservableList<String> stationsList = FXCollections.observableArrayList();
#Override
public void initialize(URL url, ResourceBundle rb) {
//
String sqlStationName = " select * from station ";
try {
conn = (Connection) DBConnection.connect();
PreparedStatement pstStn = conn.prepareStatement(sqlStationName);
ResultSet stnRS = pstStn.executeQuery(sqlStationName);
while (stnRS.next()) {
stationsList.add(stnRS.getString("stationName"));
}
stnRS.close();
pstStn.close();
conn.close();
} catch (SQLException ex) {
System.err.println("ERR" + ex);
}
}
and then left only this line in the original function....seems to be working.
#FXML
private void getStation() {
comboBoxStation.setItems(stationsList);
}
I'm trying to use HTMLUnit to get the javascript elements on a webpage (https://www.coursera.org/courses), and it is only loading the html data. How do I get it to display the information shown in the javascript container?
Thanks!
My current code:
public String DownloadPage(String str){
final WebClient webClient = new WebClient(BrowserVersion.FIREFOX_3_6);
webClient.getOptions().setTimeout(20000);
webClient.getOptions().setJavaScriptEnabled(true);
webClient.getOptions().setThrowExceptionOnScriptError(false);
try{
HtmlPage page = webClient.getPage(str);
XmlPage page2 = webClient.getPage(str);
int n = webClient.waitForBackgroundJavaScript(100000);
System.out.println("Executing " + n + " JavaSript jobs!");
System.out.println("OUTPUT: " + page2);
System.out.println("OUTPUT: " + page.asXml());
webClient.closeAllWindows();
}
catch(IOException e){
JOptionPane.showMessageDialog(null, "error");
}
webClient.closeAllWindows();
return "";
}
use
String theContent1 = webClient.getPage(theURL).getWebResponse().getContentAsString();
instead of
String theContent2 = webClient.getPage(theURL);
theContent1 should contain the actual page source including JavaScripts (if any).
I tried some code but nothing.
Me.DataGridView1.Refresh()
Why?
My datagridview is automatically connected to database,not manualy using commands to make connection.
Try the below code... i think you except the code like below....
//Text box Change Event
private void textBox1_TextChanged(object sender, EventArgs e)
{
if (textBox1.Text != "")
GetData("select * from Sample where num =" + textBox1.Text);
else
GetData("select * from Sample");
}
//Data Bind Event by using BindingSource
private void GetData(string selectCommand)
{
try
{
String connectionString ="Your Connection String";
OleDbDataAdapter dataAdapter = new OleDbDataAdapter(selectCommand, connectionString);
OleDbCommandBuilder commandBuilder = new OleDbCommandBuilder(dataAdapter);
DataTable table = new DataTable();
table.Locale = System.Globalization.CultureInfo.InvariantCulture;
dataAdapter.Fill(table);
//BindingSource - Binding
sampleBindingSource.DataSource= table;
dataGridView1.AutoResizeColumns(
DataGridViewAutoSizeColumnsMode.AllCells );
}
catch (SqlException)
{
MessageBox.Show("Error Occured");
}
}