Java JTable setting Column Width

What happens if you call setMinWidth(400) on the last column instead of setPreferredWidth(400)? In the JavaDoc for JTable, read the docs for doLayout() very carefully. Here are some choice bits: When the method is called as a result of the resizing of an enclosing window, the resizingColumn is null. This means that resizing has taken … Read more

Open a link in browser with java button? [duplicate]

Use the Desktop#browse(URI) method. It opens a URI in the user’s default browser. public static boolean openWebpage(URI uri) { Desktop desktop = Desktop.isDesktopSupported() ? Desktop.getDesktop() : null; if (desktop != null && desktop.isSupported(Desktop.Action.BROWSE)) { try { desktop.browse(uri); return true; } catch (Exception e) { e.printStackTrace(); } } return false; } public static boolean openWebpage(URL url) … Read more

How to make a JTable non-editable

You can override the method isCellEditable and implement as you want for example: //instance table model DefaultTableModel tableModel = new DefaultTableModel() { @Override public boolean isCellEditable(int row, int column) { //all cells false return false; } }; table.setModel(tableModel); or //instance table model DefaultTableModel tableModel = new DefaultTableModel() { @Override public boolean isCellEditable(int row, int column) … Read more

Java: Difference between the setPreferredSize() and setSize() methods in components

Usage depends on whether the component’s parent has a layout manager or not. setSize() — use when a parent layout manager does not exist; setPreferredSize() (also its related setMinimumSize and setMaximumSize) — use when a parent layout manager exists. The setSize() method probably won’t do anything if the component’s parent is using a layout manager; … Read more

How to add row in JTable?

The TableModel behind the JTable handles all of the data behind the table. In order to add and remove rows from a table, you need to use a DefaultTableModel To create the table with this model: JTable table = new JTable(new DefaultTableModel(new Object[]{“Column1”, “Column2”})); To add a row: DefaultTableModel model = (DefaultTableModel) table.getModel(); model.addRow(new Object[]{“Column … Read more

Multi-line tooltips in Java?

If you wrap the tooltip in <html> and </html> tags, you can break lines with <br> tags. See https://web.archive.org/web/20060625031340/http://www.jguru.com/faq/view.jsp?EID=10653 for examples and discussion. Main take-awy from that discussion: fButton.setToolTipText(“<html><font face=\”sansserif\” color=\”green\”>first line<br>second line</font></html>”); Or you can use the JMultiLineToolTip class that can be found many places on the net, including https://github.com/ls-cwi/yoshiko-app/blob/master/src/main/java/com/yoshiko/internal/view/JMultiLineToolTip.java

Detect enter press in JTextField

A JTextField was designed to use an ActionListener just like a JButton is. See the addActionListener() method of JTextField. For example: Action action = new AbstractAction() { @Override public void actionPerformed(ActionEvent e) { System.out.println(“some action”); } }; JTextField textField = new JTextField(10); textField.addActionListener( action ); Now the event is fired when the Enter key is … Read more

How do I create a right click context menu in Java Swing?

You are probably manually calling setVisible(true) on the menu. That can cause some nasty buggy behavior in the menu. The show(Component, int x, int x) method handles all of the things you need to happen, (Highlighting things on mouseover and closing the popup when necessary) where using setVisible(true) just shows the menu without adding any … Read more

Difference between validate(), revalidate() and invalidate() in Swing GUI

invalidate() marks the container as invalid. Means the content is somehow wrong and must be re-laid out. But it’s just a kind of mark/flag. It’s possible that multiple invalid containers must be refreshed later. validate() performs relayout. It means invalid content is asked for all the sizes and all the subcomponents’ sizes are set to … Read more

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)