1,076,408 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?

Posts by mKorbel Contributing to Articles being Marked Solved

there are see two issues

  1. decimal separator is dot programming languages

  2. use Numbers instance with Currency formatter, then output to the Java GUI will be 10,00, but stored with dot internally

mKorbel
Nearly a Posting Virtuoso
1,215 posts since Feb 2011
Reputation Points: 482
Solved Threads: 239
Skill Endorsements: 12
  1. model = new DefaultListModel(); should be declared as local variable or separate class

  2. List = new JList( this.observerlink.getlist() );

    a) List is reserved Java word for util.List and awt.List ....change that to myList (Java is CaseSensitive, for more infor to search for Java naming convention)

    b) myList should be local variable

  3. this.scrolllist = new JScrollPane(logList); ... as local variasble

  4. nobody knows whats drawGUI(); probably add JScrollPane with JList to the alreasy visible GUI, revalidate() notify used LayoutManager correctly

  5. don't to create, remove, modify the same JComponents on the runtime, use DeafultListModel, add, remove, modify value there, then all circus about add a new JList is more than contraproductive

  6. for better help sooner post an SSCCE, short, runnable, compilable, just about JFrame with one JList wrapped in JScrollPane

mKorbel
Nearly a Posting Virtuoso
1,215 posts since Feb 2011
Reputation Points: 482
Solved Threads: 239
Skill Endorsements: 12
  • crossposted

  • again don't paint to the container directly, use JComponent, JPanel, JLabel

  • prepare an Image in SW that can do that, is designated for, save as png, put ImageIcon to the JLabel

mKorbel
Nearly a Posting Virtuoso
1,215 posts since Feb 2011
Reputation Points: 482
Solved Threads: 239
Skill Endorsements: 12

search for DragLayout made by @camickr will helpo you with most of issues

mKorbel
Nearly a Posting Virtuoso
1,215 posts since Feb 2011
Reputation Points: 482
Solved Threads: 239
Skill Endorsements: 12
  • crossposted

  • create an SSCCE, short, runnable, compilable, otherwise anyone can ended with "Whats ModelEquivalent"

mKorbel
Nearly a Posting Virtuoso
1,215 posts since Feb 2011
Reputation Points: 482
Solved Threads: 239
Skill Endorsements: 12
  • never to paint to the Top-Level Containers, use JPanel instead

  • prepare all Objects to the Array (if there are more than one element)

  • use paintComponent inastead of paint

mKorbel
Nearly a Posting Virtuoso
1,215 posts since Feb 2011
Reputation Points: 482
Solved Threads: 239
Skill Endorsements: 12
  • use JFormattedTextField for number instance (similair code example in Oracles tutorial about how to use JFormattedTextField), or JSpinner

  • there are two ways (JTextComponents returns Document, model for JTextComponents, use getDocument rather than getText)

    1. use DocumentListener (in the case that all changes from JTextComponents will going outside)

    2. use DocumentFilter (in the case that all changes are internall, restrict, change or modify own Document)

  • you can to combine DocumentFilter with DocumentListener,

  • use LayoutManagers instead of setBounds e.g.

mKorbel
Nearly a Posting Virtuoso
1,215 posts since Feb 2011
Reputation Points: 482
Solved Threads: 239
Skill Endorsements: 12

use NumberFormat or DecimalFormat for number instance, set proper RoundingMode and decimal places if needed

for example

import java.math.RoundingMode;
import java.text.NumberFormat;
import java.util.Formatter;

public class round {

    public static double roundTo2Places(double value) {
        assert value >= Long.MIN_VALUE / 100 && value <= Long.MAX_VALUE / 100;
        long digits = (long) (value < 0 ? value * 100 - 0.5 : value * 100 + 0.5);
        return (double) digits / 100;
    }

    public static void main(String[] args) {
        NumberFormat nf = NumberFormat.getNumberInstance();
        nf.setMaximumFractionDigits(2);
        System.out.println("Default rounding mode: " + nf.getRoundingMode());
        System.out.println("123.454 rounds to " + nf.format(123.454));
        System.out.println("123.455 rounds to " + nf.format(123.455));
        System.out.println("123.456 rounds to " + nf.format(123.456));
        nf.setRoundingMode(RoundingMode.HALF_DOWN);
        System.out.println("Default rounding mode: " + nf.getRoundingMode());
        System.out.println("123.454 rounds to " + nf.format(123.454));
        System.out.println("123.455 rounds to " + nf.format(123.455));
        System.out.println("123.456 rounds to " + nf.format(123.456));
        nf.setRoundingMode(RoundingMode.FLOOR);
        System.out.println("Default rounding mode: " + nf.getRoundingMode());
        System.out.println("123.454 rounds to " + nf.format(123.454));
        System.out.println("123.455 rounds to " + nf.format(123.455));
        System.out.println("123.456 rounds to " + nf.format(123.456));
        nf.setRoundingMode(RoundingMode.CEILING);
        System.out.println("Default rounding mode: " + nf.getRoundingMode());
        System.out.println("123.454 rounds to " + nf.format(123.454));
        System.out.println("123.455 rounds to " + nf.format(123.455));
        System.out.println("123.456 rounds to " + nf.format(123.456));
        System.out.println();
        Formatter fmt = new Formatter();
        fmt.format("%.2f", 123.1234567);
        System.out.println(fmt);
        int number = 1500;
        String formatted = String.format("%07d", number);
        System.out.println("Number with leading zeros: " + formatted);
        fmt = new Formatter();
        fmt.format("%1.4f", 1234567890.123456789);
        System.out.println(fmt);
    }
}
mKorbel
Nearly a Posting Virtuoso
1,215 posts since Feb 2011
Reputation Points: 482
Solved Threads: 239
Skill Endorsements: 12
mKorbel
Nearly a Posting Virtuoso
1,215 posts since Feb 2011
Reputation Points: 482
Solved Threads: 239
Skill Endorsements: 12

harinath_2007 wrote Whenever the text in the textarea fills up completely , i want the scrollbar to scroll down.

there are two ways:

1st. way

JTextArea textArea = new JTextArea();
DefaultCaret caret = (DefaultCaret)textArea.getCaret();
caret.setUpdatePolicy(DefaultCaret.ALWAYS_UPDATE);

2nd. way

  • get the JScrollBar from JScrollPane as local variable,
  • add DocumentListener to JTextArea
  • from proper Document event to call max value for JScrollbar

after

mKorbel
Nearly a Posting Virtuoso
1,215 posts since Feb 2011
Reputation Points: 482
Solved Threads: 239
Skill Endorsements: 12

@jalpesh_007

mKorbel
Nearly a Posting Virtuoso
1,215 posts since Feb 2011
Reputation Points: 482
Solved Threads: 239
Skill Endorsements: 12
  • don't to recreate a new JComboBoxes instance inside ItemListener, then you lost referrence to the original JComboBox (fired event), create JComboBox only once time,

  • add ItemListener to both separatelly only to test SELECTED inside public void itemStateChanged(ItemEvent ie)

  • create ItemListaner class, there you determine which one firing an event, then to test for selected Item

  • for example (to 1st point)

code

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class ComboBoxListeners {

    private JFrame f;
    private JComboBox flyFromCombo;
    private JComboBox flyToCombo;
    private JLabel tripLabel = new JLabel();
    private Object[] itemsFrom;
    private Object[] itemsTo;

    public ComboBoxListeners() {
        itemsFrom = new Object[]{"-", "First - From", "Second - From", "Third - From"};
        itemsTo = new Object[]{"-", "First - To", "Second - To", "Third - To"};
        //flyFromCombo.setPrototypeDisplayValue("################################################");
        flyFromCombo = new JComboBox(itemsFrom);
        flyFromCombo.addItemListener(new ItemListener() {

            @Override
            public void itemStateChanged(ItemEvent e) {
                if ((e.getStateChange() == ItemEvent.SELECTED)) {
                    String str = flyFromCombo.getSelectedItem().toString();
                    String str1 = flyToCombo.getSelectedItem().toString();
                    setLabelText(str, str1);
                }
            }
        });
        flyToCombo = new JComboBox(itemsTo);
        flyToCombo.addItemListener(new ItemListener() {

            @Override
            public void itemStateChanged(ItemEvent e) {
                if ((e.getStateChange() == ItemEvent.SELECTED)) {
                    String str = flyFromCombo.getSelectedItem().toString();
                    String str1 = flyToCombo.getSelectedItem().toString();
                    setLabelText(str, str1);
                }
            }
        });
        tripLabel.setPreferredSize(new Dimension(400, 30));
        f = new JFrame("ComboBox ItemListeners");
        f.setLayout(new GridLayout(0, 1, 15, 15));
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        f.add(flyFromCombo);
        f.add(flyToCombo);
        f.add(tripLabel);
        f.setLocation(150, 150);
        f.pack();
        f.setVisible(true);
    }

    private void setLabelText(String str1, String str2) {
        String textForLabel = "";
        String helpStringFirst = str1.trim();
        if (helpStringFirst != null && helpStringFirst.length() > 0) {
            if (!helpStringFirst.equals("-")) {
                textForLabel = "Flight No57. from :   " + helpStringFirst;
            } else {
                textForLabel = "Flight from Un-Know :   ";
            }
        }
        String helpStringSecond = str2.trim();
        if (helpStringSecond != null && helpStringSecond.length() > 0) {
            if (!helpStringSecond.equals("-")) {
                textForLabel = textForLabel + "   --> to :   " + helpStringSecond;
            } else {
                textForLabel += "   to :   Un-Know    ";
            }
        }
        final String pushTextForLabel = textForLabel;
        SwingUtilities.invokeLater(new Runnable() {

            @Override
            public void run() {
                tripLabel.setText(pushTextForLabel);
            }
        });
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {

            @Override
            public void run() {
                ComboBoxListeners comboBoxListeners = new ComboBoxListeners();
            }
        });
    }
}
mKorbel
Nearly a Posting Virtuoso
1,215 posts since Feb 2011
Reputation Points: 482
Solved Threads: 239
Skill Endorsements: 12
mKorbel
Nearly a Posting Virtuoso
1,215 posts since Feb 2011
Reputation Points: 482
Solved Threads: 239
Skill Endorsements: 12
  • for why reason you using AWT to use Swing instead

  • then Choice should be JComboBox, (Frame/JFrame, Button/JButton)

  • add ItemListener to JComboBox

  • ItemListener always fired twice events, SELECTED and DESELECTED,

  • can to determine, test if ITEM_STATE_CHANGED only

mKorbel
Nearly a Posting Virtuoso
1,215 posts since Feb 2011
Reputation Points: 482
Solved Threads: 239
Skill Endorsements: 12
  • why bothering with compilier, better could be to post to forum, isn't it .......

  • there isnt created any of two JFrames,

  • there are two main methods remove one of them

  • don't extend JFrame, create that as local variable, otherwise part of important methods isn't accesible directly, more in describtions composition versus inheritance

  • dirty hack to change

from

public class GUIFlowLayout extends GUIFrame {

to

public class GUIFlowLayout extends JFrame {

then GUI could be displayed correctly

  • have look at Initial Thread

  • change FlowLayout to SpringLayout

  • replace frame.setSize(200, 200); with frame.pack(); and move that before JFrame#setVisible(true)

mKorbel
Nearly a Posting Virtuoso
1,215 posts since Feb 2011
Reputation Points: 482
Solved Threads: 239
Skill Endorsements: 12
wordInput.getText().toXxx()
mKorbel
Nearly a Posting Virtuoso
1,215 posts since Feb 2011
Reputation Points: 482
Solved Threads: 239
Skill Endorsements: 12

don't use two or more JFrames, use CardLayout instead, if you need popup window for input of value use JOptionPane

mKorbel
Nearly a Posting Virtuoso
1,215 posts since Feb 2011
Reputation Points: 482
Solved Threads: 239
Skill Endorsements: 12

not full text of the message could be useless (maybe) pot here a SSCCE, because in most cases
talking about to filling the the data to JTable without definitions about ouw model, no way

heavens can you somone to expain me why this a new version of this forum is totally ****,
starting with URL to tutorials .... ending with input of text in answers, crazy !!!, for why
reasons ... I leaving that

mKorbel
Nearly a Posting Virtuoso
1,215 posts since Feb 2011
Reputation Points: 482
Solved Threads: 239
Skill Endorsements: 12

this isn't simple possible override Caret and by using Highlighter together, there are bunch of code lines and required some knowledges about JTextComponents, I'm out of this thread

mKorbel
Nearly a Posting Virtuoso
1,215 posts since Feb 2011
Reputation Points: 482
Solved Threads: 239
Skill Endorsements: 12

remove textArea.setSelectedTextColor(Color.PINK);

mKorbel
Nearly a Posting Virtuoso
1,215 posts since Feb 2011
Reputation Points: 482
Solved Threads: 239
Skill Endorsements: 12
 
© 2013 DaniWeb® LLC
Page rendered in 0.1360 seconds using 2.75MB