11 Replies - 4034 Views - Last Post: 17 July 2011 - 09:06 PM Rate Topic: -----

#1 cpatton300  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 16-October 09

Help with JOptionPane.showMessageDialog(text does not fit correctly)

Posted 15 July 2011 - 10:47 PM

I am having a Problem with my output not fitting correctly. Is there a way to resize a JOptionPane.showMessageDialog window? Or do I have to use a different method to show my output? If you need to see the code for my GUI let me know and I can post it as well.

  private void checkRecActionPerformed(java.awt.event.ActionEvent evt) {                                         
         try {
            Connection conn= DriverManager.getConnection("jdbc:mysql://127.0.0.1/abccomp", "root", "root");
            Statement stmt = conn.createStatement();
            ResultSet rs;

            rs = stmt.executeQuery("SELECT * FROM orders;");

            String createdBy;
            String date;
            int orderNum;
            int numNotebooks;
            int numPencils;
            int numPens;
            int numPaper;
            int numStickers;
            int numPaperClips;
            int numHighlighters;
            int numOfficeChairs;
            int numMarkers;
            String recOrd = "";

            while ( rs.next() )
            {
                createdBy = rs.getString("createdby");
		date = rs.getString("DateCreated");
                orderNum = rs.getInt("ordernum");
                numNotebooks = rs.getInt("number_of_Notebooks");
                numPencils = rs.getInt("number_of_Pencils");
                numPaper = rs.getInt("number_of_Paper");
                numStickers = rs.getInt("number_of_Stickers");
                numPaperClips = rs.getInt("number_of_PaperClips");
                numHighlighters = rs.getInt("number_of_Highlighters");
                numOfficeChairs = rs.getInt("number_of_OfficeChairs");
                numMarkers = rs.getInt("number_of_Markers");

                recOrd += "Order number:" + orderNum + "\nDate:" + date
                 + "\nOrder created by:" + createdBy + "Order Details:\n"
                 + numNotebooks + ":Notebooks" + numPencils + ":Pencils"
                 + numPaper + ":Paper" + numStickers + ":Stickers"
                 + numPaperClips + ":Paper Clips" + numHighlighters
                 + ":Highlighters" + numOfficeChairs + ":Office Chairs" 
                 + numMarkers + ":Markers\n\n";
            }

            JOptionPane.showMessageDialog(null, recOrd, "Recent Orders",JOptionPane.PLAIN_MESSAGE);

            conn.close();
            }
           catch (Exception e) {
            System.err.println("Got an exception! ");
            System.err.println(e.getMessage());
            }


    }                                        

    private void checkInvActionPerformed(java.awt.event.ActionEvent evt) {                                         
        try {
            Connection conn= DriverManager.getConnection("jdbc:mysql://127.0.0.1/abccomp", "root", "root");
            Statement stmt = conn.createStatement();
            ResultSet rs;

            rs = stmt.executeQuery("SELECT * FROM inventory;");

            int number;
            String product;
            String inv = "";

            while ( rs.next() )
            {
                number = rs.getInt("number in stock");
		product = rs.getString("product");
                inv += product +":"+ number +"\n";
            }
            
            JOptionPane.showMessageDialog(null, "Inventory in stock\n" + inv, "Inventory in stock", JOptionPane.PLAIN_MESSAGE);

            conn.close();
          } catch (Exception e) {
            System.err.println("Got an exception! ");
            System.err.println(e.getMessage());
        }
    }                                        



Thanks in advance,
Chris

Is This A Good Question/Topic? 0
  • +

Replies To: Help with JOptionPane.showMessageDialog(text does not fit correctly)

#2 GregBrannon  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2194
  • View blog
  • Posts: 5,222
  • Joined: 10-September 10

Re: Help with JOptionPane.showMessageDialog(text does not fit correctly)

Posted 15 July 2011 - 11:07 PM

I'm guessing it's the second one that you're having the problem with. Unfortunately, standard string controls like return, tab, etc. don't work for strings displayed in JOptionPanes, but there's a solution: HTML formatted text. First create the string as HTML formatted text then send it to the JOptionPane. Here's an article to get you started.
Was This Post Helpful? 1
  • +
  • -

#3 g00se  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 2557
  • View blog
  • Posts: 10,673
  • Joined: 20-September 08

Re: Help with JOptionPane.showMessageDialog(text does not fit correctly)

Posted 16 July 2011 - 01:11 AM

You'd probably be better off using a JTable to display it:


JOptionPane.showMessageDialog(null, panelWithJTableInScrollPane);

Was This Post Helpful? 1
  • +
  • -

#4 cpatton300  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 16-October 09

Re: Help with JOptionPane.showMessageDialog(text does not fit correctly)

Posted 16 July 2011 - 01:38 PM

View Postg00se, on 16 July 2011 - 02:11 AM, said:

You'd probably be better off using a JTable to display it:


JOptionPane.showMessageDialog(null, panelWithJTableInScrollPane);


I think this is what I will try. Are there any good tutorials for making a JTable for mysql output? Sorry I am new to java and I do not know that much about Swing and GUI's.
Was This Post Helpful? 0
  • +
  • -

#5 g00se  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 2557
  • View blog
  • Posts: 10,673
  • Joined: 20-September 08

Re: Help with JOptionPane.showMessageDialog(text does not fit correctly)

Posted 16 July 2011 - 02:20 PM

Try the following


    private void checkInvActionPerformed(java.awt.event.ActionEvent evt) {
        try {
            Connection conn = DriverManager.getConnection("jdbc:mysql://127.0.0.1/abccomp",
                    "root", "root");

            Statement stmt = conn.createStatement();

            ResultSet rs = stmt.executeQuery("SELECT * FROM inventory;");

	    JPanel panel = new JPanel();
	    panel.setLayout(new BorderLayout());
	    panel.add(new JScrollPane(new JTable(DbUtils.resultSetToTableModel(rs))));
            JOptionPane.showMessageDialog(null, panel);

            conn.close();
        } catch (Exception e) {
            System.err.println("Got an exception! ");

            System.err.println(e.getMessage());
        }
    }



See http://technojeeves....t-to-tablemodel

A couple of points about that:

a. if you don't need *, don't select *
b. don't use a 'root' account. Create a user with the most restricted privileges possible
Was This Post Helpful? 1
  • +
  • -

#6 cpatton300  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 16-October 09

Re: Help with JOptionPane.showMessageDialog(text does not fit correctly)

Posted 16 July 2011 - 06:33 PM

I am getting an error message.
cannot find symbol:
Symbol: variable DbUtils

any ideas?
Was This Post Helpful? 0
  • +
  • -

#7 g00se  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 2557
  • View blog
  • Posts: 10,673
  • Joined: 20-September 08

Re: Help with JOptionPane.showMessageDialog(text does not fit correctly)

Posted 17 July 2011 - 03:37 AM

Did you download the jar from the link i posted and add it to your classpath?

See the explanatory note i added at http://technojeeves....t-to-tablemodel

This post has been edited by g00se: 17 July 2011 - 03:44 AM

Was This Post Helpful? 1
  • +
  • -

#8 cpatton300  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 16-October 09

Re: Help with JOptionPane.showMessageDialog(text does not fit correctly)

Posted 17 July 2011 - 06:51 AM

I got it to work but my output is still not fitting correctly. Here is a screenshot for reference

Posted Image

Is there a way to make all the information fit and add scroll bars?
Was This Post Helpful? 0
  • +
  • -

#9 g00se  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 2557
  • View blog
  • Posts: 10,673
  • Joined: 20-September 08

Re: Help with JOptionPane.showMessageDialog(text does not fit correctly)

Posted 17 July 2011 - 07:02 AM

Did you use the exact code i posted or something else?
Was This Post Helpful? 1
  • +
  • -

#10 cpatton300  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 16-October 09

Re: Help with JOptionPane.showMessageDialog(text does not fit correctly)

Posted 17 July 2011 - 08:32 AM

View Postg00se, on 17 July 2011 - 08:02 AM, said:

Did you use the exact code i posted or something else?


I used the exact code.
Was This Post Helpful? 0
  • +
  • -

#11 g00se  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 2557
  • View blog
  • Posts: 10,673
  • Joined: 20-September 08

Re: Help with JOptionPane.showMessageDialog(text does not fit correctly)

Posted 17 July 2011 - 10:21 AM

Try the following:


    private void checkInvActionPerformed(java.awt.event.ActionEvent evt) {
        try {
            Connection conn = DriverManager.getConnection("jdbc:mysql://127.0.0.1/abccomp",
                    "root", "root");

            Statement stmt = conn.createStatement();

            ResultSet rs = stmt.executeQuery("SELECT * FROM inventory;");

            JPanel panel = new JPanel();

            panel.setLayout(new BorderLayout());

            JTable table = new JTable(DbUtils.resultSetToTableModel(rs));
            sizeAllColumnsToFitData(table);
            panel.add(new JScrollPane(table));

            conn.close();

            JDialog d = new JDialog();
            d.setContentPane(panel);
            d.pack();
            d.setVisible(true);
        } catch (Exception e) {
            System.err.println("Got an exception! ");

            System.err.println(e.getMessage());
        }
    }

    // Originally at http://www.thatsjava.com/java-swing/32499/
    public static void sizeAllColumnsToFitData(JTable table) {
        for (int col = 0; col < table.getColumnCount(); col++) {
            TableColumn curColumn = table.getColumn(table.getColumnName(col));

            if (curColumn == null) {
                continue; // not a valid column skip
                          // Translate to the model
            }

            int modelColumn = curColumn.getModelIndex();

            // Loop for all rows in this column looking for the widest piece of data
            DefaultTableColumnModel dcm = (DefaultTableColumnModel) table.getTableHeader()
                                                                         .getColumnModel();
            TableColumn curHeader = dcm.getColumn(col);
            Object header = curHeader.getHeaderValue();
            TableCellRenderer curHeaderRenderer = curHeader.getCellRenderer();

            if (curHeaderRenderer == null) {
                curHeaderRenderer = new DefaultTableCellRenderer();
            }

            Component curHeaderRenderComponent = curHeaderRenderer.getTableCellRendererComponent(table,
                    header, true, true, -1, modelColumn);
            Dimension headerDimension = curHeaderRenderComponent.getPreferredSize();
            int maxColumnWidth = headerDimension.width;

            for (int row = 0; row < table.getRowCount(); row++) {
                TableCellRenderer curCellRenderer = table.getCellRenderer(row,
                        modelColumn);
                Object value = table.getValueAt(row, modelColumn);
                Component curRenderComponent = curCellRenderer.getTableCellRendererComponent(table,
                        value, true, true, row, modelColumn);
                Dimension cellDimension = curRenderComponent.getPreferredSize();

                if (cellDimension.width > maxColumnWidth) {
                    maxColumnWidth = cellDimension.width;
                }
            }

            // Set the column width to fit the maximum
            Dimension cellSpacing = table.getIntercellSpacing();
            curColumn.setPreferredWidth(maxColumnWidth +
                ((cellSpacing != null) ? cellSpacing.width : 1));
        }
    }


Was This Post Helpful? 1
  • +
  • -

#12 cpatton300  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 16-October 09

Re: Help with JOptionPane.showMessageDialog(text does not fit correctly)

Posted 17 July 2011 - 09:06 PM

I got it to work. I ended up setting the column widths and then adding the table to a JScrollPane.

here is what I ended up using.
    private void checkRecActionPerformed(java.awt.event.ActionEvent evt) {                                         

        try {
        Connection conn = DriverManager.getConnection("jdbc:mysql://127.0.0.1/abccomp",
                "root", "root");
        
        Statement stmt = conn.createStatement();

        ResultSet rs = stmt.executeQuery("SELECT * FROM orders;");


        JTable table1 = new JTable(DbUtils.resultSetToTableModel(rs));
 
        table1.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
        table1.getColumnModel().getColumn(0).setMinWidth(100);
        table1.getColumnModel().getColumn(1).setMinWidth(100);
        table1.getColumnModel().getColumn(2).setMinWidth(100);
        table1.getColumnModel().getColumn(3).setMinWidth(100);
        table1.getColumnModel().getColumn(4).setMinWidth(100);
        table1.getColumnModel().getColumn(6).setMinWidth(100);
        table1.getColumnModel().getColumn(7).setMinWidth(100);
        table1.getColumnModel().getColumn(8).setMinWidth(100);
        table1.getColumnModel().getColumn(9).setMinWidth(100);
        table1.getColumnModel().getColumn(10).setMinWidth(100);
        table1.getColumnModel().getColumn(11).setMinWidth(100);
 
        JScrollPane jsp = new JScrollPane(table1);

        JOptionPane.showMessageDialog(null, jsp, "Inventory status", JOptionPane.PLAIN_MESSAGE);

        conn.close();
    } 
            catch (Exception e) {
        System.err.println("Got an exception! ");
        System.err.println(e.getMessage());
    }


Thank you so much for all of the help.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1