Collage

n. A piece of art made by sticking various different materials, aka PHENOMENA Magazine
Department
programming

programming

Simple Q&A
Q : Where can I download JSTL taglibs? i.e. jstl.jar and standard.jar A : If you're running a Servlet 2.5 compatible container and the web.xml is declared as at least Servlet 2.5, then you should be able to use the new JSTL 1.2 instead. Note that JSTL 1.2 does not require a standard.jar. Q : The superclass "javax.servlet.http.HttpServlet" was not found on the Java Build Path A : Add a runtime first and select project properties. Then check the server name from the 'Runtimes' tab Q : Remove html tags from string using java A :  String noHTMLString = htmlString.replaceAll("\\<.*?>",""); Using Jsoup public static String html2text(String html) { return Jsoup.parse(html).text(); } https://stackoverflow.com/questions/4432560/remove-html-tags-from-string-using-java Q : JSPX A : https://jspx-bay.sourceforge.net/ Q : Apache Jakarta A : http://archive.apache.org/dist/jakarta/ Q : Prevent download attempts of videos in a video server A : Silverlight might be an idea to start with. Q : mp3 header information reader not working A : import org.farng.mp3.MP3File; you need to call mp3file.seekMP3Frame(); before attempting to retrieve bitrate, this method will read the file headers including the bitrate. or use http://www.jthink.net/jaudiotagger/examples_id3.jsp Q : How to get the message in a custom error page (Tomcat)? A : <c:out value="${requestScope['javax.servlet.error.message']}"/> javax.servlet.error.status_code java.lang.Integer javax.servlet.error.exception_type java.lang.Class javax.servlet.error.message java.lang.String javax.servlet.error.exception java.lang.Throwable javax.servlet.error.request_uri java.lang.String javax.servlet.error.servlet_name java.lang.String * <c:catch> Tag <%@ taglib uri = "http://java.sun.com/jsp/jstl/core" prefix = "c" %> <html> <head> <title><c:catch> Tag Example</title> </head> <body> <c:catch var ="catchException"> <% int x = 5/0;%> </c:catch> <c:if test = "${catchException != null}"> <p>The exception is : ${catchException} <br /> There is an exception: ${catchException.message}</p> </c:if> </body> </html> Q : Get current action in jsp - struts2 A :  <s:url forceAddSchemeHostAndPort="true" includeParams="all"/> Q : Oracle date to Java date A : little h for "Hour in am/pm (1-12)" and H for "Hour in day (0-23)" see here: SimpleDateFormat SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.S"); Date date = dateFormat.parse("2011-08-19 06:11:03.0"); Q : How to get previous URL? A :  HttpServletRequest.getHeader("Referer"); Q : [JSP] 파일 다운로드창에서 한글깨짐 방지? A : response.setHeader("Content-Disposition", "attachment; filename=" + URLEncoder.encode(fileName, "utf-8") + ";");  
java · Jan. 12, 2023, 2:03 a.m.
How to Display Row Header on JTable Instead of Column Header
This is a proof of concept only Disclaimer: Before I get a bunch of hate mail about the, obviously, horrible things I've done to make this work, I stole most of the painting code straight out of the source, this is how it's actually done within the look and feel code itself :P I've also gone to the nth degree, meaning that I've literally assumed that you wanted the row headers to look like the column headers. If this isn't a requirement, it would be SO much easier to do... Okay, this is a basic proof of concept, which provides the means to generate the row header and render them the same way as they are normally, just as row headers instead. Things that need to be added/supported: Detect when the table model is changed (that is, a new table model is set to the table) Detect when the column model is changed (that is, a new column model is set to the table) Much of this functionality would probably need to be added to the TableWithRowHeader implementation... Basically, what this "tries" to do, is create a custom row header, based on a JTableHeader, remove the existing column header and add itself into the row header view position of the enclosing JScrollPane. import java.awt.BorderLayout; import java.awt.Component; import java.awt.Container; import java.awt.Dimension; import java.awt.EventQueue; import java.awt.Graphics; import java.awt.Graphics2D; import java.awt.GridBagConstraints; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JPanel; import javax.swing.JScrollPane; import javax.swing.JTable; import javax.swing.JViewport; import javax.swing.SwingUtilities; import javax.swing.UIManager; import javax.swing.UnsupportedLookAndFeelException; import javax.swing.event.ChangeEvent; import javax.swing.event.ListSelectionEvent; import javax.swing.event.ListSelectionListener; import javax.swing.event.TableColumnModelEvent; import javax.swing.event.TableColumnModelListener; import javax.swing.table.DefaultTableColumnModel; import javax.swing.table.JTableHeader; import javax.swing.table.TableCellRenderer; import javax.swing.table.TableColumn; import javax.swing.table.TableColumnModel; public class TableRowHeaderTest { public static void main(String[] args) { new TableRowHeaderTest(); } public TableRowHeaderTest() { EventQueue.invokeLater(new Runnable() { @Override public void run() { try { UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName()); } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) { ex.printStackTrace(); } Object rowData1[][] = { {"", "", "", ""}, {"", "", "", ""}, {"", "", "", ""}, {"", "", "", ""} }; Object columnNames1[] = {"HEADER 1", "HEADER 2", "HEADER 3", "HEADER 4"}; JTable table1 = new TableWithRowHeader(rowData1, columnNames1); table1.getColumnModel().getColumn(0).setPreferredWidth(120); JScrollPane scrollPane1 = new JScrollPane(table1); scrollPane1.setColumnHeaderView(null); JFrame frame = new JFrame("Testing"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.add(scrollPane1); frame.pack(); frame.setLocationRelativeTo(null); frame.setVisible(true); } }); } public class TableWithRowHeader extends JTable { private TableRowHeader rowHeader; public TableWithRowHeader(final Object[][] rowData, final Object[] columnNames) { super(rowData, columnNames); rowHeader = new TableRowHeader(this); } @Override protected void configureEnclosingScrollPane() { // This is required as it calls a private method... super.configureEnclosingScrollPane(); Container parent = SwingUtilities.getUnwrappedParent(this); if (parent instanceof JViewport) { JViewport port = (JViewport) parent; Container gp = port.getParent(); if (gp instanceof JScrollPane) { JScrollPane scrollPane = (JScrollPane) gp; JViewport viewport = scrollPane.getViewport(); if (viewport == null || SwingUtilities.getUnwrappedView(viewport) != this) { return; } scrollPane.setColumnHeaderView(null); scrollPane.setRowHeaderView(rowHeader); } } } } public class TableRowHeader extends JTableHeader { private JTable table; public TableRowHeader(JTable table) { super(table.getColumnModel()); this.table = table; table.getColumnModel().addColumnModelListener(new TableColumnModelListener() { @Override public void columnAdded(TableColumnModelEvent e) { repaint(); } @Override public void columnRemoved(TableColumnModelEvent e) { repaint(); } @Override public void columnMoved(TableColumnModelEvent e) { repaint(); } @Override public void columnMarginChanged(ChangeEvent e) { repaint(); } @Override public void columnSelectionChanged(ListSelectionEvent e) { // Don't care about this, want to highlight the row... } }); table.getSelectionModel().addListSelectionListener(new ListSelectionListener() { @Override public void valueChanged(ListSelectionEvent e) { repaint(); } }); } public JTable getTable() { return table; } @Override public Dimension getPreferredSize() { Dimension size = new Dimension(); JTable table = getTable(); if (table != null) { TableColumnModel model = table.getColumnModel(); if (model != null) { for (int index = 0; index < model.getColumnCount(); index++) { TableColumn column = model.getColumn(index); TableCellRenderer renderer = column.getHeaderRenderer(); if (renderer == null) { renderer = getDefaultRenderer(); } Component comp = renderer.getTableCellRendererComponent(table, column.getHeaderValue(), false, false, -1, index); size.width = Math.max(comp.getPreferredSize().width, size.width); size.height += table.getRowHeight(index); } } } return size; } /** * Overridden to avoid propagating a invalidate up the tree when the * cell renderer child is configured. */ @Override public void invalidate() { } /** * If the specified component is already a child of this then we don't bother doing anything - stacking order doesn't matter for cell renderer components * (CellRendererPane doesn't paint anyway). */ @Override protected void addImpl(Component x, Object constraints, int index) { if (x.getParent() == this) { return; } else { super.addImpl(x, constraints, index); } } @Override protected void paintComponent(Graphics g) { // super.paintComponent(g); Graphics2D g2d = (Graphics2D) g.create(); g2d.setColor(getBackground()); g2d.fillRect(0, 0, getWidth(), getHeight()); JTable table = getTable(); if (table != null) { int width = getWidth(); TableColumnModel model = table.getColumnModel(); if (model != null) { for (int index = 0; index < model.getColumnCount(); index++) { TableColumn column = model.getColumn(index); TableCellRenderer renderer = column.getHeaderRenderer(); if (renderer == null) { renderer = getDefaultRenderer(); } boolean selected = table.getSelectedRow() == index; Component comp = renderer.getTableCellRendererComponent(table, column.getHeaderValue(), selected, false, 0, index); add(comp); comp.validate(); int height = table.getRowHeight(index) - 1; comp.setBounds(0, 0, width, height); comp.paint(g2d); comp.setBounds(-width, -height, 0, 0); g2d.setColor(table.getGridColor()); g2d.drawLine(0, height, width, height); g2d.translate(0, height + 1); } } } g2d.dispose(); removeAll(); } } } Disclaimer: This is likely to blow up in your face. I make no checks for preventing the header from responding to things like changes to the column row sortering and ... in theory ... it shouldn't try and "resize" the column but I didn't test that... https://stackoverflow.com/questions/26248084/how-to-display-row-header-on-jtable-instead-of-column-header
java · Jan. 12, 2023, 1:36 a.m.
JTable swing
How do you return a JSON object from a Java Servlet
Write the JSON object to the response object's output stream. You should also set the content type as follows, which will specify what you are returning: response.setContentType("application/json"); // Get the printwriter object from response to write the required json object to the output stream PrintWriter out = response.getWriter(); // Assuming your json object is **jsonObject**, perform the following, it will return your json object out.print(jsonObject); out.flush(); or First convert the JSON object to String. Then just write it out to the response writer along with content type of application/json and character encoding of UTF-8. Here's an example assuming you're using Google Gson to convert a Java object to a JSON string: protected void doXxx(HttpServletRequest request, HttpServletResponse response) { // ... String json = new Gson().toJson(someObject); response.setContentType("application/json"); response.setCharacterEncoding("UTF-8"); response.getWriter().write(json); } That's all. https://stackoverflow.com/questions/2010990/how-do-you-return-a-json-object-from-a-java-servlet  
java · Jan. 12, 2023, 1:30 a.m.
How do I check which version of Python is running my script?
This information is available in the sys.version string in the sys module: >>> import sys Human readable: >>> print(sys.version) # parentheses necessary in python 3. 2.5.2 (r252:60911, Jul 31 2008, 17:28:52) [GCC 4.2.3 (Ubuntu 4.2.3-2ubuntu7)] For further processing, use sys.version_info or sys.hexversion: >>> sys.version_info (2, 5, 2, 'final', 0) # or >>> sys.hexversion 34014192 To ensure a script runs with a minimal version requirement of the Python interpreter add this to your code: assert sys.version_info >= (2, 5) This compares major and minor version information. Add micro (=0, 1, etc) and even releaselevel (='alpha','final', etc) to the tuple as you like. Note however, that it is almost always better to "duck" check if a certain feature is there, and if not, workaround (or bail out). Sometimes features go away in newer releases, being replaced by others. or Use platform's python_version from the stdlib: from platform import python_version print(python_version()) # 3.9.2 https://stackoverflow.com/questions/1093322/how-do-i-check-which-version-of-python-is-running-my-script
python · Jan. 12, 2023, 1:19 a.m.
  • 1
  • 2
  • 3
  • 4
  • 5 (current)