Why is itemStateChanged on JComboBox is called twice when changed?

Have a look at this source: import javax.swing.*; import java.awt.*; import java.awt.event.*; public class Tester { public Tester(){ JComboBox box = new JComboBox(); box.addItem(“One”); box.addItem(“Two”); box.addItem(“Three”); box.addItem(“Four”); box.addItemListener(new ItemListener(){ public void itemStateChanged(ItemEvent e){ System.out.println(e.getItem() + ” ” + e.getStateChange() ); } }); JFrame frame = new JFrame(); frame.getContentPane().add(box); frame.pack(); frame.setVisible(true); } public static void main(String … Read more

Double click listener on JTable in Java

Try this: mytable.addMouseListener(new MouseAdapter() { public void mousePressed(MouseEvent mouseEvent) { JTable table =(JTable) mouseEvent.getSource(); Point point = mouseEvent.getPoint(); int row = table.rowAtPoint(point); if (mouseEvent.getClickCount() == 2 && table.getSelectedRow() != -1) { // your valueChanged overridden method } } });

How to populate JTable from ResultSet?

I think the simplest way to build a model from an instance of ResultSet, could be as follows. public static void main(String[] args) throws Exception { // The Connection is obtained ResultSet rs = stmt.executeQuery(“select * from product_info”); // It creates and displays the table JTable table = new JTable(buildTableModel(rs)); // Closes the Connection JOptionPane.showMessageDialog(null, … Read more

Resize a picture to fit a JLabel

Outline Here are the steps to follow. Read the picture as a BufferedImage. Resize the BufferedImage to another BufferedImage that’s the size of the JLabel. Create an ImageIcon from the resized BufferedImage. You do not have to set the preferred size of the JLabel. Once you’ve scaled the image to the size you want, the … Read more

How can I make my columns different sizes using GridLayout in swing?

If you want this effect then you need to utilize the GridBagLayout. http://download.oracle.com/javase/tutorial/uiswing/layout/gridbag.html Have fun with that one =P EDIT: You can work around the problem by employing a mixture of FlowLayout and GridLayout to get a similar effect. However, this solution will become extremely tedious and messy as your layout complexities become bigger.